1. 程式人生 > >GCC編譯選項-包含的標頭檔案

GCC編譯選項-包含的標頭檔案

 

許多情況下,標頭檔案和原始檔會單獨存放在不同的目錄中。

可以直接在.c檔案中利用#include“/path/file.h", 通過指定標頭檔案的路徑(可以是絕對路徑,也可以是相對路徑)來包含標頭檔案. 但這明顯降低了程式的可移植性. 在別的系統環境下編譯可能會出現問題.

  所以還是利用"-I"選項指定標頭檔案完整的包含路徑.

  針對標頭檔案比較多的情況, 最好把它們統一放在一個目錄中,比如~/project/include. 這樣就不需為不同的標頭檔案指定不同的路徑. 如果你嫌每次輸入這麼多選項太麻煩,你可以通過設定環境變數來新增路徑:

  $ C_INCLUDE_PATH=/opt/gdbm-1.8.3/include

  $ export C_INCLUDE_PATH

  $ LIBRART_PATH=/opt/gdbm-1.8.3/lib

  $ export LIBRART_PATH

  可一次指定多個搜尋路徑,":"用於分隔它們,"."表示當前路徑,如:

  $ C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include

  $ LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib

  (可以新增多個路徑,路徑之間用:相隔,.代表當前目錄,若.在最前頭,也可省略)

  當然,若想永久地新增這些路徑,可以在.bash_profile中新增上述語句.

   例如,假設存放原始檔的子目錄名為./src,而包含檔案則放在同層次的其他目錄下,如./inc。當我們在./src目錄下進行編譯工作時,如何告訴GCC到哪裡找標頭檔案呢?方法如下所示:
         $ gcc test.c  ,I../inc -o test
  上面的命令告訴GCC包含檔案存放在./inc目錄下,在當前目錄的上一級。如果在編譯時需要的包含檔案存放在多個目錄下,可以使用多個-I 來指定各個目錄:
       包含多個目錄: $ gcc test.c -I../inc -I../../inc2 -o test
    這裡指出了另一個包含子目錄inc2,較之前目錄它還要在再上兩級才能找到。

    C編譯器通過原始檔的字尾名來判斷是 C 程式還是 C++ 程式。在 Linux 中,C 原始檔的字尾名為 .c,而 C++原始檔的字尾名為 .C 或 .cpp。但是,gcc 命令只能編譯 C++ 原始檔,而不能自動和 C++程式使用的庫連線。因此,通常使用 g++ 命令來完成C++ 程式的編譯和連線,該程式會自動呼叫 gcc 實現編譯。

     -g生成除錯資訊。GNU 偵錯程式可利用該資訊。

    -w 不生成任何警告資訊。
    -Wall 生成所有警告資訊。

    -c只編譯並生成目標檔案

    the -c option says not  to run thelinker.  Then the output consists of object filesoutput by  the assembler.

 -c      Compile or assemble the source files, but do notlink.  The link-
          ing stage simply is not done.  The ultimate outputis in the form
          of an object file for each source file.

          By default, the object file name for a source file is made by
          replacing the suffix .c, .i, .s, etc., with .o.

          Unrecognized input files, not requiring compilation orassembly,
          are ignored.

  -S    Stop after the stage of compilation proper; do notassemble.  The
          output is in the form of an assembler code file for each non-
          assembler input file specified.

          By default, the assembler file name for a source file is madeby
          replacing the suffix .c, .i, etc., with .s.

          Input files that don\u2019t require compilation are ignored.

  -E    Stop after the preprocessing stage; do not run the compiler
          proper.  The output is in the form of preprocessedsource code,
          which is sent to the standard output.

          Input files which don't require preprocessing are ignored.


 -v      Print (on standard error output) the commandsexecuted to run the
          stages of compilation.  Also print the versionnumber of the com-
          piler driver program and of the preprocessor and the compiler
          proper.

 --version
          Display the version number and copyrights of the invoked GCC.

  -###
          Like -v except the commands are not executed and all commandargu-
          ments are quoted.  This is useful for shellscripts to capture the
          driver-generated command lines.

  -pipe
          Use pipes rather than temporary files for communicationbetween
          the various stages of compilation.  This fails towork on some
          systems where the assembler is unable to read from a pipe; butthe
          GNU assembler has no trouble.

