一:查看gcc/g++默认include路径
1.`gcc -print-prog-name=cc1plus` -v
2.`g++ -print-prog-name=cc1plus` -v
3.echo 'main(){}' | gcc -E -v -
二:include搜索路径
1.当前目录
2.编译的时候指定的路径,比如--prefix=/usr/local,查找的时候去/usr/local/include
3.gcc的specs里,(Command Options),这个不清楚怎么用了。
4.使用-I参数指定的路径,比如g++ demo.cpp -I../include/demo.h
这几个路径中-I参数指定的路径优先级最高。
在gcc的手册里是这么说的:
These options specify directories to search for header files, for libraries and for parts
of the compiler:
-Idir
Add the directory dir to the head of the list of directories to be searched for header fi
les. This can be used to override a system header file, substituting your own version, si
nce these directories are searched before the system header file directories. However, yo
u should not use this option to add directories that contain vendor-supplied system heade
r files (use -isystem for that). If you use more than one -I option, the directories are
scanned in left-to-right order; the standard system directories come after.
If a standard system include directory, or a directory specified with -isystem, is also
specified with -I, the -I option is ignored. The directory is still searched but as a sy
stem directory at its normal position in the system include chain. This is to ensure tha
t GCC's procedure to fix buggy system headers and the ordering for the include_next dire
ctive are not inadvertently changed. If you really need to change the search order for s
ystem directories, use the -nostdinc and/or -isystem options.
-iplugindir=dir
Set the directory to search for plugins that are passed by -fplugin=name instead of -fpl
ugin=path/name.so. This option is not meant to be used by the user, but only passed by t
he driver.
-iquotedir
Add the directory dir to the head of the list of directories to be searched for header f
iles only for the case of #include "file"; they are not searched for #include <file>, ot
herwise just like -I.
-Ldir
Add directory dir to the list of directories to be searched for -l.
-Bprefix
This option specifies where to find the executables, libraries, include files, and data
files of the compiler itself.
The compiler driver program runs one or more of the subprograms cpp, cc1, as and ld. It
tries prefix as a prefix for each program it tries to run, both with and without ‘mach
ine/version/’ (see Target Options).
For each subprogram to be run, the compiler driver first tries the -B prefix, if any. I
f that name is not found, or if -B is not specified, the driver tries two standard pref
ixes, /usr/lib/gcc/ and /usr/local/lib/gcc/. If neither of those results in a file name
that is found, the unmodified program name is searched for using the directories speci
fied in your PATH environment variable.
The compiler checks to see if the path provided by -B refers to a directory, and if nec
essary it adds a directory separator character at the end of the path.
-B prefixes that effectively specify directory names also apply to libraries in the lin
ker, because the compiler translates these options into -L options for the linker. They
also apply to include files in the preprocessor, because the compiler translates these
options into -isystem options for the preprocessor. In this case, the compiler appends
‘include’ to the prefix.
The runtime support file libgcc.a can also be searched for using the -B prefix, if need
ed. If it is not found there, the two standard prefixes above are tried, and that is al
l. The file is left out of the link if it is not found by those means.
Another way to specify a prefix much like the -B prefix is to use the environment varia
ble GCC_EXEC_PREFIX. See Environment Variables.
As a special kludge, if the path provided by -B is [dir/]stageN/, where N is a number i
n the range 0 to 9, then it is replaced by [dir/]include. This is to help with boot-str
apping the compiler.
-specs=file
Process file after the compiler reads in the standard specs file, in order to override
the defaults which the gcc driver program uses when determining what switches to pass
to cc1, cc1plus, as, ld, etc. More than one -specs=file can be specified on the comman
d line, and they are processed in order, from left to right.
--sysroot=dir
Use dir as the logical root directory for headers and libraries. For example, if the c
ompiler normally searches for headers in /usr/include and libraries in /usr/lib, it in
stead searches dir/usr/include and dir/usr/lib.
If you use both this option and the -isysroot option, then the --sysroot option applie
s to libraries, but the -isysroot option applies to header files.
The GNU linker (beginning with version 2.16) has the necessary support for this option
. If your linker does not support this option, the header file aspect of --sysroot sti
ll works, but the library aspect does not.
--no-sysroot-suffix
For some targets, a suffix is added to the root directory specified with --sysroot, de
pending on the other options used, so that headers may for example be found in dir/suf
fix/usr/include instead of dir/usr/include. This option disables the addition of such
a suffix.
-I-
This option has been deprecated. Please use -iquote instead for -I directories before
the -I- and remove the -I- option. Any directories you specify with -I options before
the -I- option are searched only for the case of #include "file"; they are not search
ed for #include <file>.
If additional directories are specified with -I options after the -I- option, these d
irectories are searched for all #include directives. (Ordinarily all -I directories a
re used this way.)
In addition, the -I- option inhibits the use of the current directory (where the curr
ent input file came from) as the first search directory for #include "file". There is
no way to override this effect of -I-. With -I. you can specify searching the direct
ory that is current when the compiler is invoked. That is not exactly the same as wha
t the preprocessor does by default, but it is often satisfactory.
-I- does not inhibit the use of the standard system directories for header files. Thu
s, -I- and -nostdinc are independent.
5.gcc环境变量设置(
CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH)
6.系统标准include路径
GCC looks in several different places for headers. On a normal Unix system, if you
do not instruct it otherwise, it will look for headers requested with #include <file> in:
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
For C++ programs, it will also look in libdir/../include/c++/version, first.
In the above, target is the canonical name of the system GCC was configured to compile
code for; often but not always the same as the canonical name of the system it runs on.
version is the version of GCC in use.
You can add to this list with the -Idir command-line option. All the directories named
by -I are searched, in left-to-right order, before the default directories. The only
exception is when dir is already searched by default. In this case, the option is ignored
and the search order for system directories remains unchanged.
Duplicate directories are removed from the quote and bracket search chains before the
two chains are merged to make the final search chain. Thus, it is possible for a directory
to occur twice in the final search chain if it was specified in both the quote and
bracket chains.
You can prevent GCC from searching any of the default directories with the -nostdinc
option. This is useful when you are compiling an operating system kernel or some other
program that does not use the standard C library facilities, or the standard C library
itself. -I options are not ignored as described above when -nostdinc is in effect.
GCC looks for headers requested with #include "file" first in the directory containing
the current file, then in the directories as specified by -iquote options, then in the
same places it would have looked for a header requested with angle brackets. For example,
if /usr/include/sys/stat.h contains #include "types.h", GCC looks for types.h first
in /usr/include/sys, then in its usual search path.
‘#line’ (see Line Control) does not change GCC's idea of the directory containing
the current file.
You may put -I- at any point in your list of -I options. This has two effects. First,
directories appearing before the -I- in the list are searched only for headers
requested with quote marks. Directories after -I- are searched for all headers.
Second, the directory containing the current file is not searched for anything,
unless it happens to be one of the directories named by an -I switch. -I- is deprecated,
-iquote should be used instead.
-I. -I- is not the same as no -I options at all, and does not cause the same behavior
for ‘<>’ includes that ‘""’ includes get with no special options. -I. searches the
compiler's current working directory for header files. That may or may not be the same
as the directory containing the current file.
If you need to look for headers in a directory named -, write -I./-.
There are several more ways to adjust the header search path. They are generally less
useful. See Invocation.
比如在我的系统上执行
`g++ -print-prog-name=cc1plus` -v
可以得到系统的标准路径:
ignoring nonexistent directory "/root/download/boost_1_59_0"
ignoring nonexistent directory "/root/download/asio-1.10.6/include/"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.3/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
.
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/x86_64-redhat-linux
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/backward
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/include
/usr/local/include
/usr/include
End of search list.
可以看到系统的标准include路径:
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
libdir/../include/c++/version
三:动态库的搜索路径搜索的先后顺序是:
1 编译目标代码时指定的动态库搜索路径;
2 环境变量LD_LIBRARY_PATH指定的动态库搜索路径;
3 配置文件/etc/ld.so.conf中指定的动态库搜索路径;
4 默认的动态库搜索路径/lib;
5 默认的动态库搜索路径/usr/lib。