我正在尝试使用Cython从Python生成C代码,但似乎存在一些名称混淆问题。我首先将代码从Python转换为C代码,然后使用gcc将代码编译为.so文件。我想要使用Cython而不是C/Python API的原因是因为我以后将在更复杂的类上使用它,希望将其作为库以提高速度等(我很难找到从Python到C++的人,因为通常情况下都是反过来)。以下是我尝试执行该代码时拥有的所有代码(但失败了)。任何意见都将不胜感激。谢谢!
这是错误信息:
#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
这是错误信息:
/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