Lai*_*ura 160
将其放入您的CMakeLists.txt文件中(如果需要,可以将任何选项从OFF更改为ON):
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS *boost libraries here*)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(progname file1.cxx file2.cxx)
target_link_libraries(progname ${Boost_LIBRARIES})
endif()
Run Code Online (Sandbox Code Playgroud)
显然你需要把你想要的库放在我放的地方*boost libraries here*.例如,如果您正在使用您编写的库filesystem和regex库:
find_package(Boost 1.45.0 COMPONENTS filesystem regex)
Run Code Online (Sandbox Code Playgroud)
And*_*dré 74
您可以使用find_package搜索可用的boost库.它推迟搜索Boost到FindBoost.cmake,它是默认安装的CMake.
在找到Boost后,find_package()调用将填充许多变量(检查FindBoost.cmake的参考).其中包括BOOST_INCLUDE_DIRSBoost_LIBRARIES和Boost_XXX_LIBRARY变量,其中XXX替换为特定的Boost库.您可以使用它们来指定include_directories和target_link_libraries.
例如,假设您需要boost :: program_options和boost :: regex,您可以执行以下操作:
find_package( Boost REQUIRED COMPONENTS program_options regex )
include_directories( ${Boost_INCLUDE_DIRS} )
add_executable( run main.cpp ) # Example application based on main.cpp
# Alternatively you could use ${Boost_LIBRARIES} here.
target_link_libraries( run ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY} )
Run Code Online (Sandbox Code Playgroud)
一些一般提示:
On:Boost_USE_STATIC_LIBS,Boost_USE_MULTITHREADED,Boost_USE_STATIC_RUNTIMEadd_definitions( -DBOOST_ALL_NO_LIB )add_definitions( -DBOOST_ALL_DYN_LINK )oLe*_*Len 21
使用导入的目标调整@LainIwakura对现代CMake语法的答案,这将是:
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS filesystem regex)
if(Boost_FOUND)
add_executable(progname file1.cxx file2.cxx)
target_link_libraries(progname Boost::filesystem Boost::regex)
endif()
Run Code Online (Sandbox Code Playgroud)
请注意,不再需要手动指定包含目录,因为它已经通过导入的目标Boost::filesystem进行处理Boost::regex.
regex并且filesystem可以由您需要的任何boost库替换.
小智 7
愿这对某些人有所帮助.我有一个顽皮的错误: 对符号'_ZN5boost6system15system_categoryEv'的未定义引用//usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0:错误添加符号:命令行中缺少DSO cmakeList.txt存在一些问题不知何故,我错过了明确包含"系统"和"文件系统"库.所以,我在CMakeLists.txt中写了这些行
在创建项目的可执行文件之前,这些行是在开头编写的,因为在此阶段我们不需要将boost库链接到项目可执行文件.
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_NO_SYSTEM_PATHS TRUE)
if (Boost_NO_SYSTEM_PATHS)
set(BOOST_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../3p/boost")
set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}/include")
set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/lib")
endif (Boost_NO_SYSTEM_PATHS)
find_package(Boost COMPONENTS regex date_time system filesystem thread graph program_options)
find_package(Boost REQUIRED regex date_time system filesystem thread graph program_options)
find_package(Boost COMPONENTS program_options REQUIRED)
Run Code Online (Sandbox Code Playgroud)
现在在文件的末尾,我通过将"KeyPointEvaluation"视为我的项目可执行文件来编写这些行.
if(Boost_FOUND)
include_directories(${BOOST_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
add_definitions(${Boost_DEFINITIONS})
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(KeyPointEvaluation ${Boost_LIBRARIES})
target_link_libraries( KeyPointEvaluation ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_SYSTEM_LIBRARY})
endif()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
125389 次 |
| 最近记录: |