As Richard points out, LD_PRELOAD only affects the current process, and then only applies to processes spawned via one of the
exec()
functions, or
system(), popen()
etc. It doesn't affect the current process. LD_PRELOAD is a list so you can do
$ LD_PRELOAD=lib1.so:lib2.so:lib3.so myprog
. This does the expected thing and preloads lib1, lib2 and lib3 before starting myprog. There are other environment variables that affect link-load which are detailed in the
ld.so
man page.
If what you are trying to do is to dynamically load a library after program launch, you might want to look into
dlopen()
and friends. There's a very basic intro to them here:
https://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html[
^] So, for example, if you wanted to write a program that the end user might want to use Postgresql, MySQl, Firebird, or some other database. You could write the low level database access functions and put them in a shared library eg pgsql.so, mysql.so, etc. Then based on the config details for the program, selectively load the shared library to talk to the database in question.