我正在为该项目使用 TDM-GCC-64。这是最小的源代码(完整的源代码包括 Python 模块初始化的代码):
// minimal.cpp
#include <Python.h>
extern "C"
{
static PyObject * f() {
PyObject * tuple = PyTuple_New(2);
PyTuple_SetItem(tuple, 0, PyLong_FromLong(0L));
PyTuple_SetItem(tuple, 1, PyFloat_FromDouble(0.));
return tuple;
}
}
int main()
{
f();
return 0;
}
并且Makefile
(原始规则将代码编译成.so
文件):
ANACONDA_PKGS = C:/Anaconda3/pkgs
PY_INCLUDE = -I$(ANACONDA_PKGS)/python-3.5.2-0/include
PY_LINK = -L$(ANACONDA_PKGS)/libpython-2.0-py35_0/libs -lpython35.dll
CXXFLAGS = -fPIC -fopenmp -O3 -std=c++11 $(PY_INCLUDE) $(PY_LINK) -Wall
CC = g++
test: minimal.o
$(CC) $(CXXFLAGS) -o minimal.exe minimal.o
minimal: minimal.cpp
$(CC) $(CXXFLAGS) -c minimal.cpp
然后运行make test
给出:
minimal.o:minimal.cpp:(.text.startup+0x12): undefined reference to `__imp_PyTuple_New'
minimal.o:minimal.cpp:(.text.startup+0x1d): undefined reference to `__imp_PyLong_FromLong'
minimal.o:minimal.cpp:(.text.startup+0x2c): undefined reference to `__imp_PyTuple_SetItem'
minimal.o:minimal.cpp:(.text.startup+0x38): undefined reference to `__imp_PyFloat_FromDouble'
collect2.exe: error: ld returned 1 exit status
Makefile:29: recipe for target 'test' failed
make: *** [test] Error 1
我也检查libpython35.dll.a
过nm
,看起来功能在那里。例如,可以在 的输出中找到以下文本nm
:
deuhs00780.o:
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 i .idata$4
0000000000000000 i .idata$5
0000000000000000 i .idata$6
0000000000000000 i .idata$7
0000000000000000 t .text
0000000000000000 I __imp_PyTuple_New
U _head_C__Users_ray_mc_x64_3_5_Library_home_ray_mc_x64_3_5_conda_bld_libpython_1476533413999__b_env_libs_libpython35_dll_a
0000000000000000 T PyTuple_New
那么问题出在哪里?谢谢。