Problems with using setenv and then making the dlopen call
Asked Answered
P

2

6

I am using setenv to set DYLD_LIBRARY_PATH so when I do a dlopen() it will have the correct paths to find my .dylib, but when I do the dlopen() it doesn't seem to search the paths that I added to DYLD_LIBRARY_PATH.

From what I can gather my changes to DYLD_LIBRARY_PATH won't take effect until a re-execute my process happens. Is this correct?

Also if that is correct, is there a way to set DYLD_LIBRARY_PATH and having my changes work with out doing a reset of my process.

Oh yeah I writing this code on MAC OSX.

Thanks in advance.

Postboy answered 15/7, 2011 at 22:9 Comment(0)
P
2

The answer to my question is no you can't use setenv without doing a re-execute of the process on LD_LIBRARY_PATH for the environmental variable to take an effect on dlopen.

I found out that you should use @exectuable_path, @loader_path, or @rpath as an install name path on my .dylb is this way you can do a relative path search on your .dylibs from dlopen.

Postboy answered 25/7, 2011 at 21:12 Comment(0)
C
6

I don't know about Mac OS, but on Linux, the loader reads the value of getenv("LD_LIBRARY_PATH") once, and saves it away, long before the first instruction of your executable runs. Subsequent modification of LD_LIBRARY_PATH by the program only affects any children it execve()s, but not the process itself. I imagine it's similar on Mac OS.

The usual way around this is to either re-execve the proces (Java does this), or to use a shell wrapper which sets the environment and then exec's the real binary (Firefox does that).

There might be a Mac OS specific way to update the library search path, though Google doesn't seem to find any matches. I am pretty sure there isn't any such mechanism on Linux.

Counts answered 18/7, 2011 at 3:29 Comment(2)
Currently our software does a re-execve to set the LD_LIBRARY_PATH with our extra paths. I was trying to do it without the re-execve, but maybe that is the only way on the MAC OS. This makes it much harder to debug since the Xcode debug gets interrupted between the first execution and the second.Postboy
Java does the re-exec IFF the existing LD_LIBRARY_PATH is not already correct. Your program must do the same, or it would enter infinite loop. So to debug it, set LD_LIBRARY_PATH to what it would have been set before re-exec (perhaps before starting Xcode), and debug away.Counts
P
2

The answer to my question is no you can't use setenv without doing a re-execute of the process on LD_LIBRARY_PATH for the environmental variable to take an effect on dlopen.

I found out that you should use @exectuable_path, @loader_path, or @rpath as an install name path on my .dylb is this way you can do a relative path search on your .dylibs from dlopen.

Postboy answered 25/7, 2011 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.