我想在一个自定义目录下安装boost。我安装了。
cd boost_dir
./bootstrap.sh --prefix=/custom_dir
./b2
现在我想做的是告诉CMake指向那个库. 为了达到这个目的,我添加了:
set(BOOST_ROOT "/custom_dir")
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(myProject ${Boost_LIBRARIES})
然后CMake显然在那个目录下找到了boost。
cmake .
-- Found Boost: /custom_dir (found suitable version "1.66.0", minimum required is "1.66") found components: program_options -- Configuring done
-- Generating done
-- Build files have been written to: /my_project
然而,在编译时,我还是得到:
undefined reference to `boost::program_options::options_description::options_description(std::string const&, unsigned int, unsigned int)'
有几个答案指出,boost一定是用和我使用的程序一样的编译器编译的。然而,我不确定我上面做的步骤是真的编译了boost还是仅仅复制了未压缩的lib?
另外,是使用系统默认usrbin中的路径解析g++还是什么?原因是我没有root权限,在自定义的dir里也安装了更新的gcc。我已经把我的PATH指向了更新的g++。
所以事实证明,GCC版本是正确的,但只有当我用静态链接编译Boost并告诉CMake静态链接时,它才会工作。我试验了动态链接(既编译Boost,又为我的目标应用设置CMake),但由于某些原因无法工作。
只要确保用相同的ABI选项编译就可以了。