【问题标题】:Can't link Boost 1.63.0 through CMake无法通过 CMake 链接 Boost 1.63.0
【发布时间】:2017-05-05 19:56:33
【问题描述】:

我正在尝试做的是正如您可以从标题中猜到的那样,通过 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 进行编译。 提前致谢!

【问题讨论】:

    标签: c++ boost cmake


    【解决方案1】:

    替换

    find_package(BOOST 1.6.0 REQUIRED)
    

    find_package(Boost 1.63.0 REQUIRED filesystem system)
    

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

    【讨论】:

    • 你知道了,但我必须添加这个:find_package(Boost 1.63.0 REQUIRED filesystem system)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多