Busque la biblioteca Boost al compilar con CMake en Linux

Busque la biblioteca Boost al compilar con CMake en Linux

 

find_package

find_packagePuede encontrar el archivo de encabezado y el archivo de biblioteca requerido o un archivo de configuración de empaquetado de CMake llamando ,

find_package(Boost
  [version] [EXACT]      # 可选项,最小版本或者确切所需版本
  [REQUIRED]             # 可选项,如果找不到所需库,报错
  [COMPONENTS <libs>...] # 所需的库名称,比如说. "date_time" 代表 "libboost_date_time"
  )     

Después de ejecutar, puede obtener muchas variables, algunas de las principales se enumeran a continuación

Boost_FOUND            - 如果找到了所需的库就设为true
Boost_INCLUDE_DIRS     - Boost头文件搜索路径
Boost_LIBRARY_DIRS     - Boost库的链接路径
Boost_LIBRARIES        - Boost库名,用于链接到目标程序
Boost_VERSION          - 从boost/version.hpp文件获取的版本号
Boost_LIB_VERSION      - 某个库的版本

Puede ayudar a encontrar la biblioteca boost configurando algunas variables antes de buscar el paquete

BOOST_ROOT             - 首选的Boost安装路径
BOOST_INCLUDEDIR       - 首选的头文件搜索路径 e.g. <prefix>/include
BOOST_LIBRARYDIR       - 首选的库文件搜索路径 e.g. <prefix>/lib
Boost_NO_SYSTEM_PATHS  - 默认是OFF. 如果开启了,则不会搜索用户指定路径之外的路径

Programa de muestra

Si el programa de destino foo necesita vincular la expresión regular de la biblioteca Boost y el sistema, escriba el siguiente archivo CMakeLists,

# CMakeLists.txt
project(tutorial-0)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 14)

set(BOOST_ROOT /usr/local/install/boost_1_61_0)

find_package(Boost COMPONENTS regex system REQUIRED)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    
    MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
    MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
    MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")

    add_executable(foo foo.cpp)
    target_link_libraries (foo ${Boost_LIBRARIES})
endif()
  • Establezca la ruta de búsqueda preferida configurando BOOST_ROOT
  • Imprime los resultados de la búsqueda a través de la función MENSAJE
-- Boost_INCLUDE_DIRS = /usr/local/install/boost_1_61_0/include.
-- Boost_LIBRARIES = /usr/local/install/boost_1_61_0/lib/libboost_regex.so;/usr/local/install/boost_1_61_0/lib/libboost_system.so.
-- Boost_LIB_VERSION = 1_61.
  • El proceso de compilación necesita usar la ruta de búsqueda se almacena en la ruta del archivo de biblioteca de la variable Boost_INCLUDE_DIRS , el enlace requerido se almacena en la variable Boost_LIBRARIES en

Reimpreso: https://www.jianshu.com/p/1827cd86d576

Supongo que te gusta

Origin blog.csdn.net/hyl999/article/details/108084247
Recomendado
Clasificación