Pixelord
Pixelord

Reputation: 641

Switching build configuration from cmake link to proper version of boost library

I am using Cmake to build my C++ project and I am using conan to maintain my boost libraries. My conanfile.txt is very simple,

[requires]
boost/1.71.0@conan/stable

[generators]
cmake

When I install boost from conan, I can use following commands,

conan install ..

Or,

conan install .. -s build_type=Debug

Both command install bost libraries in two different folders in my C:/.conan foler.

In CMakeLists.txt I have following lines to find boost

set(Boost_USE_STATIC_LIBS       ON) 
set(Boost_USE_DEBUG_LIBS        ON)
set(Boost_USE_RELEASE_LIBS      OFF)
set(Boost_USE_MULTITHREADED     ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost REQUIRED COMPONENTS program_options)

However, When I configure cmake,

cmake .. -DBoost_DEBUG=ON

it doesn't find the debug version of boost library. It always shows that it it pointing to release version. So naturally, the following command works,

cmake --build . --config Release

However, following command failed,

cmake --build . --config Debug

as it is still trying to link ot release version.

How can I reconfigure my cmake and conan setup so that I can easily switch between debug and release version? Any suggestion?

Cheers, M

Upvotes: 2

Views: 796

Answers (2)

Alan Birtles
Alan Birtles

Reputation: 36498

If you aren't already I'd recommend using https://github.com/conan-io/cmake-conan it makes life easier in some areas.

Unless you want to be able to build your project without conan there is no real need to use find_package with conan. See https://docs.conan.io/en/latest/integrations/build_system/cmake/cmake_generator.html. You can either use:

conan_basic_setup()

target_link_libraries(<target> ${CONAN_LIBS})

Or:

conan_basic_setup(TARGETS)

target_link_libraries(<target> CONAN_PKG::boost)

If you want to use find_package you need to use the cmake_find_package generator and add the following to your conanfile.txt:

[generators]
cmake_find_package
cmake_paths

Upvotes: 2

jgsogo
jgsogo

Reputation: 726

If you want to consume the libraries with find_package(Boost) you need to generate the corresponding files. Use the cmake_find_package generator (you need different folders for Debug and Release) or the cmake_find_package_multi one (both Debug and Release can live in the same folder). Just add them to your conanfile.txt:

[requires]
boost/1.71.0

[generators]
cmake_find_package_multi

Now, you can call CMake from the command line, but you need to tell where to find the generated FindBoost.cmake files (or the BoostConfig.cmake if you are using the multi generator):

cmake .. -DCMAKE_MODULE_PATH=<path/to/dir/with/findcmake> [...more arguments]

Take into account that, if you are not using the cmake generator and the call to conan_basic_setup, you need to be sure that the configuration used to build the Boost binaries you are getting from Conan matches the one you are using to build your project (otherwise you might get linker errors).

Upvotes: 3

Related Questions