2012-12-28 25 views
0

簡短介紹: - (GCC版本4.6.3,OS-Ubuntu 12.04,解決貓鼬網絡服務器程序,所以當我運行「make」命令編譯和安裝貓鼬,它已經完成了任務)。我無法編譯這個貓鼬網絡服務器的示例代碼

[問題的第一部分] 這個問題是參考這個帖子在stackowerflow。

mongoose web server helloworld program

Valenok已經通過給一個鏈接到Hello示例程序回答了這個職位。

基本上,我試圖編譯此鏈接中給出的示例招呼程序代碼: - 。

http://code.google.com/p/mongoose/source/browse/examples/hello.c

,並把這個代碼在貓鼬的已編譯的目錄(目錄中有mongoose.h文件)

以下是用於編譯hello程序的命令行輸出。

[email protected]:~$ gcc mongoose/hello.c -o mongoose/hello 
/tmp/ccroC5Z6.o: In function `callback': 
hello.c:(.text+0x32): undefined reference to `mg_get_request_info' 
hello.c:(.text+0x96): undefined reference to `mg_printf' 
/tmp/ccroC5Z6.o: In function `main': 
hello.c:(.text+0xee): undefined reference to `mg_start' 
hello.c:(.text+0x103): undefined reference to `mg_stop' 
collect2: ld returned 1 exit status 
[email protected]:~$ 

[問題的第2部分]

現在,我發現mg_stop,mg_start,mg_printf和mongoose.c文件mg_get_request_info的實現,所以我編譯-c選項爲mongoose.c文件: 的gcc -o -c mongoose.o mongoose.c

我想我的問題是相似的: -

undefined reference to function declared in *.h file

但後來當我在GCC libmongoose.so與-L選項鍊接我收到以下錯誤: - (libmongoose.so存在於同一個目錄,我的CWD)

[email protected]:~/mongoose$ gcc -L libmongoose.so -o hello hello.o mongoose.o 
mongoose.o: In function `mg_start_thread': 
mongoose.c:(.text+0x1369): undefined reference to `pthread_create' 
mongoose.o: In function `load_dll': 
mongoose.c:(.text+0xa955): undefined reference to `dlopen' 
mongoose.c:(.text+0xa9b4): undefined reference to `dlsym' 
collect2: ld returned 1 exit status 

也,我繼續得到^^誤差以上當我編譯而不使用libmongoose.so

[編輯]:上的gcc加入-pthread選項,仍顯示錯誤: - mongoose.o:在功能load_dll': mongoose.c:(.text+0xa955): undefined reference to的dlopen」 mongoose.c :(.text + 0xa9b4):未定義的引用'dlsym' collect2:ld返回1退出狀態

對於我的問題的第1部分和第2部分:我想擺脫這些錯誤併成功運行hello.c程序示例。 在此先感謝。

回答

5

-L選項不用於鏈接庫,它用於指定動態庫的搜索路徑。要鏈接特定的庫,請使用-l。但是,您不需要鏈接到mongoose.olibmongoose.so,任何一個都足夠。

在Linux上,您還必須鏈接到pthread和動態加載庫,因爲儘管它們是C標準庫的一部分,但它們不在libc.so中。還有一點需要注意的是,binutils(特別是ld)的最新版本要求庫和對象文件按照符號相互依賴的順序指定,即i。即庫必須到命令行的末尾。

總而言之,請使用以下命令之一:

gcc -o hello hello.o mongoose.o -ldl -lpthread 

gcc -L. -o hello hello.o -lmongoose -ldl -lpthread 
+0

@ H2CO3非常感謝!你提到的第一個命令工作正常。 但對於第二個命令出現以下錯誤 當我通過./hello運行它: - 「錯誤而載入共享庫:libmongoose.so:無法打開共享對象文件:沒有這樣的文件或目錄‘’ –

+0

@AkshayPatil是,這就是它的工作原理,請閱讀一下動態加載。 – 2012-12-28 10:28:52

+0

好的!再次感謝 –