c++boostcmakecmake-gui

How can I link my CMake project to BOOST::python that using conan?


I have a simple main.cpp that I am trying to utilize boost/python.hpp. All of my current CMake experience so far has used target_include_directories or me having the header or source files. Now I need to utilize Conan to get BOOST however I am unable to link it to my project.

So far I have a main.cpp that looks lile,

#include <iostream>

#include <boost/python.hpp>  

using namespace boost::python;  

int main()
{
    std::cout << "Hello World!";
}

A CMakeLists.txt that looks like,

cmake_minimum_required(VERSION 3.10)

project(myMath)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/build/dependencies/conan")

find_package(Boost 1.72 CONFIG REQUIRED)

target_link_libraries(my_math Boost::python)

add_executable(my_math 
    main.cpp)

The conanfile.txt is,

[requires]
boost/1.83.0

[generators]
CMakeDeps
CMakeToolchain

I have set the CMake GUI,

"Where is the source code" to ".../Desktop/C++/BoostExample" "Where to build the binaries" to ".../Desktop/C++/BoostExample/build/CMakeBuild"

However my CMake keeps giving me an error saying it can't find the BoostConfig.cmake, which I can see inside of .../dependencies/conan which should be found because I set, list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/build/dependencies/conan").

  Could not find a package configuration file provided by "Boost" (requested
  version 1.72) with any of the following names:

    BoostConfig.cmake
    boost-config.cmake

My conan install command was conan install conanfile.txt -of build/dependencies/conan

What are the remaining requirements so that I can find BOOST::python and also, why did Conan not give me any .hpp files?


Solution

  • As you're using the CMakeToolchain and CMakeDeps generators you should be using the Conan toolchain rather than setting CMAKE_MODULE_PATH: https://docs.conan.io/2/tutorial/consuming_packages/build_simple_cmake_project.html which is for the legacy cmake_find_package generator.

    When running cmake use:

    cmake .. -DCMAKE_TOOLCHAIN_FILE=dependencies/conan/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
    

    Note that boost_python isn't enabled by default in the conan recipe so you'll need to explicitly enable it:

    [requires]
    boost/1.83.0
    
    [options]
    boost*:without_python=False
    
    [generators]
    CMakeDeps
    CMakeToolchain