【问题标题】:Telling omake to use static version of a c library告诉 omake 使用静态版本的 c 库
【发布时间】:2018-07-26 23:19:18
【问题描述】:
我正在使用 omake 构建本机二进制可执行文件。在它链接后我尝试运行它,它无法运行并出现以下错误:
加载共享库时出错:libprotobuf-c.so.1: cannot open shared object file: No such file or directory
有没有办法在编译时告诉可执行文件选择静态版本:libprotobuf-c.a 而不是 shared on?
【问题讨论】:
标签:
ocaml
protobuf-c
omake
【解决方案1】:
我不熟悉omake,但我相信您正在寻找的ocamlc 的标志是dllpath:
-dllpath dir
Adds the directory dir to the run-time search path for shared C libraries. At link-
time, shared libraries are searched in the standard search path (the one corresponding
to the -I option). The -dllpath option simply stores dir in the produced executable
file, where ocamlrun(1) can find it and use it.
如果您可以配置 omake 以将适当的 -dllpath 参数传递给 ocamlc,那么您应该很高兴。
在底层我相信这是使用ld 的rpath 特性(运行时库搜索路径),GNU 链接器。见https://linux.die.net/man/1/ld。还有一个chrpath 实用程序可以更改已构建的可执行文件的rpath。
另一个选项是运行您的可执行文件并设置LD_LIBRARY_PATH,以便共享库位于加载路径上。如果合适,您还可以在系统范围内安装共享库。最后一个选项是在您的应用程序启动时使用 dlopen 手动加载库。
正确的选择取决于您将如何分发以及分发给谁(如果有的话)。请记住,如果您使用rpath/dllpath,最终用户不太可能将protobuf 安装在与您相同的位置。
【解决方案2】:
似乎没有可以传递给链接器 ld 的全局标志,它强制链接器在可用时更喜欢静态库而不是动态库。就我而言,我像这样明确设置库名称:
OCAML_LINK_FLAGS += -cclib -l:libprotobuf-c.a