【发布时间】:2017-12-15 20:07:41
【问题描述】:
我正在使用 CMake 编译一个使用 boost::program_options 的 C++ 程序。我已经强制 CMake 通过运行 CMake 来找到正确的头文件和库,如下所示
cmake \
-DBOOST_ROOT:PATHNAME=/path/to/correct/boost/ \
-DBOOST_LIBRARYDIR=/path/to/correct/boost/lib/ \
-DBOOST_INCLUDEDIR=/path/to/correct/boost/include/ \
-DBoost_NO_BOOST_CMAKE=BOOL:ON \
.
CMake 报告它找到了我希望它找到的 boost 库:
-- Found the following Boost libraries:
-- program_options
-- Boost_INCLUDE_DIRS: /path/to/correct/boost/include
-- Boost_LIBRARIES: /path/to/correct/boost/lib/libboost_program_options.so
然后我打电话给make VERBOSE=1 -j。
但是,CMake 会设置 Makefile,而不是传递
-L/path/to/correct/boost/lib/libboost_program_options.so
到gcc,它通过了
-lboost_program_options
然后链接器无法找到正确的库,因为它将../lib64/ 附加到目录:
attempt to open /path/to/correct/boost/lib/../lib64/libboost_program_options.so failed
gcc 然后尝试链接错误的 boost 版本:
attempt to open /usr/lib/../lib64/libboost_program_options.so succeeded
-lboost_program_options (/usr/lib/../lib64/libboost_program_options.so)
这会导致undefined reference 错误,正如人们所期望的错误库版本一样。
如何强制 gcc 使用 CMake 找到的确切库路径 (Boost_LIBRARIES)?有没有办法阻止 CMake 将显式库路径转换为-lboost_program_options?或者,有没有办法让链接器在正确的目录中查找,在路径中使用lib/ 而不是lib64/?
【问题讨论】:
-
什么版本的 CMake 和 Boost?
-
CMake 3.9.4 和 Boost 1.63.0。
-
好的,你的 Cmake 已经够新了。
-
您的
target_link_libraries看起来如何?它应该类似于target_link_libraries(myTarget Boost::program_options) -
@oLen 它看起来像这样:
target_link_libraries(target ${Boost_LIBRARIES})。它前面是find_package(Boost REQUIRED COMPONENTS program_options)和include_directories(${Boost_INCLUDE_DIRS})。我已经验证${Boost_INCLUDE_DIRS}和${Boost_LIBRARIES}是正确的。