Boost库使用记录

本文档介绍了在使用CMake构建项目并尝试链接Boost库时遇到的错误,错误涉及到CMake的PolicyCMP0057。解决方法是在CMakeLists.txt中设置CMAKE_MINIMUM_REQUIRED版本,并正确调用find_package。示例代码和详细步骤帮助开发者理解和解决此类问题。

问题

Q: 通过cmake使用boost库的时候,报了错误如下:
  CMake Warning (dev) at /usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake:240 (if):
  Policy CMP0057 is not set: Support new IN_LIST if() operator.  Run "cmake
  --help-policy CMP0057" for policy details.  Use the cmake_policy command to
  set the policy and suppress this warning.

  	IN_LIST will be interpreted as an operator when the policy is set to NEW.
  Since the policy is not set the OLD behavior will be used.
	Call Stack (most recent call first):
  /usr/share/cmake-3.5/Modules/FindBoost.cmake:245 (find_package)
  CMakeLists.txt:10 (find_package)
	This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Error at /usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake:240 (if):
 if given arguments:
 "ALL" "IN_LIST" "Boost_FIND_COMPONENTS"
 Unknown arguments specified

A: 由于boost最新的库需要更高的cmake 支持, 添加以下设定到CMakeLists.txt中

CMAKE_MINMUM_REQUIRED(VERSION 3.5)

要点

  • 在每个boost库的文件下,都有相应的使用例程:
    $ cd boost_1_74_0/libs/date_time/example 
    $ ls
    gregorian  Jamfile  local_time  posix_time  tutorial
    
  • Boost在CMakeLists.txt文件中的相关使用例程在FindBoost.cmake文件中有举例:
    如果只调用boost的头文件的时候, 不用指定组件, 如果需要调用某个库的时候, 需要在find_package中指定组件, 该组件其实就是被调用的库文件的别名(Boost libraries by their canonical name)。
     164 # Example to find Boost headers only::
     165 #
     166 #   find_package(Boost 1.36.0)
     167 #   if(Boost_FOUND)
     168 #     include_directories(${Boost_INCLUDE_DIRS})
     169 #     add_executable(foo foo.cc)
     170 #   endif()
     171 #
     172 # Example to find Boost libraries and use imported targets::
     173 #
     174 #   find_package(Boost 1.56 REQUIRED COMPONENTS
     175 #                date_time filesystem iostreams)
     176 #   add_executable(foo foo.cc)
     177 #   target_link_libraries(foo Boost::date_time Boost::filesystem
     178 #                             Boost::iostreams)
     179 #
     180 # Example to find Boost headers and some *static* libraries::
     181 #
     182 #   set(Boost_USE_STATIC_LIBS        ON) # only find static libs
     183 #   set(Boost_USE_MULTITHREADED      ON)
     184 #   set(Boost_USE_STATIC_RUNTIME    OFF)
     185 #   find_package(Boost 1.36.0 COMPONENTS date_time filesystem system ...)
     186 #   if(Boost_FOUND)
     187 #     include_directories(${Boost_INCLUDE_DIRS})
     188 #     add_executable(foo foo.cc)
     189 #     target_link_libraries(foo ${Boost_LIBRARIES})
     190 #   endif()
     191 #
     192 # Boost CMake
    
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值