0

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?

memelord23
  • 84
  • 1
  • 2
  • 11

1 Answers1

0

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
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • I use CMake using the GUI, do I instead plug these into the "Name" and "Value" fields under the search bar? – memelord23 Aug 27 '23 at 09:13
  • I have accepted your answer because it seems to be what I did wrong, but when it works nicely it seems to list a lot of Boost targets such as `Boost::json`, but it never found `Boost::python` which I wanted to for a C++ Python binding. I can't seem to find it on the conan store, would you know where to find it? – memelord23 Aug 27 '23 at 11:02
  • @memelord23 I've updated by answer. See https://stackoverflow.com/questions/29982505/setting-a-cross-compiler-file-using-the-cmake-gui for setting the toolchain in the GUI – Alan Birtles Aug 27 '23 at 11:27
  • Thanks, I was not aware of these options and did not see them anywhere in the conan store configurations. I am new to this. Also it seems I need my compiler to find pyconfig.h in order to work, when I run `>cmake --build . --config Release` I then get a `fatal error C108 3: Cannot open include file: 'pyconfig.h': No such file or directory`. The other stackoverflow answers don't seem to be helpful unfortunately. – memelord23 Aug 27 '23 at 12:22
  • Yep, the new Conan centre website isn't great. You need to look in the recipes really to see what options are available. Have you installed the python development files from your package manager? – Alan Birtles Aug 27 '23 at 14:37
  • Thanks, I tried having a look at the Boost conanfile.txt located at https://github.com/conan-io/conan-center-index/blob/master/recipes/boost/all/conanfile.py, is this where you looked to figure out how to get Python retrieved? I don't see a `without_python` flag. Also sorry which package manager are we referring to now, conan or pip? I have retrieved the python-dev-tools via pip install and have the header files in my interpreter location. Do I have a nicer way of including them using CMake? I am wondering where the tutorial code included the ZLIB header files too o.O – memelord23 Aug 28 '23 at 00:09
  • The options are generated from a list, [here](https://github.com/conan-io/conan-center-index/blob/230ab447b90a136dca3b2bf45875b39c62136103/recipes/boost/all/conanfile.py#L151) is where others is set to default to true. Which os are you using? – Alan Birtles Aug 28 '23 at 05:15
  • Thanks for the link, that's quite a difficulty for a new conan user like myself to be honest. Is this expected for other recipes too? I am on Windows. I am looking into the `conan_toolchain.cmake` and I can see a line that performs `list(PREPEND CMAKE_INCLUDE_PATH...)` I have looked inside this include location and can see `python.hpp` inside `include/boost/python.hpp` so I am a little confused why this is not found when I opened my generated visual studio solution. – memelord23 Aug 28 '23 at 10:35