关于Linux GCC的路径问题(Include路径, lib 路径)

本文详细介绍了GCC编译器的路径配置方法,包括如何通过配置文件指定头文件和库文件的位置,解析了`../configure --prefix`等参数的作用,并提供了具体的配置实例。
    前几天遇到一个问题GCC include 路径是怎样设定的  GCC 在执行编译的时候是怎样搜索它的头文件路径的喃? 还有它的LIB文件库的路径?   我问了一个同学,他说的是它自己内部就已经确定的了。 我在想如果我重新安装GCC 它会不会产生覆盖?
    我就当它是默认的, 但是我的Gcc遇到了一个问题使我不得不去重新认识gcc的工作。
    安装gcc的时候首先要配置, 就是在配置这里, 我终于认清了gcc的路径问题。
    当你在bash下输入 gcc -v 的时候它会显示:
    ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-cpu=generic --build=i386-redhat-linux
    这个就是你的Gcc的配置文件。在执行 ./configure  后面加上的数据。  看见了“--prefix”了没有, 正是它在影响GCC的执行路径。
   下面的是在gcc 安装原码包里面的文件内容:(我挑了几句出来)
    --prefix=dirname
Specify the toplevel installation directory. This is the recommended way to install the tools into a directory other than the default. The toplevel installation directory defaults to /usr/local.
    --exec-prefix=dirname
    Specify the toplevel installation directory for architecture-dependent files. The default is prefix.
    --bindir=dirname
   Specify the installation directory for the executables called by users (such as gcc and g++). The default is exec-prefix/bin.
--libdir=dirname
Specify the installation directory for object code libraries and internal data files of GCC. The default is exec-prefix/lib.
--libexecdir=dirname
Specify the installation directory for internal executables of GCC. The default is exec-prefix/libexec.
--with-slibdir=dirname
Specify the installation directory for the shared libgcc library. The default is libdir.
--infodir=dirname
Specify the installation directory for documentation in info format. The default is prefix/info.
--datadir=dirname
Specify the installation directory for some architecture-independent data files referenced by GCC. The default is prefix/share.
--mandir=dirname
Specify the installation directory for manual pages. The default is prefix/man. (Note that the manual pages are only extracts from the full GCC manuals, which are provided in Texinfo format. The manpages are derived by an automatic conversion process from parts of the full manual.)
--with-gxx-include-dir=dirname
Specify the installation directory for G++ header files. The default is prefix/include/c++/version.

看清楚了吗? 其它的配置命令我准备把它上来。
   如果你看完了你肯定就有所了解了!  如果我说得有什么问题的话,通知我一声。 wushibin1@gmail.com  大家共同进步!
### 3.1 使用 `cpp -v` 查看默认 include 路径Linux 系统中,可以通过以下命令查看 C/C++ 编译器默认的头文件搜索路径: ```bash cpp -v ``` 该命令会输出编译器在预处理阶段查找头文件的路径列表。输出内容通常包括多个目录,例如: ``` #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-linux-gnu/9/include /usr/local/include /usr/include/x86_64-linux-gnu /usr/include End of search list. ``` 这些路径表示系统默认的头文件搜索顺序,首先是用户通过 `-I` 指定的路径,其次是环境变量 `C_INCLUDE_PATH` `CPLUS_INCLUDE_PATH` 中的路径,最后是系统默认路径,如 `/usr/include` `/usr/local/include`[^2]。 ### 3.2 使用 `gcc -print-search-dirs` 查看编译器搜索路径 除了 `cpp -v`,还可以使用以下命令查看 GCC 编译器的搜索路径: ```bash gcc -print-search-dirs ``` 该命令会列出所有编译器查找头文件库文件的路径,包括标准头文件路径路径。输出内容通常包括 `install`, `programs`, `libraries` 三部分,其中 `libraries` 部分显示的是链接器查找库文件的路径[^4]。 ### 3.3 查看环境变量中设置include 路径 如果用户或系统管理员通过环境变量设置了额外的头文件路径,可以通过以下命令查看: ```bash echo $C_INCLUDE_PATH echo $CPLUS_INCLUDE_PATH ``` 这些命令会输出当前用户设置的 C C++ 头文件路径。如果设置了这些变量,GCC/G++ 在编译时会将这些路径加入头文件搜索路径列表中[^1]。 ### 3.4 默认系统 include 路径 Linux 系统的标准头文件通常位于以下目录中: - `/usr/include`:用于存放系统级头文件。 - `/usr/local/include`:通常用于存放用户自行安装的库的头文件。 - `/usr/lib/gcc-$(arch)-linux-gnu/$(version)/include`:存放特定 GCC 版本的头文件。 这些目录是 GCC/G++ 默认搜索的路径,除非特别指定 `-nostdinc` 选项来禁用默认头文件路径[^3]。 ### 3.5 在 C/C++ 代码中使用相对路径包含头文件 在代码中使用 `#include <...>` `#include "..."` 时,GCC/G++ 会按照不同的顺序查找头文件: - `#include <stdio.h>`:仅在系统默认路径中查找头文件。 - `#include "stdio.h"`:首先在当前源文件所在目录查找,如果未找到,则在系统默认路径中查找。 例如,若 `/usr/local/include` 是系统默认路径,且头文件 `v8.h` 位于 `/usr/local/include/node/v8.h`,则可以使用以下方式包含: ```c #include <node/v8.h> ``` 这种方式可以简化路径引用,避免冗长的绝对路径[^3]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值