使用提升库和 clion 链接时出错
Linking with error using boost library and clion
当我尝试从 boost 使用文件系统时,我遇到了链接问题。我从教程中复制了代码:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 pathn";
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()
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 在 CLion (MacOS) 中使用 OpenGL 和 GLUT 库时出现链接错误
- 如何在 CLion 中链接库
- CMake 无法在 CLion 中链接 libxml2
- 链接错误时,使用CLION/CMAKE/MSVC 2015编译WIN32应用程序
- libigl,GLEW,GLFW和Eigen在CLion,Windows 10上.链接器错误
- 使用提升库和 clion 链接时出错
- 在CLion中链接Boost.Asio时Static_initialization_and_distruction错误
- CMAKE OS X CLION.如何链接自定义动态库
- 如何在Clion中链接ws2_32
- 在CLion中使用CMake静态链接boost库
- CMake CLion 问题 - 错误:无法确定目标"XYZ"的链接语言
- 如何在CLion中静态链接Qt5与CMake
- OpenCV CLion (Cmake)链接问题- Cmake报告一个奇怪的错误