操作系统未定义的引用:Ubuntu的12.04 x86_64的的Python 3.3静态链接
我编译从源代码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 [email protected] $^
python: $(SOURCES)
g++ $(CFLAGS) -o [email protected] $^
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把它安装到新目录(一个我指着LIBROOT在我的makefile)内置了Python。
我已经证实,libpython3.3m.a是$(LIBDIR)
使用纳米检查Py_SetProgramName ...
[email protected]:~/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.
如果我将编译拆分为对象阶段然后是二进制阶段,则二进制阶段会失败,并且会出现与最初提到的相同的错误消息。
什么是您的操作系统和寻址架构? –
Ubuntu 12.04 x86_64。我使用--prefix构建了Python并进行了altinstall。 – Miles
回答了你的问题的第二部分,在原文中增加了信息 – Miles