For some reason, I can not set $LD_LIBRARY_PATH to global env. I try to set it up in golang code use os.Setenv.
os.Setenv("LD_LIBRARY_PATH", my_library_paths)
lib := C.dlopen(C.CString(libpath), C.RTLD_LAZY)
I use another C++ function to get $LD_LIBRARY_PATH
, it shows corretly.
But lib returns '<nil>', and C.dlerror() shows
>> %!(EXTRA string=libhasp_linux_x86_64_demo.so: cannot open shared object file: No such file or directory)
Means $LD_LIBRARY_PATH does not work in dlopen, cgo can not find depend libraries.
I don't know why.Hope some one can help me.Thanks!
It looks like you're trying to call os.Setenv("LD_LIBRARY_PATH", ...)
and then C.dlopen()
from within the same process.
From the man page dlopen(3):
Otherwise, the dynamic linker searches for the object as follows
...
If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched.
The key phrase being, at the time the program is started. We can see in the implementation for dlopen
in the glibc source elf/dl-load.c that it looks at a global variable __rtld_env_path_list.dirs
that has already been set when searching for libraries to load; it does not look at the current value of $LD_LIBRARY_PATH
.
If you want to use LD_LIBRARY_PATH
to find things in C.dlopen
, then you'll need to set it before your process starts (by running e.g. LD_LIBRARY_PATH=/my/path go run my-app.go
).