简体   繁体   中英

Link the static versions of the Boost libraries using CMake

I've got both the static and the dynamic versions of the boost libraries in /usr/lib. Now I'd like CMake to prefer the static versions during the linkage of my executable. What can I do?

In your CMakeLists.txt file:

set(Boost_USE_STATIC_LIBS   ON)
find_package(Boost REQUIRED ...)

Where I have ... , you optionally put the names of the libraries you want to use, and then target_link_libraries(targetname ${Boost_LIBRARIES}) later below. If you have a fairly recent distribution of CMake, it should work exactly as advertised. I do it exactly this way in my own projects.

Here is a full example of CMAKEFILE,For example, include boost program options

cmake_minimum_required(VERSION 3.15)
project(your_project)
set(Boost_USE_STATIC_LIBS   ON)
find_package(Boost 1.70 COMPONENTS program_options REQUIRED)
set(CMAKE_CXX_STANDARD 14)   
add_executable(your_project main.cpp)
target_link_libraries(rconpp Boost::program_options)

references:

cmake documents about BOOST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM