我在 64 位 Windows 服务器上遇到以下问题:
我有一个声明 Python 扩展的 .c 文件。使用以下命令将 .c 文件编译为 .o 文件
gcc -c chr.c -IC:\Anaconda\include -o chr.o
现在,.o 文件已创建,一切正常。但是当我尝试创建 .py 文件时,我收到下面的错误响应。
我的理解是,在我的 C 代码中,我调用了“not_doublevector”、“__impPy_InitModule4”、“chr”和“pyvector_to_Carrayptrs”函数。
gcc -shared chr.o -LC:\Anaconda\libs -lpython27 -o chr.py
chr.o:chr.c:(.text+0x1a15): undefined reference to `not_doublevector'
chr.o:chr.c:(.text+0x1a35): undefined reference to `not_doublevector'
chr.o:chr.c:(.text+0x1a89): undefined reference to `pyvector_to_Carrayptrs'
chr.o:chr.c:(.text+0x1a99): undefined reference to `pyvector_to_Carrayptrs'
chr.o:chr.c:(.text+0x1aeb): undefined reference to `chr'
chr.o:chr.c:(.text+0x1c3d): undefined reference to `__imp_Py_InitModule4'
collect2.exe: error: ld returned 1 exit status
最佳答案
我能够解决这个问题。问题是上面提到的函数是在我的头文件中向前声明的,但函数的主体是在使用这些相同头文件的单独源文件中声明的。我将函数体复制到源文件中,一切正常
关于python - 错误: "Undefined Reference to" - Compile C extension for Python using Anaconda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491158/