gcc查看引用了哪些头文件|linux下编写c++include的那些头文件在什么地方

|

① gcc去哪找头文件

gcc默认会到/usr/include下面去找头文件,那就使用绝对路径吧!利用系统的环境变量。对于头文件的搜索路径:C_INCLUDE_PATH=<your include path;export C_INCLUDE_PATH对于库文件的搜索路径:LIBRARY_PATH=<your lib path;export LIBRARY_PATH对于链接程序ld使用的库文件搜索路径:LD_LIBRARY_PATH=<your ldlib path;export LD_LIBRARY_PATHgcc -I/usr/local/headers/ld:ld –verbose | grep SEARCH你echo $PATH,看看/usr/local/include是不是放在/usr/include前面了。PATH环境变量只是可执行程序的查找路径吧?gcc的include好象跟这个没关系

② 如何让linux-gcc找到自定义的头文件

要么就使用绝对路径, 要么就使用工程. 如:#include "c:\foo\foo.h" 使用工程的话就是新建一个工程, 工程建好之后会有source files, header files, other files三个目录, 你用右键把头文件添加到header files里边即可 工程的话实际上也是使用了绝对路径, 只是工程文件帮你做了文件的搜寻工作. 如果是在linux下使用GCC编译器的话,把头文件放在当前目录也可以找到的

③ gcc 在编译时如何去寻找所需要的头文件

