I am trying to read a C++ code (long and nested) in Python. My C++ files are:
- celinoid_propagator.cpp --> contains the "main" function.
- celinoid_propagator_lib.cpp --> contains the methods of each of the functions. The only function I want to use is here.
- celinoid_propagator_lib.h --> contains the declarations of variables, structures, functions, etc, all inside a class named "MODEL_GAUSS".
I have tried with ctypes but no luck. After some research (and because of a suggestion in my last question), I have decided to try Pybind11.
In principle, it seems more straightforward to use. I am building the project with Cmake. Both the cmake command and the make command work fine.
However, when I try to run my .py program I get the following error:
Traceback (most recent call last): File "/home/milagros/Desktop/OPUS/C-Python/C-Python/pybind11/program.py", line 3, in . from celinoid import MODEL_GAUSS ImportError: /home/milagros/Desktop/OPUS/C-Python/pybind11/build/celinoid.cpython-311-x86_64-linux-gnu.so: undefined symbol: _ZN11MODEL_GAUSS27generate_syntetic_magnitudoEPddi
This looks like there is a problem with the mangled names.
This is my .cpp wrapper:
// model_gauss-wrapper.cpp
#include <pybind11/pybind11.h>
#include "celinoid_propagator_lib.h"
namespace py = pybind11;
constexpr auto byref = py::return_value_policy::reference_internal;
PYBIND11_MODULE(celinoid, m) {
m.doc() = "Model Gauss for celinoid propagator";
py::class_<MODEL_GAUSS>(m, "Model_Gauss")
.def(py::init<>())
.def("generate_syntetic_magnitudo", &MODEL_GAUSS::generate_syntetic_magnitudo, py::call_guard<py::gil_scoped_release>());
}
Somewhere I read that I should put "extern "C" {#include "celinoid_propagator_lib.h"}" but it really doesn't change anything.
And here is my CMake file:
cmake_minimum_required(VERSION 3.15)
project(celinoid)
link_directories(${PROJECT_SOURCE_DIR})
find_package(pybind11 REQUIRED)
pybind11_add_module(celinoid model_gauss-wrapper.cpp)