優化選項:

    OptionsThat Control Optimization

      Theseoptions control various sorts ofoptimizations. Without any optimization option,the compiler's goal is to reduce the cost of compilation and tomake debugging produce the expectedresults.  Statements are independent: if you stopthe program with a  breakpoint betweenstatements(計算機程式指令或語句), you can then assign a new value to anyvariable or change the program  counter to any other statement in the function and get exactly theresults you would expect from the source code.

     Turningon optimization flags makes the compiler attempt to improve theperformance and/or code size at the expense ofcompilation time and possibly the ability to debug the program.

      The compiler performs optimization based on the knowledge it has ofthe program.  Usingthe -funit-at-a-time flag will allow the compilerto consider information gained from later functions in the filewhen compiling a function.  Compiling multiplefiles at once to a single output file (and using -funit-at-a-time)will allow the compiler to use information gained from all of thefiles when compiling each of them.

      Not all optimizations are controlled directly by aflag.  Only optimizations that have a flagare
      listed.

      -O
      -O1 Optimize.  Optimizing compilation takessomewhat more time, and a lot more memory for alarge  function.

     With -O, the compiler tries to reduce code size and execution time,without performingany   optimizations that take a great deal of compilation time.-O turnson the following optimization flags: -fdefer-pop -fmerge-constants-fthread-jumps -floop-optimize -fif-conversion-fif-conversion2 -fdelayed-branch-fguess-branch-probability-fcprop-registers  -Oalso turns on -fomit-frame-pointer on machines where doing so doesnot interfere with debugging.

          -O2 Optimize even more.  GCC performs nearly allsupported optimizations that do not involve a space-speedtradeoff.  The compiler does not perform loopunrolling or function inlining when you specify-O2.  As compared to -O, this option increasesboth compilation time and the performance of  thegenerated code.
     -O2 turns on all optimization flags specified by-O.  It also turns on the following optimizationflags: -fforce-mem -foptimize-sibling-calls -fstrength-reduce-fcse-follow-jumps
 -fcse-skip-blocks-frerun-cse-after-loop  -frerun-loop-opt-fgcse  -fgcse-lm -fgcse-sm  -fgcse-las -fdelete-null-pointer-checks-fexpensive-optimizations -fregmove -fschedule-insns
-fschedule-insns2 -fsched-interblock  -fsched-spec-fcaller-saves -fpeephole2 -freorder-blocks
-freorder-functions -fstrict-aliasing -funit-at-a-time-falign-functions -falign-jumps-falign-loops  -falign-labels-fcrossjumping

     Please note the warning under -fgcse about invoking -O2 on programsthat use computed gotos.

    -O3Optimize yet more.  -O3 turns on all optimizationsspecified by -O2 and also turns on the -fin-line-functions, -fweb,-frename-registers and -funswitch-loops options.

      -O0 Do not optimize.  This is the default.

      -Os Optimize for size.  -Os enables all -O2optimizations that do not typically increase code size.
       It also performs further optimizations designed to reduce codesize.

     -Os disables the following optimization flags:-falign-functions  -falign-jumps -falign-loops  -falign-labels -freorder-blocks  -fprefetch-loop-arrays

    Ifyou use multiple -O options, with or without level numbers, thelast such option is the one
that is effective.

      Optionsof the form -fflag specify machine-independentflags.  Most flags have both positive and negativeforms; the negative form of -ffoo would be-fno-foo.  In the table below, only one of theforms is listed---the one you typically will use. You can figure out the other form by either removing no or addingit.

     Thefollowing options control specific optimizations. They are either activated by -O options or are related to ones thatare.  You can use the following flags in the rarecases when "fine turning"of optimizations to be performed is desired.

   -fno-default-inline
    Donot make member functions inline by default merely because they aredefined inside the class scope (C++only).  Otherwise, when you specify -O, memberfunctions defined inside class scopeare compiled inline by default;i.e., you don\u2019t need to add inline in front of the memberfunction name.
 

   關於編譯選項的次序: Order does matter when you useseveral options of the same kind; for example, ifyou specify -L more than once, the directories are searched in theorder specified.

  Overall Options
          -c  -S  -E  -ofile  -pipe  -pass-exit-codes -xlanguage  -v  -###
          --help  --target-help --version

 C Language Options
          -ansi  -std=standard  -aux-infofilename -fno-asm  -fno-builtin
          -fno-builtin-function -fhosted -ffreestanding  -fms-extensions
          -trigraphs  -no-integrated-cpp -traditional  -traditional-cpp
          -fallow-single-precision  -fcond-mismatch-fsigned-bitfields
          -fsigned-char -funsigned-bitfields -funsigned-char
          -fwritable-strings

 C++ Language Options
          -fabi-version=n -fno-access-control  -fcheck-new-fconserve-space
          -fno-const-strings -fno-elide-constructors-fno-enforce-eh-specs
          -ffor-scope  -fno-for-scope -fno-gnu-keywords -fno-implicit-tem-
          plates -fno-implicit-inline-templates -fno-implement-inlines
          -fms-extensions -fno-nonansi-builtins -fno-operator-names
          -fno-optional-diags  -fpermissive-frepo  -fno-rtti  -fstats
          -ftemplate-depth-n -fno-threadsafe-statics -fuse-cxa-atexit
          -fno-weak  -nostdinc++-fno-default-inline  -fvisibil-
          ity-inlines-hidden -Wabi  -Wctor-dtor-privacy-Wnon-virtual-dtor
          -Wreorder -Weffc++  -Wno-deprecated-Wno-non-template-friend
          -Wold-style-cast -Woverloaded-virtual -Wno-pmf-conversions
          -Wsign-promo
 

