gpt4 book ai didi

c++ - 无法通过 CMake 链接 Boost 1.63.0

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:19 25 4
gpt4 key购买 nike

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

最佳答案

替换

find_package(BOOST 1.6.0 REQUIRED)

find_package(Boost 1.63.0 REQUIRED filesystem system)

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

关于c++ - 无法通过 CMake 链接 Boost 1.63.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43812866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com