操作系统: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.
如果我将编译拆分为对象阶段然后是二进制阶段,则二进制阶段将失败,并显示与最初提到的错误消息相同的错误消息。
答案 0 :(得分:1)
找到解决方案:Python实际上安装了自己的解决方案。在这篇文章中谈到了它(我知道我的标题并没有丢失,但我很愚蠢并试图将它全部链接起来,想着“哦,这正是任何人都需要做的才能让Python在我的平台上工作” )...