gpt4 book ai didi

c++ - 使用 boost 库和 clion 链接时出现错误

转载 作者:行者123 更新时间:2023-12-03 12:49:47 24 4
gpt4 key购买 nike

当我尝试使用 boost 中的文件系统时,我遇到链接问题。我从教程中复制了代码:

#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}

也许它应该有效,但我有这样的错误:

Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::file_size(boost::filesystem::path const&) in main.cpp.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init.2 in main.cpp.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.cpp.o
___cxx_global_var_init.1 in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2

我的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})

find_package(Boost)
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
endif()

我可以改进什么来编译此代码?我在 OSX 上使用 CLion 编辑器。

最佳答案

在 CMakeList.txt 中更改此行:

find_package(Boost)

find_package(Boost COMPONENTS system filesystem REQUIRED)

并且不要忘记链接到您的目标:

target_link_libraries(untitled 
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)

所以你的 CMakeList.txt 将是:

cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})

find_package(Boost COMPONENTS system filesystem REQUIRED)
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(untitled
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
endif()

关于c++ - 使用 boost 库和 clion 链接时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43215591/

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