CMake: Proper way to install boost component libraries with project

Viewed 3116

I'm building and linking a project against Boost shared libraries. Now: How do I put these libraries into the target application's install directory? I need this, because the application is going to be installed inside a container without the proper Boost version.

What I tried in multiple variants is:

install(PROGRAMS ${Boost_LIBRARIES} DESTINATION install/lib)

But neither this nor any other variant I could think of gives me the actual shared library file names.

2 Answers

I found an answer here: https://cmake.org/pipermail/cmake/2010-March/036053.html

I think you should be able to use get_target_properties(LOCATION) on the imported targets, and then install(FILES ... ) with the locations you get.

...so this might work in your case:

foreach(lib_target ${Boost_LIBRARIES})
    get_target_property(lib_location ${lib_target} LOCATION)
    install(FILES ${lib_location} DESTINATION lib)  
endforeach()

This is an abridged summary of the answer to this question. For a full transcript, please see the revision history

Accepted Answer

The line of code indicated in the question should work, but that depends on how CMake locates the Boost framework.

Only specific components of the Boost framework have installable runtime libraries. If you need any of these, you must list these components in the FindBoost command of your CMakeLists.txt file.

I've tested this on Ubuntu 18.04 with CMake 3.10 and Boost 1.65 using the minimal CMakeLists.txt below:

cmake_minimum_required(VERSION 3.10)
find_package( 
 Boost 1.65 REQUIRED 
 COMPONENTS  filesystem system 
)
install(PROGRAMS ${Boost_LIBRARIES} DESTINATION ~/install/lib)

Following the standard convention of ...:

mkdir build
cd build
ccmake ..
cmake .
make
make install

... I successfully see libboost_filesystem.so and libboost_system.so installed in the specified location below the user's home folder.

Related