Boost/python.hpp is not correctly linked when using CMake and Conan

168 Views Asked by At

I am trying to use conan for the very first time to try to retrieve Boost.Python and start a project using it. I have having issues that once I open my visual studio solution I get a red underline for #include <boost/python.hpp> and it will not compile.

I have a following build structure,

src/main.cpp is the following,

#include <iostream>

#include <boost/python.hpp>  

using namespace boost::python;  

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

My CMakeLists.txt is,

cmake_minimum_required(VERSION 3.15)

project(myMath)

find_package(Boost)

add_executable(${PROJECT_NAME}     
    src/main.cpp)

target_link_libraries(${PROJECT_NAME}     
    Boost::python)

Finally my conanfile.txt is,

[requires]
boost/1.83.0

[options]
boost*:without_python=False

[generators]
CMakeDeps
CMakeToolchain

Running conan install . --output-folder=build --build=missing will retrieve Boost correctly and also find Boost::python. I build my solution using,

cmake .. -G "Visual Studio 19 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake.

Looking inside the conan_toolchain.cmake I can see the following line,

list(PREPEND CMAKE_INCLUDE_PATH "C:/Users/Name/.conan2/p/b/boost4b77a2a85257b/p/include"

I can verify that python.hpp exists at C:/Users/Name/.conan2/p/b/boost4b77a2a85257b/p/include/boost/python.hpp.

From this component alone I am unsure why it is not linking.

From all of this I do not understand why my visual studio cannot correctly link to boost/python.hpp once I open my solution. Any help is appreciated. I am also receiving a linking error trying to find ZLIB when following the conan2 tutorial. I have followed all the steps and made sure the variables are all set from my previous question.

The desired folder structure is,

BoostExample
    - Build
        - CMake
            - CMake output is here (soltuion etc)
        - (Build file from conan are put here)
    - Src
        main.cpp
    - CMakeLists.txt
    - conanfile.txt

enter image description here

The exact error when trying to compile from the CLI is,

C:/Users/Michael/.conan2/p/b/boost4b77a2a85257b/p/include\boost/python/detail/wrap_python.hpp(57,11): fatal  error C108
3: Cannot open include file: 'pyconfig.h': No such file or directory [C:\Users\Michael\Desktop\C++\BoostExample\build\C
Make\myMath.vcxproj]

When opening the solution file, a red squiggly is under the include and it will not compile there. I also receive the same problem when following the tutorial using ZLIB, however that seems to compile when using the CLI commands, but still does not link correctly.

1

There are 1 best solutions below

2
Alan Birtles On

Strangely find_package(Boost) doesn't look for python, this is the case even when not using conan, you need to manually bring in python yourself:

cmake_minimum_required(VERSION 3.15)

project(myMath)

find_package(Boost)
find_package(Python3 COMPONENTS Development)

add_executable(${PROJECT_NAME}     
    main.cpp)

target_link_libraries(${PROJECT_NAME}     
    Boost::python Boost::python Python3::Python)