无法通过CMake链接Boost 1.63.0

时间:2017-05-05 19:56:34

标签: c++ boost cmake

我想要做的就是从标题中猜到通过CMake链接Boost库(我正在与CLion合作编写跨平台代码,所以我没有其他机会)。我确信我正确地构建了一切,因为当我在Visual Studio中使用它时它完全没有问题。 这是我的CMake代码:

cmake_minimum_required(VERSION 3.7)
project(BoostHello)

set(BOOST_ROOT C:/boost_1.63.0)
find_package(BOOST 1.6.0 REQUIRED)
include_directories( ${Boost_INCLUDE_DIR} )
set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp)
add_executable(BoostHello ${SOURCE_FILES})
target_link_libraries( BoostHello ${Boost_LIBRARIES} )

这是我的编译错误:

"C:\Program Files (x86)\JetBrains\CLion 2017.1\bin\cmake\bin\cmake.exe" --build C:\Users\Admin\CLionProjects\BoostHello\cmake-build-debug --target all --     -j 8
Scanning dependencies of target BoostHello
[ 50%] Building CXX object CMakeFiles/BoostHello.dir/main.cpp.obj
[100%] Linking CXX executable BoostHello.exe
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:6: undefined reference to     `boost::filesystem::path::root_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:7: undefined reference to     `boost::filesystem::path::relative_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:8: undefined reference to `boost::filesystem::path::filename() const'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function     `_static_initialization_and_destruction_0':
C:/boost_1.63.0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function     `ZN5boost10filesystem11path_traits7convertEPKwS3_RNSt7__cxx1112basic_stringIcSt1    1char_traitsIcESaIcEEE':
C:/boost_1.63.0/boost/filesystem/path.hpp:989: undefined reference to     `boost::filesystem::path::codecvt()'
C:/boost_1.63.0/boost/filesystem/path.hpp:989: undefined reference to `boost::filesystem::path_traits::convert(wchar_t const*, wchar_t const*,     std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>     >&, std::codecvt<wchar_t, char, int> const&)'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\BoostHello.dir\build.make:96: recipe for target 'BoostHello.exe'     failed
mingw32-make.exe[2]: *** [BoostHello.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/BoostHello.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/BoostHello.dir/all'     failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed

最终这里是我试图编译的代码:

#include <iostream>
#include <boost/filesystem.hpp>

int main(int argc, char** argv) {
    boost::filesystem::path myPath =     {L"C:/Users/Admin/ClionProjects/BoostHello"};
    std::cout << "Root:\t"      << myPath.root_path()            << std::endl;
    std::cout << "Relative:\t"  << myPath.relative_path()        << std::endl;
    std::cout << "Filename:\t"  << myPath.filename()             << std::endl;
    return 0;
}

我做错了什么? 我正在使用MinGW进行编译。 提前谢谢!

1 个答案:

答案 0 :(得分:2)

替换

find_package(BOOST 1.6.0 REQUIRED)

通过

find_package(Boost 1.63.0 REQUIRED filesystem system)

否则CMake不知道要链接哪个升级库。