使用cmake 2.8.8链接boost库

linking boost libaries using cmake 2.8.8

本文关键字:boost 链接 使用 cmake      更新时间:2023-10-16

我正在努力解决我在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_INCLUDEDIRBOOST_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搜索的结果。在他的回答中阅读更多关于它的信息。