下面的是一個呼叫數學庫 libm.asin函式的的例子,建立檔案calc.c

    #include<math.h>

    #include<stdio.h>

    int main(void)

   {

    double x= 2.0;

    double y= sin (x);

    printf("The value of sin(2.0) is %f\n", y);

    return0;

  }

嘗試單獨從該檔案生成一個可執行檔案將導致一個連結階段的錯誤:函式sin,未在本程式中定義也不在預設庫‘libc.a’中;

    gcc -Wallcalc.c /usr/lib/libm.a -o calc

為避免在命令列中指定長長的路徑,編譯器為連結函式庫提供了快捷的選項‘-l’。例如,下面的命令      $gcc -Wall calc.c -lm -o calc

與我們上面指定庫全路徑‘/usr/lib/libm.a’的命令等價。

程式通常要使用很多 -l 選項來指定要連結的數學庫,圖形庫,網路庫等。

1)在實現共享庫時,要將原始檔編譯為相對地址編碼的格式。
2)Gcc選項 -fpic是實現1)中要求的選項。pic是position independent code的縮寫。如:gcc -c-fpic component1.c component2.c

3)在連結時定位共享庫:用-L指定絕對路徑,也可以是用-l指定相對路徑。

   預設搜尋路徑是/lib和/usr/lib

4)在執行時,應用程式的搜尋路徑包括:環境變數LD_LIBRARY_PATH指定的路徑、/etc/ld.so.cache中的共享庫(由ldconfig生成)、/lib和/usr/lib。另外還有一個環境變數LD_PRELOAD,在這裡定義的共享庫會比任何前述路徑優先搜尋。

   gdb除錯時:檢視資料

       print variable       檢視變數

       print *[email protected]     檢視陣列(array是陣列指標,len是需要資料長度)

       可以通過新增引數來設定輸出格式:

           /x 按十六進位制格式顯示變數。

           /d 按十進位制格式顯示變數。

           /u 按十六進位制格式顯示無符號整型。

           /o 按八進位制格式顯示變數。

           /t 按二進位制格式顯示變數。

           /a 按十六進位制格式顯示變數。

           /c 按字元格式顯示變數。

           /f 按浮數格式顯示變數。

相關推薦

gcc編譯連結時檔案和庫檔案的搜尋順序

編譯:找符號定義 連結:找實現 執行:執行 靜態庫連結時直接寫程序序裡了 動態庫連結時只連結到了一些地址資訊,需要到執行時再進行動態載入 編譯時搜尋標頭檔案的順序: 1.  gcc先找-I設定的路徑 2.  再找gcc的環境變數C_INCLUDE_PATH, CPLU

GCC編譯選項-包含檔案

  許多情況下,標頭檔案和原始檔會單獨存放在不同的目錄中。 可以直接在.c檔案中利用#include“/path/file.h", 通過指定標頭檔案的路徑(可以是絕對路徑,也可以是相對路徑)來包含標頭檔案. 但這明顯降低了程式的可移植性. 在別的系統環境下編譯可能會出現問題

idea使用javah一鍵編譯JNI的.檔案

.h標頭檔案中方法命名規範需要用到包名,所以,使用javah編譯jni時工作目錄一定要是在包名父目錄中,否則Exception in thread “main” java.lang.IllegalArgumentException: Not a valid class name:

VScode編譯C++,檔案顯示not found的解決方法

一直用codeblocks,想試試vscode,結果這個問題給我弄懵逼了。一開始以為是iostream這個標頭檔案not found,後來發現第一個標頭檔案都會這樣顯示,放到後面就不會了,然而,光這一個顯示not found(雖然並不影響編譯),就能逼死強迫症的啊~~~ 言歸正傳,這個問題解

c語言編譯過程和檔案<>與""的區別

編譯過程:   預處理--編譯--彙編--連結 預處理:用於將所有#include標頭檔案及#define等巨集定義替換成真正的內容,預處理後的得到的仍然是文字檔案,但體積會大很多。 編譯:將預處理之後的程式轉換成特定彙編程式碼的過程

Javac編譯生成.h檔案

首先分兩種情況: 1、新建Java工程 ①編譯成.class檔案,重新整理工程編譯或者工程右鍵點選Build Project編譯。(在bin下生成.class檔案) ②在命令列cd 到bin目錄下,執行:javah -classpath . -jni com.android

opencv包含檔案出錯的粗略解讀

