使用cmake 2.8.8链接boost库
linking boost libaries using cmake 2.8.8
我正在努力解决我在stackoverflow和其他论坛上搜索过的cmake-baed boost链接问题,但没有成功。
我正在一个已经安装了Boost的集群上编译一个C++程序套件(除了Boost之外,还取决于不同的库((我知道完整的路径(。在编译时,boost与cmake出现了奇怪的链接错误,我想首先编译一个非常简单的boost-cmake示例来解决这个问题。
代码如下所示。
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/option.hpp>
using namespace std;
#include <iostream>
namespace po = boost::program_options;
int main(int argc, char** argv) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
;
return 0;
}
我正在使用以下CMakeLists.txt文件进行构建。
cmake_minimum_required(VERSION 2.8)
set(Boost_INCLUDE_DIR /software/apps/boost/1.55.0/build06/include)
set(Boost_LIBRARY_DIR /software/apps/boost/1.55.0/build06/lib)
find_package(Boost COMPONENTS program_options REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
message("Boost include dir is ${Boost_INCLUDE_DIR}")
message("Boost library dir is ${Boost_LIBRARY_DIR}")
message("Boost libraries are at ${Boost_LIBRARIES}")
add_executable(main main.cpp)
target_link_libraries( main ${Boost_LIBRARIES} )
当我使用cmake编译时,我得到以下输出
x_ikrul@ikramu build]$ emacs ../CMakeLists.txt
Display localhost:39.0 unavailable, simulating -nw
[x_ikrul@triolith1 build]$ rm -rf *
[x_ikrul@triolith1 build]$ cmake ..
-- The C compiler identification is GNU 4.7.2
-- The CXX compiler identification is GNU 4.7.2
-- Check for working C compiler: /software/apps/comp_wrapper/gnu/gcc
-- Check for working C compiler: /software/apps/comp_wrapper/gnu/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /software/apps/comp_wrapper/gnu/c++
-- Check for working CXX compiler: /software/apps/comp_wrapper/gnu/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
Boost found.
Found Boost components:
program_options
Boost include dir is /usr/include
Boost library dir is /software/apps/boost/1.55.0/build06/lib
Boost libraries are at optimized;boost_program_options-mt-shared;debug;boost_program_options-mt-shared-debug
-- Configuring done
-- Generating done
-- Build files have been written to: /home/x_ikrul/Downloads/boost_example/build
当我"制作"下面给出的代码时,问题就来了。
x_ikrul@ikramu build]$make
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
make[2]: *** No rule to make target `/usr/lib64/lib64/libboost_program_options-mt.so.5', needed by `main'. Stop.
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
正如您所看到的,尽管CMake将boost的lib目录显示为/software/apps/boost/1.55.0/build06/lib,但奇怪的是,在链接时,它指的是另一个不存在的目录。
有人遇到过这样的错误吗?集群运行的是CentOS 6.5版,boost(我所指的位置(是1.55.0,我使用的cmake是2.8.8。
您没有正确设置提示路径。
来自FindBoost模块文档:
该模块从变量中读取有关搜索位置的提示:
BOOST_ROOT
-首选安装前缀(或BOOSTROT(
BOOST_INCLUDEDIR
-首选包含目录,例如<prefix>/include
BOOST_LIBRARYDIR
-首选库目录,例如<prefix>/lib
Boost_NO_SYSTEM_PATHS
-设置为ON
可禁用在非由这些提示变量指定。默认值为OFF
。并将搜索结果持久保存在CMake缓存条目中:
Boost_INCLUDE_DIR
-包含Boost标头的目录
Boost_LIBRARY_DIR
-包含Boost库的目录
所以,你的线路:
set(Boost_INCLUDE_DIR /software/apps/boost/1.55.0/build06/include)
set(Boost_LIBRARY_DIR /software/apps/boost/1.55.0/build06/lib)
实际上并没有设置提示变量——您希望设置BOOST_INCLUDEDIR
和BOOST_LIBRARYDIR
;也可能是CCD_ 13。
最好不要在CMakeLists.txt中硬编码,因为它不可移植。而是在调用CMake:时在命令行上传递这些
cmake . -DBOOST_INCLUDEDIR=/software/apps/boost/1.55.0/build06/include -DBOOST_LIBRARYDIR=/software/apps/boost/1.55.0/build06/lib -DBoost_NO_SYSTEM_PATHS=ON
如果将Boost_DEBUG
设置为ON
,您还可以更好地了解发生了什么。
顺便说一句,您不应该需要link_directories
调用,因为Boost库的完整路径是在target_link_libraries
调用中传递的。
这个stackerflow答案建议在cmake命令行上添加-DBoost_NO_BOOST_CMAKE=ON
。
这似乎是由于某些CMake版本与某些Boost版本不兼容。
我同意用户Fraser的说法。这些线路没有意义
set(Boost_INCLUDE_DIR /software/apps/boost/1.55.0/build06/include)
set(Boost_LIBRARY_DIR /software/apps/boost/1.55.0/build06/lib)
因为CMake使用这些变量来存储Boost搜索的结果。在他的回答中阅读更多关于它的信息。
- 基于boost的程序的静态链接——zlib问题
- 在Cygwin上链接Boost库
- CMAKE中无法链接Boost
- 使用cmake错误链接boost日志
- 程序使用boost::文件系统链接clang,但不使用gcc
- 编译时出现Boost.python链接错误
- 链接问题 boost::p ython::numpy.
- 将 boost 与 Visual Studio 和 vcpkg 链接时出错
- 如何在 Oracle Linux 上的 Eclipse 中编译和链接 boost 库
- 使用 BOOST property_tree / iostreams / 文件系统 / foreach - 结果出现链接
- 链接 boost 库时"Error while loading shared libraries"引发的,除了我无法使用 root 访问权限来修复它
- boost::存在符号链接不取消引用
- Android Studio 链接器命令在包含 Boost 库时失败
- 如何链接到与 Linux 中不同版本的 boost 链接的共享库
- boost/filesystem.hpp未通过Homebrew安装的OSX Boost链接
- Boost:链接由bjam命名的库时出现致命错误LNK1104
- Boost链接错误-单个未定义的引用
- 使用仅头文件库时,Boost链接器错误
- dev - c++中Boost链接/构建的问题
- 我应该使用哪些编译选项来使用cl.exe与Boost链接