I am using TDM-GCC-64 for the project. Here is the minimal source code (the full source includes code for Python module initialization):
// 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;
}
And the Makefile
(the original rule compiles the code into an .so
file):
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
Then running make test
gives:
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
I also checked libpython35.dll.a
by nm
, and it looks like the functions are there. For example, the following text can be found in the output of 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
So where is the problem? Thanks.