golang os.Setenv does not work in cgo C.dlopen?

  c++, cgo, dlopen, go, linux

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!

Source: Windows Questions C++

LEAVE A COMMENT