2013-04-13 66 views
1

操作系統未定義的引用: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. 

如果我將編譯拆分爲對象階段然後是二進制階段,則二進制階段會失敗,並且會出現與最初提到的相同的錯誤消息。

+0

什麼是您的操作系統和尋址架構? –

+0

Ubuntu 12.04 x86_64。我使用--prefix構建了Python並進行了altinstall。 – Miles

+0

回答了你的問題的第二部分,在原文中增加了信息 – Miles

回答

0

找到解決方案:Python實際上安裝了自己的解決方案。在這篇文章中我討論了這個問題(我知道我的頭文件並沒有丟失,但是我很愚蠢,並試圖將它連接起來,想:「哦,這正是任何人需要做的,才能讓Python在我的平臺上工作」 )...

Python.h header missing