2012-12-26 36 views
9

我跑..GCC未定義參考

gcc -c -I/usr/vt/sample ttssample.c 
gcc -L. -lttsapi ttssample.o -o ttsample 

,我發現了以下錯誤......

ttssample.o: In function `_TTSFile': 
ttssample.c:(.text+0x352): undefined reference to `TTSRequestFile' 
ttssample.o: In function `_TTSFileEx': 
ttssample.c:(.text+0x5e0): undefined reference to `TTSRequestFileEx' 
ttssample.o: In function `_TTSBuffer': 
ttssample.c:(.text+0x833): undefined reference to `_TTSRequestBuffer' 
ttssample.o: In function `_TTSBufferEx': 
ttssample.c:(.text+0xabd): undefined reference to `_TTSRequestBufferEx' 
ttssample.o: In function `_TTSBuffering_cont': 
ttssample.c:(.text+0xcbf): undefined reference to `_TTSRequestBuffer' 
ttssample.o: In function `_TTSBuffering_stop': 
ttssample.c:(.text+0xf2d): undefined reference to `_TTSRequestBuffer' 
ttssample.o: In function `_TTSBuffering_SSML': 
ttssample.c:(.text+0x122b): undefined reference to `_TTSRequestBufferSSMLEx' 
ttssample.o: In function `_TTSStatus': 
ttssample.c:(.text+0x157b): undefined reference to `TTSRequestStatus' 
collect2: ld returned 1 exit status 

和TTSRequestFile是在lib頭,但它有DLLEXPORT上我想知道的正面是我錯誤的原因?任何幫助非常感謝。

DllExport int TTSRequestFile(char *szServer, int nPort, char *pText, int nTextLen, char *szSaveDir, char *szSaveFile, int nSpeakerID, int nVoiceFormat); 

回答

21

您的鏈接命令是錯誤的。應該在命令末尾指定庫:

gcc ttssample.o -o ttsample -L. -lttsapi
+0

它的工作謝謝你! – JLB

+0

任何想法爲什麼這會錯誤運行可執行文件?加載共享庫時出錯:-melf_x86_64:無法打開共享目標文件:無此文件或目錄 – JLB

+1

@JLB因爲庫不在默認情況下由運行時加載程序搜索的目錄中。您需要將該庫放入搜索的目錄(如/ usr/local/lib),或者導出LD_LIBRARY_PATH環境變量以包含庫駐留的目錄(如LD_LIBRARY_PATH =「/ home/me/mylib」),或者你可以使用一個指向lib所在目錄的rpath來鏈接你的程序,或者使用rpath的$ ORIGIN特性來總是在一個目錄中尋找一個相對於可執行文件相對路徑的lib。搜索Google「rpath origin」以查找更多信息。 –

0

您可以添加預處理器ifdefines圍繞DllExport調用就像這樣:

#ifdef _WIN32 
// we are on windows 

#elif defined __linux__ 
//we are on linux 

#elif defined __APPLE__&__MACH__ 
// we are on mac 

#endif // os specific 

我添加了三個平臺,我一直編譯跨平臺。請注意我用來識別平臺的關鍵字可能會更改,但_WIN32之一已經在Windows 7和8上進行了測試。 我認爲這是一年前在sourceforge上發現的。我現在找不到該頁面,但如果我找到它,我會盡快與您聯繫。

由於我還不能評論Nikos C的答案,我會在這裏評論它:你的鏈接命令是正確的,我當然可以看不到你的文件,所以我假設你的路徑是正確的。重要的是-l需要根據依賴關係按正確的順序排列,但就我所經歷的情況而言,這通常不是問題。