操作系统:Ubuntu 12.04 x86_64
我已经从源代码编译了 libpython3.3m.a 以及相关的头文件。我编写了以下 Makefile(具体来说,请查看我的 CFLAGS 变量,其中包括 -lpython3.3m)...
BINS=python python-d
SOURCES=python.cpp
LIBROOT=/home/uberblah/lib/cpp
LIBDIR=$(LIBROOT)/lib
BINDIR=$(LIBROOT)/bin
INCDIR=$(LIBROOT)/include
CFLAGS= -I$(INCDIR)/python3.3m -L$(LIBDIR) -lpython3.3m
all: $(BINS)
python-d: $(SOURCES)
g++ $(CFLAGS) -g -o $@ $^
python: $(SOURCES)
g++ $(CFLAGS) -o $@ $^
clean::
rm -r -f *~ $(BINS)
edit::
gedit $(SOURCES) Makefile &
我编写了以下 .cpp 源文件...
#include <Python.h>
int main(int argc, char** argv)
{
Py_SetProgramName((wchar_t*)argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
在 Makefile 的目录中键入 make 会导致以下消息。
g++ -I/home/uberblah/lib/cpp/include/python3.3m -L/home/uberblah/lib/cpp/lib -lpython3.3m -o python python.cpp
/tmp/ccBD4sLY.o: In function `main':
python.cpp:(.text+0x1a): undefined reference to `Py_SetProgramName'
python.cpp:(.text+0x1f): undefined reference to `Py_Initialize'
python.cpp:(.text+0x2e): undefined reference to `PyRun_SimpleStringFlags'
python.cpp:(.text+0x33): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status
make: *** [python] Error 1
根据我对 g++ 的“-l”参数的理解,它正在找到 libpython3.3m.a,否则它会告诉我它无法找到库并退出。
那么,如果它正在查找库,为什么不定义这些函数呢?
补充说明...
使用 --prefix 和 altinstall 构建 Python 以将其安装到新目录(我在 makefile 中指向 LIBROOT 的目录)。
我已经确认 libpython3.3m.a 在 $(LIBDIR)
使用 nm 检查 Py_SetProgramName ...
uberblah@uberblah-N80Vm:~/lib/cpp/lib$ nm libpython3.3m.a | grep Py_SetProgramName
U Py_SetProgramName
0000000000001c00 T Py_SetProgramName
U Py_SetProgramName
详细运行 Make...
...
Finished prerequisites of target file `python'.
Must remake target `python'.
g++ -I/home/uberblah/lib/cpp/include/python3.3m -L/home/uberblah/lib/cpp/lib -lpython3.3m -o python python.cpp
Putting child 0x00a585b0 (python) PID 16811 on the chain.
Live child 0x00a585b0 (python) PID 16811
/tmp/cc2dom1S.o: In function `main':
python.cpp:(.text+0x1a): undefined reference to `Py_SetProgramName'
python.cpp:(.text+0x1f): undefined reference to `Py_Initialize'
python.cpp:(.text+0x2e): undefined reference to `PyRun_SimpleStringFlags'
python.cpp:(.text+0x33): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status
Reaping losing child 0x00a585b0 PID 16811
make: *** [python] Error 1
Removing child 0x00a585b0 PID 16811 from chain.
如果我将编译拆分为对象阶段然后是二进制阶段,则二进制阶段将失败,并显示与最初提到的相同的错误消息。