使用 setenv 然后进行 dlopen 调用时出现问题
我使用 setenv 来设置 DYLD_LIBRARY_PATH,因此当我执行 dlopen() 时,它将具有找到我的 .dylib 的正确路径,但是当我执行 dlopen() 时,它似乎不会搜索我添加到 DYLD_LIBRARY_PATH 的路径。
据我所知,对 DYLD_LIBRARY_PATH 的更改只有在重新执行我的进程时才会生效。这是正确的吗?
另外,如果这是正确的,有没有办法设置 DYLD_LIBRARY_PATH 并让我的更改工作而无需重置我的进程。
哦,是的,我在 MAC OSX 上编写这段代码。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不了解 Mac OS,但在 Linux 上,加载程序会读取
getenv("LD_LIBRARY_PATH")
的值一次,并将其保存起来,早在第一次读取之前您的可执行文件运行的指令。程序对 LD_LIBRARY_PATH 的后续修改只会影响它的 execve() 的任何子进程,但不会影响进程本身。我想 Mac 操作系统上也是类似的。解决这个问题的常用方法是重新
execve
进程(Java 就是这样做的),或者使用 shell 包装器来设置环境,然后 exec 真正的二进制文件(Firefox 就是这样做的)。可能有 Mac OS 特定的方法来更新库搜索路径,尽管 Google 似乎没有找到任何匹配项。我非常确定 Linux 上没有任何这样的机制。
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 ofLD_LIBRARY_PATH
by the program only affects any children itexecve()
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.
我的问题的答案是否定的,如果不重新执行 LD_LIBRARY_PATH 上的进程以使环境变量对 dlopen 生效,则无法使用 setenv。
我发现您应该使用 @exectuable_path、@loader_path 或 @rpath 作为我的 .dylb 上的安装名称路径,这样您就可以从 dlopen 对 .dylibs 进行相对路径搜索。
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.