2010-09-28 92 views
2

我是使用gcc的新手,所以我有幾個問題。gcc開關 - 這些做什麼?

什麼以下開關完成:

 
gcc -v -lm -lfftw3 code.c 

我知道lfftw3code.c使用.h文件,但爲什麼是命令的一部分?

我找不到-lm在我的搜索中做了什麼。它有什麼作用?

我想我找到了-v導致gcc顯示它調用的程序。

+3

如果您在Unix,Linux或* BSD機器上發出man gcc命令並讀取它。如果沒有,那麼谷歌'man gcc'並閱讀其中一個結果頁面。 – nategoose 2010-09-28 20:23:18

+0

在使用它們的模塊之後立即獲取圖書館*的習慣。這意味着你應該寫'gcc -v code.c -lfftw3 -lm'而不是你顯示的順序。這是因爲事物通常在命令行上從左到右加載,並且許多平臺上的鏈接器強烈偏好右側的項不依賴於左側的庫。 '-lm'幾乎總是最後一個,因爲它是一個沒有依賴關係的系統庫(當然除了libc)。 – RBerteig 2010-09-28 20:37:33

回答

11

-l指定庫包括。在這種情況下,你包括數學庫(-lm)和fftw3庫(-lffw3)。該庫將在您的庫路徑中的某處,可能是/ usr/lib,並且將被命名爲類似於libffw3.so

+1

要擴大一點,'-l'是開關,緊隨其後的是文本庫名稱。你也可以寫'-l m'。空格是可選的。 – atzz 2010-09-28 20:22:49

+0

難怪我無法在我的搜索中找到-lm ... – DemiSheep 2010-09-28 20:30:33

1

-lm將您的程序與數學庫鏈接。 -v是編譯器的詳細(額外輸出)標誌。 -lfftw3將您的程序與fftw3庫鏈接。

你只包括通過使用#include "fftw3.h"頭。如果你想實際包含與之相關的代碼,你需要鏈接它。 -l就是這樣。與庫鏈接。

0

以-l開頭的參數指定鏈接到程序中的庫。像帕布羅聖克魯斯說,-lm是標準的數學庫,-lfftw3是一個用於傅里葉變換的庫。

4

從GCC的手冊頁:

 -v Print (on standard error output) the commands executed to run the 
      stages of compilation. Also print the version number of the 
      compiler driver program and of the preprocessor and the compiler 
      proper. 
-l library 
      Search the library named library when linking. (The second 
      alternative with the library as a separate argument is only for 
      POSIX compliance and is not recommended.) 

      It makes a difference where in the command you write this option; 
      the linker searches and processes libraries and object files in the 
      order they are specified. Thus, foo.o -lz bar.o searches library z 
      after file foo.o but before bar.o. If bar.o refers to functions in 
      z, those functions may not be loaded. 

      The linker searches a standard list of directories for the library, 
      which is actually a file named liblibrary.a. The linker then uses 
      this file as if it had been specified precisely by name. 

      The directories searched include several standard system 
      directories plus any that you specify with -L. 

      Normally the files found this way are library files---archive files 
      whose members are object files. The linker handles an archive file 
      by scanning through it for members which define symbols that have 
      so far been referenced but not defined. But if the file that is 
      found is an ordinary object file, it is linked in the usual 
      fashion. The only difference between using an -l option and 
      specifying a file name is that -l surrounds library with lib and .a 
      and searches several directories. 

libmmath.h使用該庫,所以-lm包括圖書館。您可能想要更好地理解linking的概念。基本上,該開關爲你的程序增加了一堆編譯好的代碼。

0

嘗試man當你試圖瞭解一個命令。

man gcc

-v打印(在標準錯誤輸出)執行以運行彙編 階段的命令。同時打印 的COM 堆垛機驅動程序和預處理器的版本號和編譯器 正確。

正如Pablo所述,-lm鏈接你的數學庫。

-lfftw3鏈接在用於傅立葉變換庫。該項目頁面,更多信息可以在這裏找到:
http://www.fftw.org/

所有這些語句的淨要點是,他們編譯代碼文件轉換成程序,該程序將被命名爲默認(a.out的),是取決於來自數學和傅里葉變換庫的函數調用。 -v語句只是幫助您跟蹤編譯過程並診斷應該發生的錯誤。

+0

我嘗試過gcc,試圖搜索-v,但我無法弄清楚如何只顯示「-v」而沒有其他東西出現數以百計的其他版本類型的東西。 – DemiSheep 2010-09-28 20:29:30

+1

你只需要保持向下滾動,直到你到達字母標誌部分。像gcc這樣的程序有很多選項,所以手冊頁可以非常強大,但是值得深入研究。 – 2010-09-28 22:04:13

0

除了man gcc這應該是有關任何命令的問題的第一站,您也可以嘗試幾乎標準的--help選項。即使對不支持它的命令,一個不受支持的選項通常會導致它打印一個包含應該提示類似選項的使用信息的錯誤。在這種情況下,gcc會顯示一個簡短的內容(對於gcc來說,它只有大約50行)幫助總結列出gcc程序本身理解的少量選項,而不是傳遞給它的組件程序。在--help選項本身的描述之後,它列出了--target-help-v --help作爲獲取有關目標體系結構和組件程序的更多信息的方法。

我的MinGW GCC 3.4.5安裝在Windows XP上從gcc -v --help產生超過1200行的輸出。我很確定,在其他安裝中,它不會變得更小。

閱讀official manual for GCC也是一個好主意。閱讀鏈接器(ld)和彙編器(通常爲gas或僅爲as,但它也可能是某個平臺特定的彙編器)的文檔也很有幫助;除了特定於平臺的彙編程序外,還將這些文件記錄爲binutils集合的一部分。

對Unix工具的命令行風格的一般熟悉也很有幫助。單字符選項的值不能與選項名稱分開的想法是一個基本上可以追溯到Unix的慣例。現代會議(由GNU頒佈)--引入了多字符選項名稱,而不僅僅是-意味着-lm可能是-l m(或一些選項中的一對選項-l -m)的同義詞,但事實並非如此gcc),但它可能不是一個名爲-lm的選項。例如,您將看到與控制特定優化的-f選項或控制警告的-W選項類似的模式。