Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I optionally link against static or dynamic boost library using CMake?

Tags:

c++

boost

cmake

I have a CMake project that I sometimes want to compile against the static boost libraries, but I want to also make it easy to just use the dynamic libraries from the cmake GUI. In my top level CMakeLists.txt I have this:

option(USE_STATIC_BOOST "Build with static BOOST libraries instead of dynamic" NO)

Then in a different file, I have the following logic set up:

if(USE_STATIC_BOOST)
   unset(Boost_LIBRARIES)
   message(WARNING "Linking against boost static libraries")
   set(Boost_USE_STATIC_LIBS ON)
   set(Boost_USE_MULTITHREADED ON)
   find_package(Boost REQUIRED COMPONENTS thread program_options system)
else(USE_STATIC_BOOST)
   unset(Boost_LIBRARIES)
   message(WARNING "Linking against boost dynamic libraries")
   set(Boost_USE_STATIC_LIBS OFF)
   set(Boost_USE_MULTITHREADED ON)
   find_package(Boost REQUIRED COMPONENTS thread program_options system)
endif(USE_STATIC_BOOST)

This seems to work fine if I start from scratch and use:

cmake ../.. -DUSE_STATIC_BOOST=YES

However, when I use

ccmake ../..

I cannot get it to use the static libraries no matter what I do. CMake appears to load the dynamic option into cache on startup and changing USE_STATIC_BOOST doesn't switch it. I even tried to unset(Boost_LIBRARIES) to explicitly clear it out. Is there a way to do what I'm trying to do?

Using x86_64 Linux and g++ to compile. Thanks in advance!

like image 626
KyleL Avatar asked Jul 27 '12 01:07

KyleL


People also ask

How does CMake find Boost?

You can use find_package to search for available boost libraries. It defers searching for Boost to FindBoost. cmake, which is default installed with CMake. Upon finding Boost, the find_package() call will have filled many variables (check the reference for FindBoost.

Can I build Boost with CMake?

One of my current projects relies on Boost. Python, which requires a more recent version of Boost (1.64) than the one (1.54) provided by my Linux distribution (Ubuntu 14.04).

How do I install CMake Boost?

Install Boost (Windows)Download the source code from http://www.boost.org/users/download/. Extract in a Boost folder located at C:\ or C:\Program files so that CMake find-modules can detect it. Invoke the command line and navigate to the extracted folder (e.g. cd C:\Boost\boost_1_63_0 ).


1 Answers

To force the FindBoost CMake module to search for the desired libraries again, you have to clear the cache variables Boost_INCLUDE_DIR and Boost_LIBRARY_DIRS, i.e.:

set(Boost_USE_STATIC_LIBS ${USE_STATIC_BOOST})
set(Boost_USE_MULTITHREADED ON)
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
find_package(Boost REQUIRED COMPONENTS thread program_options system)
if(USE_STATIC_BOOST)
   message(STATUS "Linking against boost static libraries")
else()
   message(STATUS "Linking against boost dynamic libraries")
endif()

Note that the argument CACHE is necessary to make the unset command clear the variables in the cache.

Also note that once the option variable USE_STATIC_BOOST has been cached, you need to explicitly set the variable from the command line or edit the value in the cache file to make CMake notice the change:

cmake ../.. -DUSE_STATIC_BOOST=NO 
like image 147
sakra Avatar answered Sep 21 '22 06:09

sakra