'boost::python::init<>() undefined reference

I'm trying to expose a simple class and constructor using boost python. I have the following:

files:

boost_test
    ├── boost_test.cpp
    └── CMakeLists.txt

CMakeLists.txt:

cmake_minimum_required(VERSION 3.22.1)

project(boost_test)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

find_package(PythonLibs 3.8 REQUIRED)
find_package(Boost COMPONENTS python38 REQUIRED)

add_library(${PROJECT_NAME} MODULE boost_test.cpp)

set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

set(CMAKE_SHARED_MODULE_PREFIX "")

target_include_directories(${PROJECT_NAME} PRIVATE ${PYTHON_INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES})
target_link_libraries(${PROJECT_NAME} Boost::python38)

boost_test.cpp:

#include <boost/python.hpp>

using namespace boost::python;

struct A {
};

BOOST_PYTHON_MODULE(boost_test) {
    class_<A>("A", init<>());
}

When I try to import the library in python I get the following:

>>> import boost_test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: .../src/boost/boost_test/boost_test.so: undefined symbol: _ZN5boost6python15instance_holder8allocateEP7_objectmmm

If I replace the init<>() with no_init in the boost_test.cpp I don't get the error, but I won't be able to instantiate the class.

I'm really not sure how to fix this. Any help is greatly appreciated.



Solution 1:[1]

It turned out boost was installed incorrectly. I'm not sure what I did wrong, but rebuilding the boost library fixed the issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 catmousedog