Answers:
将其放入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()
显然,您需要将所需的库放在我放置的位置*boost libraries here*
。例如,如果您使用filesystem
and regex
库,则应编写:
find_package(Boost 1.45.0 COMPONENTS filesystem regex)
lexical_cast
。因此,您只需要find_package
and include_directories
命令。
*boost libraries here*
意思
FIND_PACKAGE(Boost REQUIRED COMPONENTS system)
,如果你不知道提振使用精确的版本
您可以使用find_package搜索可用的增强库。它将搜索推迟到FindBoost.cmake,默认与CMake一起安装。
找到Boost后,该find_package()
调用将填充许多变量(请参阅FindBoost.cmake的参考)。其中有BOOST_INCLUDE_DIRS
Boost_LIBRARIES和Boost_XXX_LIBRARY变量,用特定的Boost库替换了XXX。您可以使用它们来指定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} )
一些一般性提示:
On
:Boost_USE_STATIC_LIBS
,Boost_USE_MULTITHREADED
,Boost_USE_STATIC_RUNTIME
add_definitions( -DBOOST_ALL_NO_LIB )
add_definitions( -DBOOST_ALL_DYN_LINK )
用导入的目标修改@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()
请注意,不再需要手动指定include目录,因为已经通过导入的目标Boost::filesystem
和进行了处理Boost::regex
。
regex
并filesystem
可以替换为您需要的任何Boost库。
可能对某些人有帮助。我有一个调皮的错误: 对符号'_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)
现在,在文件末尾,我将“ 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()
我同意答案1和2。但是,我更愿意分别指定每个库。这使得大型项目中的依赖关系更加清晰。但是,存在混淆(区分大小写)变量名称的危险。在那种情况下,没有直接的cmake错误,但是稍后会出现一些未定义的引用链接程序问题,这可能需要一些时间才能解决。因此,我使用以下cmake函数:
function(VerifyVarDefined)
foreach(lib ${ARGV})
if(DEFINED ${lib})
else(DEFINED ${lib})
message(SEND_ERROR "Variable ${lib} is not defined")
endif(DEFINED ${lib})
endforeach()
endfunction(VerifyVarDefined)
对于上面提到的示例,它看起来像:
VerifyVarDefined(Boost_PROGRAM_OPTIONS_LIBRARY Boost_REGEX_LIBRARY)
target_link_libraries( run ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY} )
如果我写了“ BOOST_PROGRAM_OPTIONS_LIBRARY”,那么cmake会触发一个错误,而链接器会在稍后触发一个错误。
尝试说Boost文档:
set(Boost_USE_STATIC_LIBS ON) # only find static libs
set(Boost_USE_DEBUG_LIBS OFF) # ignore debug libs and
set(Boost_USE_RELEASE_LIBS ON) # only find release libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.66.0 COMPONENTS date_time filesystem system ...)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo foo.cc)
target_link_libraries(foo ${Boost_LIBRARIES})
endif()
不要忘记将foo替换为您的项目名称,并将组件替换为您的项目名称!