问题描述
我正在尝试使用 CMakeLists.txt 中的以下代码将 CMake 2.8.6 链接到 boost::program_options
I'm trying to have CMake 2.8.6 link to boost::program_options using the following code in my CMakeLists.txt
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES (${Boost_INCLUDE_DIR})
ADD_EXECUTABLE (segment segment.cpp)
TARGET_LINK_LIBRARIES (segment ${Boost_LIBRARIES})
find 命令似乎成功了,但将错误的目录传递给了链接器.包裹实际上在:
The find command seems to succeed but passes the wrong directory to the linker. The package is actually in:
`/usr/lib64/libboost_program_options-mt.so.5`
但是CMakeFiles/segment.dir/link.txt
列出了以下内容:
/cm/shared/apps/gcc/4.4.6/bin/c++ CMakeFiles/segment.dir/segment.cpp.o -o segment -rdynamic /usr/lib64/lib64/libboost_program_options-mt.so.5 -lpthread -lrt -Wl,-rpath,/usr/lib64/lib64
注意路径中额外的 lib64
.此外,路径前面的 -l 标志似乎丢失了.
Note the extra lib64
in the path. Also, the -l flag in front of the path seems to be missing.
当运行 CMake 时,它报告它正确地找到了包,并且 {$Boost_LIBRARIES}
变量似乎列出了正确的库:
When running CMake it reports that it correctly finds the package, and the {$Boost_LIBRARIES}
variable seems to list the correct libs:
Boost found.
Found Boost components:
program_options
${Boost_LIBRARIES} - optimized;boost_program_options-mt-shared;debug;boost_program_options-mt-shared-debug
生成的 CMakeCache.txt 文件以:
The generated CMakeCache.txt file starts with:
//The directory containing a CMake configuration file for Boost.
Boost_DIR:PATH=/usr/lib64/boost
//Boost include directory
Boost_INCLUDE_DIR:FILEPATH=/usr/include
这似乎是正确的.但是当运行 make 它使用上面link.txt中的路径时,我得到了错误:
Which seems to be correct. But when running make it uses the path in link.txt above and I get the error:
make[2]: *** No rule to make target `/usr/lib64/lib64/libboost_program_options-mt.so.5', needed by `segment'. Stop.
make[1]: *** [CMakeFiles/segment.dir/all] Error 2
make: *** [all] Error 2
什么可能导致将子目录额外注入到路径中?什么可能导致以这种方式生成link.txt?我该如何解决(或解决它)?
What might cause this extra injection of a subdir into the path? What might cause link.txt to be generated in this way? And how do I fix it (or work around it)?
推荐答案
当使用一些旧版本的 boost 和 cmake-2.8.6-rc2
或更高版本时会出现这个问题,其中 boost 包发现代码已更改.
This problem occurs when using some older versions of boost with cmake-2.8.6-rc2
or later, where the boost package finding code was changed.
该问题可以通过在 cmake 命令行中指定 -DBoost_NO_BOOST_CMAKE=ON
来解决.
The problem can be worked around by specifying -DBoost_NO_BOOST_CMAKE=ON
on the cmake command line.
引入这个问题的实际 commit 是 7da796d1fdd7cca07df733d010cd343f6f8787a9,可以是 在此处查看.
The actual commit where this problem is introduced is 7da796d1fdd7cca07df733d010cd343f6f8787a9, and can be viewed here.
这篇关于CMake FIND_PACKAGE 成功但返回错误的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!