当我们给$ gcc -o foo.o foo.cgcc怎么知道去哪里找foo.c里面所include的header文件,连结数据库与系统定义呢? 总共有下列来源指定gcc去那找。当初在编译时指定的(在~gcc/gcc/collect2.c:locatelib()写在specs内的后来用-D -I -L指定的gcc环境变量设定(编译的时候)ld.so的环境变量(这是run time的时候)在prefix/lib/gcc-lib/xxxx-xxx-xxx-gnulibc/2.9.5/里面有个很重要的specs这个档案 gcc根据这个档,做一些内定的动作。 通常系统上的specs内定装起来是在/usr/lib/gcc-lib/xxxx-gnulibc/version/specs档看起来是像这样*asm:%{v:-V} %{Qy:} %{!Qn:-Qy} %{n} %{T} %{Ym,*} %{Yd,*} %{Wa,*:%*} *asm_final:%| *cpp:%(cpp_cpu) %{fPIC:-D__PIC__ -D__pic__} %{fpic:-D__PIC__ -D__pic__} %{posix:-D_POSIX_SOURCE} %{pthread:-D_REENTRANT} *cc1:%(cc1_cpu) %{profile:-p} *cc1plus:*endfile:%{!shared:crtend.o%s} %{shared:crtendS.o%s} crtn.o%s *link:-m elf_i386 %{shared:-shared} %{!shared: %{!ibcs: %{!static: %{rdynamic:-export-dynamic} %{!dynamic-linker:-dynamic-linker/lib/ld-linux.so.2}} %{static:-static}}} *lib:%{shared: -lc –version-script libgcc.map%s} %{!shared: %{mieee-fp:-lieee}%{pthread:-lpthread} %{profile:-lc_p} %{!profile: -lc}} *libgcc:-lgcc *startfile:%{!shared: %{pg:gcrt1.o%s} %{!pg:%{p:gcrt1.o%s} %{!p:%{profile:gcrt1.o%s}%{!profile:crt1.o%s}}}} crti.o%s %{!shared:crtbegin.o%s}%{shared:crtbeginS.o%s} *switches_need_spaces: *signed_char:%{funsigned-char:-D__CHAR_UNSIGNED__} *predefines:-D__ELF__ -Dunix -Di386 -D__i386__ -Dlinux -Asystem(posix) *cross_compile:0 *version:egcs-2.91.66 *multilib:. ; *multilib_defaults: *multilib_extra: *multilib_matches: *linker:collect2 *cpp_cpu_default:-D__tune_i386__ *cpp_cpu:-Asystem(unix) -Acpu(i386) -Amachine(i386) %{!ansi:-Di386}-D__i386 -D__i386__ %{march=i486:-D__i486 -D__i486__}%{march=pentium|march=i586:-D__pentium -D__pentium__ }%{march=pentiumpro|march=i686:-D__pentiumpro -D__pentiumpro__ }%{m386|mcpu=i386:-D__tune_i386__ } %{m486|mcpu=i486:-D__tune_i486__ }%{mpentium|mcpu=pentium|mcpu=i586:-D__tune_pentium__ }%{mpentiumpro|mcpu=pentiumpro|mcpu=i686:-D__tune_pentiumpro__ }%{!mcpu*:%{!m386:%{!m486:%{!mpentium*:%(cpp_cpu_default)}}}} *cc1_cpu:%{!mcpu*: %{m386:-mcpu=i386} %{mno-486:-mcpu=i386 -march=i386}%{m486:-mcpu=i486} %{mno-386:-mcpu=i486 -march=i486}%{mno-pentium:-mcpu=i486 -march=i486} %{mpentium:-mcpu=pentium}%{mno-pentiumpro:-mcpu=pentium} %{mpentiumpro:-mcpu=pentiumpro}}在shell下用这行,-E 表示只做到preprocess就好$ echo 'main(){}' | gcc -E -v -你会看到gcc去读specs档Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.2/specsgcc version 2.95.2 20000220 (Debian GNU/Linux) /usr/lib/gcc-lib/i386-linux/2.95.2/cpp -lang-c -v -D__GNUC__=2 -D__GNUC_MINOR__=95 -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux -Asystem(posix) -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -GNU CPP version 2.95.2 20000220 (Debian GNU/Linux) (i386 Linux/ELF)#include "…" search starts here:#include <…> search starts here: /usr/local/include /usr/lib/gcc-lib/i386-linux/2.95.2/include /usr/includeEnd of search list.The following default directories have been omitted from the search path: /usr/lib/gcc-lib/i386-linux/2.95.2/../../../../include/g++-3 /usr/lib/gcc-lib/i386-linux/2.95.2/../../../../i386-linux/includeEnd of omitted list.# 1 ""main(){}所以有内定的定义,(就是用在#if defined #ifndef #define这些东西, 如果有定义这个字符串,就去编译等等。) -Dxxxx -Dxxxx -Axxxx。 还有内定的include文件的搜寻路径/usr/include/usr/local/include/usr/lib/gcc-lib/i386-linux/2.95.2/include/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../include/g++-3/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../i386-linux/include但是如果装gcc的时候,是有给定的prefix的话,那么就是/usr/includeprefix/includeprefix/xxx-xxx-xxx-gnulibc/includeprefix/lib/gcc-lib/xxxx-xxx-xxx-gnulibc/2.8.1/include所以header file的搜寻会从-I开始然后找gcc的环境变量 C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PATH 再找上述的内定目录函式库当我们用到数学函式cos(),cos这个symbol,gcc并不晓它到底是什么东西, 是变量,是函式,要预留多少空间给他等等,完全没有任何讯息,你必须标头 档要#include ,gcc才知道。而且因为specs这个档里面只有要 link -lc也就是只有libc.so这个档内的symbol会被搜寻, 像printf scanf等都在这里面,可是像cos()等就没有了, 所以函式库的选项要多加 -lm ,这时ld才会来找libm这个函式库,编译的时候,gcc会去找-L,再找gcc的环境变量LIBRARY_PATH,再找内定目录 /lib /usr/lib /usr/local/lib 这是当初compile gcc时写在程序内的, gcc环境变量与pass给ld的机制在~gcc/gcc/collect2.c下找得到。 这上面只是搜寻路径而已,如果要不加-lm 也能正确的主动搜寻某个特定的lib,例如libm, 就要去在specs这个档案改一下,把math这个函式库加进自动联结函式库 之一。就不用写-lm了。RUN TIME的时候, 如果编译时没有指定-static这个选项,其实可执行文件并不是真的可执行, 它必须在执行(run time)时需要ld.so来做最后的连结动作,建造一个可执行的 image丢到内存。如果是静态连结,编译时ld会去找libm.a的档 。如果是动态连结去找libm.so。 所以每次有新改版程序, 或新加动态函式库如果不在原本的/etc/ld.so.conf搜寻路径中,都要把路径 加进来,然后用ldconfig -v会重建cache并且显示它所参照的函式库。Run Time时ld.so才找得到lib"执行"。 ld与ld.so不一样喔。一些重要的程序ld :Link Editor 连结各obj写进一个可执行档(executable)。ldd :秀出一个执行文件用了那些动态函式库。ld.so :Dynamic Linker, 动态连结的话,是由ld.so完成执行时期symbol的 :参照与连结。ld-linux.so :ELF文件的动态连结,跟ld.so一样。只是ld.so是给a.out format的。 :新的glicb2的ld-linux.so.2已经跟ld.so.2结合成单一程序了。ldconfig :根据/etc/ld.so.conf内的目录,做出动态连结所需的cache档。ld 就是负责各个函式库文件的信息写进最后可执行档(executable),所以它叫做 link editor,编译时根据flags -L搜寻需要的lib,gcc也会把他的设定pass下来。 ld.so ld-linux.so.2是负责最后动态连结,叫做dynamic linker, RUN Time 执行程序时,它根据这个顺序搜寻函式库。LD_LIBRARY_PATH 或LD_AOUT_LIBRARY_PATH环境变量所指的路径ldconfig所建立的cache/lib /usr/lib内的档来找程序所需要的动态函式库ldconfig会根据/etc/ld.so.conf这个档的设定,加上内定的两个目录 /lib /usr/lib来设定ld.so要用到所需要的连结 以及连结的cache到/etc/ld.so.cache。 所以如果换了新的函式库,新的kernel,内部的标头档可能会有变化, 都要跟着改变让gcc正确的找到,喔不,应该是cpp, ld, ld.so能正确的找到。 不然编出来的执行档可能是错误的,执行时还可能segmentation fault。

④ linux下编写c++,include的那些头文件在什么地方

C/C++程序在linux下被编译和连接时,GCC/G++会查找系统默认的include和link的路径,以及自己在编译命令中指定的路径。

1、#include <stdio.h>,直接到系统指定目录去查找头文件。

系统默认路径为:/usr/include,/usr/local/include,/usr/lib/gcc-lib/i386-Linux/2.95.2/include(gcc库文件的路径,各个系统不一致)

2、#include "stidio.h",会先到当前目录查找头文件,如果没找到在到系统指定目录查找。

3、gcc编译时查找头文件,按照以下路径顺序查找:

gcc编译时,可以设置-I选项以指定头文件的搜索路径,如果指定多个路径,则按照顺序依次查找。比如,gcc -I /usr/local/include/node a.c

gcc会查找环境变量C_INCLUDE_PATH,CPLUS_INCLUDE_PATH中指定的路径。

(4)gcc查看引用了哪些头文件扩展阅读:

应用程序代码编译过程:

编译器根据头文件提供的库函数接口形式,来编译代码,然后生成目标文件;然后,再使用链接器将这个目标文件与系统库链接;最终生成应用程序。代码包含了自己写的内容,还有系统提供好的现成的库函数,整个结合起来才形成一个完整的程序。

库函数的头文件,在编译的时候被使用,而库函数的代码段(库文件),在链接的时候被使用。

example:

应用程序代码在使用一个系统调用的时候,例如printf()函数,需要指定包含的头文件stdio.h;另外,在链接的时候对应的链接libc.a(笔者电脑文件所在目录:/usr/lib/i386-linux-gnu/libc.a)。

总结一下,编写应用程序,需要使用linux系统提供的库函数。具体实现起来,需要头文件和库文件。头文件是需要我们编写应用程序的时候,在源文件开头添加的;而库文件则需要配置编译环境进行指定搜索目录。

⑤ gcc交叉编译怎么找头文件及lib库的

是在specs里面读取的路径信息。命令行中键入 gcc -vReading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specsConfigured with: /usr/build/package/orig/test.respin/gcc-3.4.4-3/configure –verbose –prefix=/usr –exec-prefix=/usr –sysconfdir=/etc –libdir=/usr/lib –libexecdir=/usr/lib –mandir=/usr/share/man –infodir=/usr/share/info –enable-languages=c,ada,c++,d,f77,pascal,java,objc –enable-nls –without-included-gettext –enable-version-specific-runtime-libs –without-x –enable-libgcj –disable-java-awt –with-system-zlib –enable-interpreter –disable-libgcj-debug –enable-threads=posix –enable-java-gc=boehm –disable-win32-registry –enable-sjlj-exceptions –enable-hash-synchronization –enable-libstdcxx-debugThread model: posixgcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)注意“–prefix=/usr” 以及“–libdir=/usr/lib ”表示gcc ld as 等可执行文件安装在/usr/bin,而libc.a 等文件是在/usr/lib中。解压缩交叉编译器时,也是要解压缩在在–prefix 指定的目录下。比如 下载了arm-linux 的交叉编译器cross-3.3.2.tar.bz2,解压缩之后,运行 arm-linux-gcc -v得到 –prefix=/usr/local/arm。那么就要把 bin lib 等所有的文件和文件夹到/usr/local/arm目录下。否则到时候运行arm-linux-gcc hello.c会提示找不到stdio.h 或者 lib.so.6 等HOWTO Use the GCC specs fileAbout Specs fileThe "gcc" program invoked by users is a convenient front-end driver executable which will invoke other programs in the background such as cc1, as or ld to do its work according to the command line parameter given. A specs file is plain text used to control the default behavior for the "gcc" front-end. The specs file is usually built-in but for flexibility purposes, it can be overridden with an external version.Basic Specs file modificationsCC will proce a specs file via the following command.gcc -mpspecs > specsYou may use a text editor of your choice to inspect it. It may be confusing at first, but there are many places of interest. To use the specs file, invoke gcc with -specs= or place it at "/mingw/lib/gcc/mingw32//specs" to make GCC use it by default, where refers to the GCC version installed.Adding include directories to the search path& #160;he *cpp: section should be modified. It contains the following by default:*cpp:%{posix:-D_POSIX_SOURCE} %{mthreads:-D_MT}If "z:\libx\include" needs to be added to the GCC includes search path, it should be changed to the following*cpp:%{posix:-D_POSIX_SOURCE} %{mthreads:-D_MT} -I/z/libx/includeAdding lib directories to the search path& #160;he *link_libgcc: section should be modified. It contains the following by default:*link_libgcc:%D& #160;f "z:\libx\lib" needs to be added to the GCC library search path, it should be changed to the following*link_libgcc:%D -L/z/libx/lib


赞 (0)