You cannot change LD_LIBRARY_PATH at runtime, the dynamic loader reads it once when the program executes and never checks it, you can use dlopen () to load the shared library instead:
dlopen("/path/to/shared/lib.so", RTLD_LAZY);
This will only work when loading the library and using dlsym() at run time to search for characters, otherwise, if you call functions in the library, these links should be allowed at boot time and AFAIK, you should use something like bashscript.
Note. At runtime, you can change LD_LIBRARY_PATH , if you re-execute this process, I just checked it and it seems to work, but it is very hacky, maybe the only way to do this in C :
void *handle; // first time check if path is not set if (getenv("LD_LIBRARY_PATH")==NULL) { //set it and re-execute the process setenv("LD_LIBRARY_PATH", "/path/to/lib/", 1); execl(argv[0], argv[0], NULL); } // open the shared library the second time handle = dlopen("test.so", RTLD_LAZY); printf("%p\n", handle); dlclose(handle);
source share