Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error importing python module made by pybind11

I followed the steps in this video, and was able to successfully import a python module made by pybind11 and call the function add(). When I try to refactor my code and move the add() function to a separate file, I get the following error message:

>>> import pybindings
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/user_name/Desktop/project/build/pybindings.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace (__Z3addii)

This is my cpp file:

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "refactored_code.hpp" //this contains the add() function

PYBIND11_MODULE(pybindings, m) {
  m.doc() = "test of pybind11";

  m.def("add", &add, "adds two numbers");
}

and this is my CMakeLists.txt:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cmake_minimum_required(VERSION 3.6)
project(test_project)
add_subdirectory(pybind11)
pybind11_add_module(pybindings pybindings.cpp)

I’d appreciate any help since I’m very new to this. Thank you!

>Solution :

First: Turn on all compiler warnings and errors

-pedantic-errors -Wall -Wextra

Second: Can you show the definition you have of the add() function?

Third: Does your refactored_code.hpp have a cpp file? You need to list it along with your pybindings.cpp file in here:

pybind11_add_module(pybindings pybindings.cpp refactored_code.cpp) // Notice the "cpp" not "hpp"

Undefined symbol means that you had the header but did not link in the actual implementation. There are some legitimate reasons to do this but it’s almost never right to not link it in directly like this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading