c++ - 为什么我无法在 CMake 中正确使用 boost?

标签 c++ ubuntu boost clion

我会尽力详细解释我遇到的困难。

最近我想在 Ubuntu Mint 18 x64 中使用 boost 库。所以我下载了它的最新版本,即1.62.0。 Sha256sum 没问题。然后我开始用“./bootstrap.sh”和“./b2”编译它。最后用“sudo ./b2 install”复制到“/usr/local”。到目前为止一切顺利。

我现在正在使用 Clion,所以新的工作是在 Cmakelist.txt 中添加库。下面的代码是我添加的。

set(BOOST_ROOT /usr/local)

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost 1.62.0)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(BoostTest main.cpp)
    target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()

首先我要解释的是,我知道在 CMake 中设置特定位置是不好的,但如果没有它,Cmake 就找不到它。所以我就这么做。并且,此时,CMake 没有发布任何错误信息。这是我的 hello_world 演示。

#include <iostream>
#include <boost/thread/testable_mutex.hpp>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

我只包含一个 .hpp 用于测试,没有其他。当我尝试运行它时。我得到了这些。

-- Boost version: 1.62.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vita-nove/ClionProjects/BoostTest/cmake-build-debug
[ 50%] Building CXX object CMakeFiles/BoostTest.dir/main.cpp.o
[100%] Linking CXX executable BoostTest
CMakeFiles/BoostTest.dir/main.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
CMakeFiles/BoostTest.dir/build.make:94: recipe for target 'BoostTest' failed
make[3]: *** [BoostTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/BoostTest.dir/all' failed
make[2]: *** [CMakeFiles/BoostTest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/BoostTest.dir/rule' failed
make[1]: *** [CMakeFiles/BoostTest.dir/rule] Error 2
Makefile:118: recipe for target 'BoostTest' failed
make: *** [BoostTest] Error 2

有线的事情是我确实在外部库中找到了一些 .hpp 文件。它们都位于/include/boost 中。在/usr/local/lib 和/usr/local/include/boost 中,一切都按我的预期显示。

抱歉我的英语不好。经过近一整天的搜索和测试,我无法解决它。我不知道我错过了哪些依赖项或其他东西。不管怎样,感谢您阅读本文,希望有人能提供帮助。

最佳答案

你必须告诉cmake你需要线程库

set(BOOST_ROOT /usr/local)

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost 1.62.0 COMPONENTS thread system) # <-- here
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(BoostTest main.cpp)
    target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()

关于c++ - 为什么我无法在 CMake 中正确使用 boost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125180/

相关文章:

ubuntu - 同一网络中的 docker 容器无法到达同一网络中的其他容器

QT_STATIC_CONST 没有命名类型

c++ - cmake交叉编译boost链接错误

c++ - 使用 boost 读取执行命令的结果(C++ 库)

ubuntu - 当我在 ubuntu 中执行 Heroku 命令 "ERROR: EOF"时出现错误 "heroku login"?

c++ - boost::function 运行时性能

c++ - 将指针传递给参数为引用的函数

C++11 析构函数 = 删除

c++ - 用 cin 检测输入结束

c++ - 生成用于测试的随机结构的通用方法