0

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.

1 Answer 1

2

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");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.