Reputation: 204
I'm trying to compile a very simple example using pybind11, but unlike all tutorials I can find, I don't want to copy the pybind11 repo into my project. I currently have
CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(relativity)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
find_package(pybind11)
file(GLOB SOURCES "*.cpp")
pybind11_add_module(${PROJECT_NAME} ${SOURCES})
main.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function that adds two numbers");
}
When I run cmake ..
and make
I get no errors and the relativity.so
file is built. However if I attempt to import it in python using import relativity
I get:
ImportError: dynamic module does not define module export function (PyInit_relativity)
What am I doing wrong exactly? I can't really find any detailed examples or tutorials that do it this way.
EDIT: I tried cloning the pybind11 repo into my project and using the following CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(relativity)
add_subdirectory(pybind11)
pybind11_add_module(${PROJECT_NAME} main.cpp)
but this gives the same error when importing in python3.
Upvotes: 0
Views: 313
Reputation: 1232
The first argument passed to the PYBIND11_MODULE
macro should be the name of the module (and therefore should match the content of the "PROJECT_NAME" variable as defined in the cmake file):
PYBIND11_MODULE(relativity, m) { // <---- "relativity" instead of "example"
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function that adds two numbers");
}
Upvotes: 2