1. 程式人生 > >gcc和arm-linux-gcc預設標頭檔案庫搜尋路徑

gcc和arm-linux-gcc預設標頭檔案庫搜尋路徑

LINUX程式設計當中,經常會遇到標頭檔案包含的問題,那麼這些標頭檔案到底在哪個路徑下?具體的標頭檔案路徑依賴於程式性質(應用程式還是和核心相關的程式)和編譯器。

機器環境如下:UBUNTU10.04LTS,自己另外安裝的LINUX核心原始碼目錄為/usr/src/kernel,交叉連結器arm-linux-gcc安裝目錄為/usr/lcoal/arm/2.95.3/

1.如果是應用程式,並且使用GCC進行普通編譯,如果編譯時沒有使用-I選項指定包含目錄的話,那麼預設的標頭檔案目錄在/usr/include下,可以在shell下輸入如下命令

    echo 'main(){}' | gcc -E -v -

看到如下輸出內容

Using built-in specs. 

Target: i486-linux-gnu 
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu 
Thread model: posix 
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=i486' 
/usr/lib/gcc/i486-linux-gnu/4.4.3/cc1 -E -quiet -v - -D_FORTIFY_SOURCE=2 -mtune=generic -march=i486 -fstack-protector 
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu" 
ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../i486-linux-gnu/include" 
ignoring nonexistent directory "/usr/include/i486-linux-gnu" 
#include "..." search starts here: 
#include <...> search starts here: 
/usr/local/include 
/usr/lib/gcc/i486-linux-gnu/4.4.3/include 
/usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed 
/usr/include 
End of search list. 
# 1 "" 
# 1 "" 
# 1 "" 
# 1 "" 
main(){} 
COMPILER_PATH=/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/ 
LIBRARY_PATH=/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../:/lib/:/usr/lib/:/usr/lib/i486-linux-gnu/ 
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=i486' 

這就是內定的include檔案搜尋路徑

#include "..." search starts here:

#include <...> search starts here: 

/usr/local/include 

/usr/lib/gcc/i486-linux-gnu/4.4.3/include 

/usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed 

/usr/include 

End of search list.

實際上,這些內定的include檔案搜尋路徑,是存放在specs檔案中。

2.如果是應用程式,採用交叉編譯,編譯時沒有使用-I選項指定包含目錄的話,那麼預設的標頭檔案搜尋路徑為/usr/local/arm/2.95.3/arm-linux/include,即交叉編譯器的安裝目錄下的include目錄,執行如下命令

echo 'main(){}' | arm-linux-gcc -E -v -

看到如下輸出內容

Reading specs from /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/specs 

gcc version 2.95.3 20010315 (release) 
/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_MINOR__=95 -Dunix -D__arm__ -Dlinux -D__ELF__ -D__unix__ -D__arm__ -D__linux__ -D__ELF__ -D__unix -D__linux -Asystem(unix) -Asystem(posix) -Acpu(arm) -Amachine(arm) -D__CHAR_UNSIGNED__ -D__ARM_ARCH_3__ -D__APCS_32__ - 
GNU CPP version 2.95.3 20010315 (release) (ARM GNU/Linux with ELF) 
#include "..." search starts here: 
#include <...> search starts here: 
/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/include 
/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/sys-include 
/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/include 
End of search list. 
The following default directories have been omitted from the search path: 
/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../include/g++-3 
End of omitted list. 
# 1 "" 
main(){} 

這就是內定的include檔案搜尋路徑

#include "..." search starts here:

#include <...> search starts here:

/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/include 

/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/sys-include 

/usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/include 

End of search list.

在採用交叉編譯時,應用程式所包含的標頭檔案是在交叉編譯器的安裝目錄下。

    3.如果是和核心相關的程式,比如驅動程式,那麼所包含的標頭檔案是在核心原始碼的安裝目錄下,即/usr/src/kernel/include,這時一般要在Makefile檔案中指定核心原始碼目錄。

    總結:

    1.gcc預設標頭檔案搜尋路徑其實就是安裝路徑

        gcc預設標頭檔案搜尋的是/usr/include       庫檔案:    /lib和/usr/lib

        注意/usr/local/include   和/usr/local/lib不是預設的搜尋路徑

        可以在編譯的時候新增-I /director設定搜尋路徑 (i大寫)

    2.arm-Linux-gcc預設搜尋路徑其實就是編譯器的安裝路徑

       我的標頭檔案搜尋路徑在/zjk/software/arm-linux-gcc4.4.3/opt/FriendlyARM/toolschain/4.4.3/lib/gcc/arm-none-linux-gnueabi/4.4.3/include

        庫檔案在:/zjk/software/arm-linux-gcc4.4.3/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/lib

        注意:標頭檔案不是4.4.3目錄下的include,而是lib中......的路徑

                    庫檔案不是4.4.3目錄下的lib,而是arm-none-linux-gnueabi下的lib

   3.可以在編譯的時候新增-l 設定預設搜尋路徑(L小寫)