Building a shared object from a single .cpp
I am trying to build a shared object library from a cpp file that is a simple set of functions. I want to use ctypes to interface with python.
Say I have the cpp file:
and header file
I tried to build
Then in python
the line
yields
nm -D print.so
gives the output
What am I fundamentally doing wrong in the compilation step?
In short, you are calling a c++ function while ctypes expects C functions linkage:
In short, you are missing an extern "C" declaration right before the function declaration in the header file:
This will prevent the C++ name mangling.
Using vectors and other C++ types will likely cause you ABI issues It's a good idea to use C functions with plain C types in your functions as an interface.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.