Opencv新手遇到的問題很多問題之一就是  #include opencv標頭檔案的時候經常會出錯。程式明明就很簡單,一個稍懂opencv的人都知道程式沒有錯誤,但是一編譯就是一大堆錯誤,就是找不到標頭檔案。 fatal error C1083: 無法開啟包括檔案:“cv

gcc新增自寫檔案的問題解決

一.標頭檔案編譯出錯問題     1 有時候我們在編寫程式,特別是多個程式時,有時候我們需要寫自己的標頭檔案,有時候我們用gcc編譯時會出錯,因為編譯器不會自動的幫你新增標頭檔案,此時你需要手動新增自己寫的標頭檔案。      2我們編譯的時候會出現為定義的問題 3.這時

C++ 在.h檔案包含檔案和在.cpp檔案包含檔案的原則

1、 第一個原則:如果可以不包含標頭檔案,那就不要包含了,這時候前置宣告可以解決問題。如果使用的僅僅是一個類的指標,沒有使用這個類的具體物件(非指標),也沒有訪問到類的具體成員,那麼前置宣告就可以了。因為指標這一資料型別的大小是特定的,編譯器可以獲知(C++編譯

Idl檔案編譯成c++檔案

如何使用本機上的一個activeobject呢,首先用vs自帶的ole/com object viewer檢視你想要的object。   生成出idl檔案。那麼對於c/c++開發的人員應該怎麼使用這個東西呢。首 先我們需要要idl檔案轉換成.h檔案,你可以使用midl命令生

解決KinectSDK 包含檔案"NuiApi.h"報錯問題

C:\Program Files\Microsoft SDKs\Kinect\v1.7\inc\NuiSensor.h(46): error C2146: 語法錯誤: 缺少“;”(在識別符號“INuiAudioBeam”的前面) 1>C:\Program File

對類前置宣告和包含檔案的一點理解(類的交叉引用)

//Layer.h// 圖層類#pragma once #include "Symbol.h"class CLayer {public:     CLayer(void);     virtual~CLayer(void);     void CreateNewSymbol();private:     

gcc和交叉編譯檔案包含問題

eclipse開發環境下,同一個工程,使用gcc成功編譯,但是使用交叉編譯,提示找不到標頭檔案,這是因為兩者預設的標頭檔案包含路徑不一樣。 在LINUX程式設計當中,經常會遇到標頭檔案包含的問題,那麼這些標頭檔案到底在哪個路徑下?具體的標頭檔案路徑依賴於程

windows下使用gcc完成檔案和目標檔案編譯

環境要求 安裝了gcc  win+r然後輸入cmd , dos介面輸入 gcc -v 檢視有沒有安裝gcc   進入正題 新建 text.c檔案鍵入如下程式碼: #include <stdio.h> #include ".\\head.h" int main(

檔案多次包含編譯出錯

在編譯一個工程時,有多個c檔案包含同一個.h檔案 比如a.c,b.c都包含c.h(#ifndef DH #idefine DH ... #endif) 當只有a.c包含c.h時,工程沒有問題 但是a.c和b.c同時包含c.h時,工程出現了很多問題。 首先排除重複編譯的問題

gcc -l引數和-L引數(編譯檔案路徑和庫檔案路徑的設定相關)

放在/lib和/usr/lib和/usr/local/lib裡的庫直接用-l引數就能連結了,但如果庫檔案沒放在這三個目錄裡,而是放在其他目錄裡,這時我們只用-l引數的話,連結還是會出錯,出錯資訊大概是:“/usr/bin/ld: cannot find -lxxx”,也就是連結程式ld在那3個目錄裡找不到l

0420 測試記錄 gcc 編譯時 庫檔案 檔案問題及其解決方案

[[email protected] c]# g++ -L/usr/local/lib -I/usr/local/includes -o morphology morphology.cmorphology.c:1:16: 錯誤:cv.h:沒有那個檔案或目錄morph

linux-gcc 編譯檔案和庫檔案搜尋路徑

一、標頭檔案    gcc 在編譯時尋找所需要的標頭檔案 :    ※搜尋會從-I開始    ※然後找gcc的環境變數 C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PATH    ※再找內定目錄 /usr/include  /usr/local/incl

GCC:預編譯檔案的嘗試

    公司開發了一個自己的基礎類庫,有著龐大的標頭檔案。為了便於使用並提高編譯速度,嘗試了一下GCC的預編譯標頭檔案的功能:1、先定義標頭檔案afl.h,內容如下:(afl意味a framework library) #ifndef _AFL_H_#define _AFL_

如何獲得gcc/g++編譯巨集定義和檔案搜尋目錄的方法說明

/* co-gcc.lnt: This is the seed file for configuring Lint for use with GCC versions 2.95.3 and later. Like all compiler options files thi