c++ - "dll-path"在构建 boost 时没有作用

标签 c++ boost dll otool install-name-tool

我的目标是在编译 boost 时通过提供 dll-path 选项在已编译的 boost 库中拥有完整的运行路径:

$ ./b2 dll-path=$(pwd)/build --prefix=$(pwd)/build
$ ./b2 install dll-path=$(pwd)/build --prefix=$(pwd)/build

但是,当我检查 $(pwd)/build 文件夹中的库时,我得到了这个:

$ otool -L build/lib/libboost_system.dylib 
build/lib/libboost_system.dylib:
    libboost_system.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)

即而不是带有 lib 名称的完整路径,只有 lib 名称 (libboost_system.dylib)。应该如何使用 dll-path 选项或者是否有一种“官方”方式来实现这一点(除了在每个库上手动运行 install_name_tool 的脚本之外)?

最佳答案

Why are the dll-path and hardcode-dll-paths properties useful?

However, in order for application depending on shared libraries to be started the OS may need to find the shared library when the application is started. The dynamic linker will search in a system-defined list of paths, load the library and resolve the symbols. Which means that you should either change the system-defined list, given by the LD_LIBRARY_PATH environment variable, or install the libraries to a system location. This can be inconvenient when developing, since the libraries are not yet ready to be installed, and cluttering system paths may be undesirable. Luckily, on Unix there is another way.

An executable can include a list of additional library paths, which will be searched before system paths. This is excellent for development because the build system knows the paths to all libraries and can include them in the executables. That is done when the hardcode-dll-paths feature has the true value, which is the default. When the executables should be installed, the story is different.

Obviously, installed executable should not contain hardcoded paths to your development tree. (The install rule explicitly disables the hardcode-dll-paths feature for that reason.)

我对该文档的解释是,Boost 期望它的库安装在标准系统位置。通过使用 dll-pathhardcode-dll-paths 属性,可以将非标准位置用于 Boost 本身的开发,但此功能在 安装 Boost构建过程的规则:

似乎有几个选项可以在非标准安装位置使用 Boost 共享库:

  1. 根据 https://stackoverflow.com/a/33893062/4313507 ,更新安装的共享库文件中的路径(使用硬编码路径或 RPath):

    install_name_tool libboost_regex.dylib -id @rpath/libboost_regex.dylib
    

    请记住,某些 Boost 组件相互链接,因此请务必同时更新内部链接的路径。示例:

    $ otool -L libboost_filesystem.dylib
    libboost_filesystem.dylib:
      libboost_filesystem.dylib (compatibility version 0.0.0, current version 0.0.0)
      libboost_system.dylib (compatibility version 0.0.0, current version 0.0.0)
    
  2. 使用 LD_LIBRARY_PATH 环境变量来帮助定位共享库。示例:

    LD_LIBRARY_PATH=$HOME/local/lib exe_name
    

关于c++ - "dll-path"在构建 boost 时没有作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667795/

相关文章:

c++ - 将 String.data() 转换为数组

c++ - 哈希函数 - 截断和除法

c++ - dll A 在不同的文件夹位置调用 dll B 函数

java - DLL 可以与其他编程语言一起使用吗?

c++ - 删除 CRT (DLL) 后的编译器问题

c++ - 在具有前向声明的转换运算符中无效使用不完整类型

c++ - 跨线程从 QML 调用 QObject 函数

c++ - 多线程感知模式下的 BOOST 库

boost - 为什么Boost的`bcp smart_ptr dir/`复制6MB的源代码?

c++ - boost中有线程池实现吗?