亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2847 | 回復(fù): 0
打印 上一主題 下一主題

gcc 編譯與鏈接選項 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-01-16 22:39 |只看該作者 |倒序瀏覽
Q: I keep getting errors due to library functions being undefined, but I'm #including all the right header files.
--------------------------------------------------------------------------------
A: In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search the external library itself. The declarations in the header file only tell the compiler how to call the external functions; the header file doesn't supply the definitions of the external functions, or tell the compiler/linker where to find those definitions.
In some cases (especially if the functions are nonstandard) obtaining those definitions may require explicitly asking for the correct libraries to be searched when you link the program. (Some systems may be able to arrange that whenever you #include a header, its associated library, if nonstandard, is automatically requested at link time, but such a facility is not widespread.) See also questions 10.11 , 11.30 , 13.26 , 14.3 , and 19.40 .
Q: I'm trying to do some simple trig, and I am #including  , but the linker keeps complaining that functions like sin and cos are undefined.
--------------------------------------------------------------------------------
A: Make sure you're actually linking with the math library. For instance, due to a longstanding bug in Unix and Linux systems, you usually need to use an explicit -lm flag, at the end of the command line, when compiling/linking. See also questions 13.25 , 13.26 , and 14.2 .


當(dāng)使用gcc編譯器編譯含數(shù)學(xué)函數(shù)的C程序時,會出現(xiàn)undefined reference to `sin'錯誤.這種錯誤一般是由于缺少庫造成的.使用-lm即可.Makefile可以這樣寫:
pe14-18-11:pe14-18-11.o
         gcc pe14-18-11.o -lm -o pe14-18-11
pe14-18-11.o:pe14-18-11.c
         gcc -c pe14-18-11.c -o pe14-18-11.o
clean:
         rm -f *.o pe14-18-11
具體原因及解決辦法為:
加入連結(jié)的函式庫
剛剛我們都僅只是在屏幕上面印出一些字眼而已,如果說要計算數(shù)學(xué)公式呢?!例如我們想要計算出三角函數(shù)里面的 sin(90度角),要注意的是,大多數(shù)的程序語言都是使用徑度而不是一般我們在計算的『角度』, 180 度角約等于 3.14 徑度!嗯!那我們就來寫一下這個程序吧!
  [guest@test guest]# vi sin.c
#include  
int main(void)
{
        float value;
        value = sin ( 3.14 / 2 );
        printf("%f\n",value);
}
# 上面這個檔案的內(nèi)容可以在底下取得!
#
http://linux.vbird.org/download/books/basic/source_code/sin.c
  
 
那要如何編譯這支程序呢?我們先直接編譯看看:
  [guest@test guest]# gcc sin.c
/tmp/ccppUCx8.o(.text+0x1e): In function `main':
: undefined reference to `sin'
collect2: ld returned 1 exit status  
 
特別注意上面的說明,唉。≡趺礇]有編譯成功?它說的是『 undefined reference to sin 』,說的是『 沒有 sin 的相關(guān)定義參考值! 』,為什么會這樣呢?這是因為 C 語言里面的 sin 函示是寫在 libm.so 這個函式庫中,而我們并沒有在原始碼里面加入相關(guān)的說明,所以當(dāng)然就需要在編譯與連結(jié)的時候?qū)⑦@個函式庫給他連結(jié)進(jìn)執(zhí)行檔里面!所以我們可以這樣做:
  [guest@test guest]# gcc sin.c -lm -L/lib -L/usr/lib
# 特別注意,那個 -lm 可以拆開成兩部份來看,
# -l 是『加入某個函式庫(library)』的意思,而
# m 則是 libm.so 這個函式庫,其中, lib 與附檔名(.a 或 .so)不需要寫
# 所以 -lm 表示使用 libm.so (或 libm.a) 這個函式庫的意思~
# 至于那個 -L 后面接的路徑呢?這表示:
#『我要的函式庫 libm.so 請到 /lib 或 /usr/lib 里面搜尋! 』
[guest@test guest]# ./a.out
1.000000  
 
上面的說明很清楚了吧!!不過,要注意的是,由于 Linux 預(yù)設(shè)是將函式庫放置在 /lib 與 /usr/lib 當(dāng)中,所以您沒有寫 -L/lib 與 -L/usr/lib 也沒有關(guān)系的!不過,萬一哪天您使用的函式庫并非放置在這兩個目錄下,那么 -L/path 就很重要了!否則會找不到函式庫喔!
 
除了連結(jié)的函式庫之外,您或許已經(jīng)發(fā)現(xiàn)一個奇怪的地方,那就是在我們的 sin.c 當(dāng)中第一行『 #include  』,這行說的是要將一些定義數(shù)據(jù)由 stdio.h 這個檔案讀入,這包括 printf 的相關(guān)設(shè)定。這個檔案其實是放置在 /usr/include/stdio.h 的!那么萬一這個檔案并非放置在這里呢?那么我們就可以使用底下的方式來定義出要讀取的 include 檔案放置的目錄:
  [guest@test guest]# gcc sin.c -lm -I/usr/include  
 
-I/path 后面接的路徑( Path )就是設(shè)定要去搜尋相關(guān)的 include 檔案的目錄啦!不過,同樣的,默認(rèn)值是放置在 /usr/include 底下,除非您的 include 檔案放置在其它路徑,否則也可以略過這個項目!

本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:
http://blog.csdn.net/tylovemx/archive/2009/11/25/4872727.aspx


本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u3/108299/showart_2151034.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP