boost 具有很好的平台独立性, 因此会作为首选的 api 来完成特定的功能.
我在项目中使用了 boost 的 filesystem 功能来获取程序的运行目录.
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
int main()
{
...
std::string exePath = boost::filesystem::initial_path<boost::filesystem::path>().string();
...
return 0;
}
但编译的时候提示如下错误:
In function `__static_initialization_and_destruction_0(int, int)‘:
process_template.cpp:(.text+0x1d09c): undefined reference to `boost::system::generic_category()‘
process_template.cpp:(.text+0x1d0a8): undefined reference to `boost::system::generic_category()‘
process_template.cpp:(.text+0x1d0b4): undefined reference to `boost::system::system_category()‘
CMakeFiles/process.dir/src/process_template.cpp.o: In function `boost::filesystem::initial_path()‘:
process_template.cpp:(.text._ZN5boost10filesystem12initial_pathEv[_ZN5boost10filesystem12initial_pathEv]+0x19): undefined reference to `boost::filesystem::detail::initial_path(boost::system::error_code*)‘
collect2: error: ld returned 1 exit status
很明显是没有将对应的库链接进来. 因此, 需要在 CmakeLists.txt 中添加关于 boost 的链接选项.
cmake_minimum_required(VERSION 2.8)
project( process )
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11")
find_package(Boost REQUIRED COMPONENTS
# regex
filesystem # 我的工程中只使用了 boost 的 filesystem 功能,因此这里只有一个组件
)
if(NOT Boost_FOUND)
message("Not found Boost")
endif()
include_directories(${Boost_INCLUDE_DIRS})
message("${Boost_INCLUDE_DIRS}")
message("${Boost_LIBRARIES}")
add_executable( process src/process_template.cpp )
target_link_libraries(process ${Boost_LIBRARIES})
然后重新 make, 就可以顺利通过了.
[ 50%] Linking CXX executable process
[100%] Built target process
原文地址:https://www.cnblogs.com/magic-428/p/9144492.html
时间: 2024-11-12 21:49:10