Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Python - undefined reference to `_Py_NoneStruct'

Tags:

c++

boost

I'm getting the following error when trying to use Boost Python:

/usr/include/boost/python/object_core.hpp:400: undefined reference to `_Py_NoneStruct'

Installed on Ubuntu 18.04 using:

sudo apt-get install libboost-all-dev

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(boostpy)

set(CMAKE_CXX_STANDARD 14)
include_directories(/usr/include/python3.6)
find_package(Boost 1.65.1 COMPONENTS python3-py36 REQUIRED)
if(Boost_FOUND)
    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")
    include_directories(${Boost_INCLUDE_DIRS})
endif()

add_executable(boostpy main.cpp)
if(Boost_FOUND)
    target_link_libraries(boostpy ${Boost_LIBRARIES})
endif()

main.cpp

#include <iostream>
#include <boost/python.hpp>
#include <Python.h>

int main()
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Results in:

-- Found Boost: /usr/include (found suitable version "1.65.1", minimum required is "1.65.1") found components: python3-py36 
-- Boost_INCLUDE_DIRS: /usr/include
-- Boost_LIBRARIES: /usr/lib/x86_64-linux-gnu/libboost_python3-py36.so
-- Boost_VERSION: 106501
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tb/CLionProjects/boostpy/cmake-build-debug

[ 50%] Linking CXX executable boostpy
CMakeFiles/boostpy.dir/main.cpp.o: In function `boost::python::api::object::object()':
/usr/include/boost/python/object_core.hpp:400: undefined reference to `_Py_NoneStruct'
collect2: error: ld returned 1 exit status
CMakeFiles/boostpy.dir/build.make:95: recipe for target 'boostpy' failed
make[3]: *** [boostpy] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/boostpy.dir/all' failed
make[2]: *** [CMakeFiles/boostpy.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/boostpy.dir/rule' failed
make[1]: *** [CMakeFiles/boostpy.dir/rule] Error 2
Makefile:118: recipe for target 'boostpy' failed
make: *** [boostpy] Error 2
like image 675
tyler124 Avatar asked Oct 31 '25 08:10

tyler124


1 Answers

The solution to this issue was that I had to import the Python library itself in addition to the Boost Python library.

In this case, my Python library was at /usr/lib/x86_64-linux-gnu/libpython3.6m.so

Along the way I learned some helpful use of the find_package functionality.

There were a few other issues along the way:

  • I had to use a shared library instead of an executable
  • find_package was defaulting to a different version of Python. I had to specify the correct version.
  • I had to make sure the name specified in BOOST_PYTHON_MODULE(hello) matched the library name. The library was being called "libhello.so". I had to tell cmake to remove the "lib" prefix.

Final files:

hello.cpp

char const* greet()
{
    return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(hello)

set(CMAKE_CXX_STANDARD 14)

find_package(Python 3.6 COMPONENTS Interpreter Development REQUIRED)
find_package(Boost 1.65.1 COMPONENTS python3.6 REQUIRED)

if(Boost_FOUND)
    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")
    include_directories(${Boost_INCLUDE_DIRS})
endif()
if(Python_FOUND)
    message(STATUS "Python Found: ${Python_EXECUTABLE}")
    message(STATUS "Python Found: ${Python_INCLUDE_DIRS}")
    message(STATUS "Python Found: ${Python_LIBRARIES}")
    message(STATUS "Python Found: ${Python_LIBRARY_DIRS}")
    include_directories(${Python_INCLUDE_DIRS})
endif()

add_library(hello SHARED hello.cpp)
SET_TARGET_PROPERTIES(hello PROPERTIES PREFIX "")
if(Boost_FOUND)
    target_link_libraries(hello ${Boost_LIBRARIES} ${Python_LIBRARIES})
endif()
like image 189
tyler124 Avatar answered Nov 01 '25 23:11

tyler124