I am experimenting with Cython to generate c code from python but there seems to be some issues with name mangling. I first generate convert the code from python to c code and then I compile the code using gcc into a .so . The reason I want to use cython instead of C/python API is because I will be later using this on more complicated classes that I would like to be a library for speed etc later on (I am having a lot of trouble finding people who go from python to C++ since it is usually the other way around). Below is all the code that I have to try to execute the code (but fails). Any input will be appreciated. Thanks!
#hello.pyx
def say_hello():
print "Hello World!"
#generate the c code
cython -a hello.pyx
#creates the shared library
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.6 -o libhello.so hello.c
//temp.cpp
#include <iostream>
extern "C" {
void say_hello();
};
using namespace std;
int main(){
say_hello();
return 1;
};
#attempt to compile (this is where it fails)
g++ -I/usr/include/python2.6/ -lpython2.6 -L./ -lhello temp.cpp -o temp
Here is the error message:
/tmp/ccpKHOMl.o: In function main: temp.cpp:(.text+0x5): undefined reference to say_hello' /tmp/ccpKHOMl.o:
In function __static_initialization_and_destruction_0(int, int):
temp.cpp:(.text+0x33): undefined reference to std::ios_base::Init::Init()
temp.cpp:(.text+0x38): undefined reference to std::ios_base::Init::~Init()
collect2: ld returned 1 exit status
main': temp.cpp:(.text+0x5): undefined reference tosay_hello' /tmp/ccpKHOMl.o: In function__static_initialization_and_destruction_0(int, int)': temp.cpp:(.text+0x33): undefined reference tostd::ios_base::Init::Init()' temp.cpp:(.text+0x38): undefined reference to `std::ios_base::Init::~Init()' collect2: ld returned 1 exit status