gcc/g++搜索路径

2023-10-31

一:查看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.gccspecs,(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


三:动态库的搜索路径搜索的先后顺序是:

编译目标代码时指定的动态库搜索路径;

环境变量LD_LIBRARY_PATH指定的动态库搜索路径;

配置文件/etc/ld.so.conf中指定的动态库搜索路径;

默认的动态库搜索路径/lib

默认的动态库搜索路径/usr/lib

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

gcc/g++搜索路径 的相关文章

  • 在64位操作系统上以32位模式和64位模式编译ioctl函数的执行有什么不同?

    我有 64 位 Enterprise SuSE 11 我有一个应用程序 它打开 HIDRAW 设备并在其上操作 ioctl 函数以从该设备获取原始信息 如下所示 struct hidraw devinfo devinfo int fd op
  • GCC 和 -Wconversion

    让我们编译以下程序 int main uint16 t data 0 data uint16 t std round 3 14f return 0 with g Wconversion prog cpp 我们会得到warning conve
  • 使用 OpenMP 编译会导致内存泄漏

    根据 valgrind 的说法 使用 OpenMP 编译简单的 hello world 程序时可能会导致内存泄漏 这是没有意义的 因为 hello world 程序并没有有意使用任何 OpenMP 功能 假设下面的程序名为hi c并根据 g
  • 分析 ELF 部分和符号大小的工具

    我需要一种方法来分析 ARM 的 GCC 编译器的输出文件 我正在为裸机进行编译 并且我非常关心大小 我可以用arm none eabi objdump由交叉编译器提供 但如果存在用于此任务的工具 则解析输出并不是我渴望做的事情 您知道存在
  • g++ C++0x 枚举类编译器警告

    我一直在将可怕的 C 类型安全伪枚举重构为新的 C 0x 类型安全枚举 因为它们是way更具可读性 不管怎样 我在导出的类中使用它们 所以我明确地将它们标记为导出 enum class attribute visibility defaul
  • 这种对有效类型规则的使用是否严格遵守?

    C99和C11中的有效类型规则规定 没有声明类型的存储可以用任何类型写入 并且存储非字符类型的值将相应地设置存储的有效类型 抛开 INT MAX 可能小于 123456789 的事实不谈 以下代码对有效类型规则的使用是否严格符合 inclu
  • 代码块 - 使用大地址感知标志进行编译

    如何使用以下命令在 64 位系统上编译 32 位应用程序LARGE ADRESS AWARE使用代码块标记 我需要使用超过 2GB 的内存 应该是添加的情况 Wl large address aware到链接标志 我不使用 CodeBloc
  • GCC:数组类型具有不完整的元素类型

    我已经宣布了struct 我尝试传递这些结构的数组 以及double双精度数组和一个整数 到一个函数中 我得到一个 数组类型具有不完整的元素类型 当我编译它时来自 gcc 的消息 我在通过考试的过程中犯了什么错误struct到函数 type
  • 服务器端的 ASP.NET 等效项包括

    虽然服务器端包含的经典 ASP 方法在 ASP NET 中有效 但我的印象是它不是首选方法 我 应该 如何达到同样的效果 这就是我现在正在做的事情 您现在有许多选项可以提供这种效果 但方式不同 用户控件 ascx 母版页 master 服务
  • INT_MIN % -1 是否会产生未定义的行为?

    gcc 生成浮动代码 引发SIGFPE对于以下代码 include
  • 无法执行'x86_64-conda_cos6-linux-gnu-gcc':没有这样的文件或目录(pysam安装)

    我正在尝试安装 pysam 执行后 python path to pysam master setup py build 这个错误的产生是 unable to execute x86 64 conda cos6 linux gnu gcc
  • 如何编译GCC生成的asm?

    我正在玩一些汇编代码 有些事情困扰着我 我编译这个 include
  • arm-linux-gnueabi 编译器选项

    我在用 ARM Linux gnueabi gcc在 Linux 中为 ARM 处理器编译 C 程序 但是 我不确定它编译的默认 ARM 模式是什么 例如 对于 C 代码 test c unsigned int main return 0x
  • ELF动态符号表

    我有一个关于 ELF 动态符号表的问题 对于 FUNC 类型的符号 我注意到某些二进制文件中的值为 0 但在其他二进制文件中 它具有一些非零值 这两个二进制文件都是由 gcc 生成的 我想知道为什么会出现这种差异 有没有编译器选项来控制这个
  • 避免 gcc 函数序言开销?

    我最近遇到了很多 gcc 在 x86 上生成非常糟糕的代码的函数 它们都符合以下模式 if some condition do something really simple and return else something comple
  • 在GCC中添加父目录的包含路径

    我想将父目录中的文件包含在我正在处理的项目中 所有的头文件都在父目录中 有没有办法在命令行上使用 I 来搜索父目录中的包含而不使用绝对路径 我知道我可以使用 makefile 解决这些问题 并且我可能最终会这样做 但我想知道是否有一个可以使
  • 为什么pow函数比简单运算慢?

    从我的一个朋友那里 我听说 pow 函数比简单地将底数乘以它的指数的等价函数要慢 例如 据他介绍 include
  • 在 PHP 中包含 PHP 文件

    我有一个网站 它运行 PHP if 语句来根据附加文件的类型 即 Jpg Txt MP4 显示内容 所以我显示 TXT 文件的代码是 if post attachment txt display attachment div class d
  • 为什么我不能将 与 g++ 4.9.2 一起使用?

    我正在尝试使用文件系统 http en cppreference com w cpp experimental fs path 我有 std c 11 std c 1y in my CMakeLists txt 海湾合作委员会版本是4 9
  • g++ 对于看似不相关的变量“警告:迭代...调用未定义的行为”

    考虑以下代码strange cpp include

随机推荐