【发布时间】:2012-04-14 10:58:40
【问题描述】:
我正在尝试使用我的 CMakeLists.txt 中的以下代码将 CMake 2.8.6 链接到 boost::program_options
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES (${Boost_INCLUDE_DIR})
ADD_EXECUTABLE (segment segment.cpp)
TARGET_LINK_LIBRARIES (segment ${Boost_LIBRARIES})
find 命令似乎成功了,但将错误的目录传递给了链接器。包实际上在:
`/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 标志似乎丢失了。
运行 CMake 时,它报告它正确找到了包,并且 {$Boost_LIBRARIES} 变量似乎列出了正确的库:
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 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 中的路径时,我得到了错误:
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?我该如何修复它(或解决它)?
【问题讨论】:
-
您能否在 CMakeLists.txt 中的
FIND_PACKAGE和FIND_PACKAGE之后添加SET(Boost_DEBUG 1)和MESSAGE("\${Boost_LIBRARIES} - ${Boost_LIBRARIES}")。然后删除您的 CMakeCache.txt,运行 CMake 并将输出粘贴为您的问题的编辑。 -
@Fraser 似乎找到了正确的库,这里是输出(也包括在上面):
${Boost_LIBRARIES} - optimized;boost_program_options-mt-shared;debug;boost_program_options-mt-shared-debug -
作为一种解决方法,如何手动设置链接路径?