如何查看和修改gcc/g++默认include路径

2015-06-21 21:31:00
admin
原创 9837
摘要:如何查看和修改gcc/g++默认include路径

一、如何查看gcc/g++默认include路径

安装gcc:yum install gcc

安装g++:yum install gcc-c++

gcc/g++参考手册:https://gcc.gnu.org/onlinedocs/gcc-4.8.4/gcc/Invoking-GCC.html#Invoking-GCC


1、查看gcc的include路径命令:

`gcc -print-prog-name=cc1` -v

`g++ -print-prog-name=cc1` -v


2、查看g++的include路径命令:

`gcc -print-prog-name=cc1plus` -v

`g++ -print-prog-name=cc1plus` -v


CentOS release 6.4 (Final)下面执行:

[root@ebs-19233 ~]# `gcc -print-prog-name=cc1` -v
忽略不存在的目录“/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include-fixed”
忽略不存在的目录“/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../x86_64-redhat-linux/include”
#include "..." 搜索从这里开始:
#include <...> 搜索从这里开始:
 /tmp
 /usr/local/include
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/include
 /usr/include
搜索列表结束。


CentOS release 6.4 (Final)下面执行:

[root@ebs-19233 ~]# `gcc -print-prog-name=cc1plus` -v
忽略不存在的目录“/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include-fixed”
忽略不存在的目录“/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../x86_64-redhat-linux/include”
#include "..." 搜索从这里开始:
#include <...> 搜索从这里开始:
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/backward
 /usr/local/include
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/include
 /usr/include
搜索列表结束。


即默认include路径如下,可以看出/usr/local/include的优先级高于/usr/include

/usr/include/c++/4.4.7
/usr/include/c++/4.4.7/x86_64-redhat-linux
/usr/include/c++/4.4.7/backward
/usr/local/include
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include
/usr/include


二、如何添加gcc/g++默认include路径(用户添加的路径优先级最高

方法1:通过环境变量添加

对于C语言:

C_INCLUDE_PATH=/usr/include:/usr/local/include:/tmp
export C_INCLUDE_PATH

对于C++语言:
CPLUS_INCLUDE_PATH=/usr/include:/usr/local/include:/tmp
export CPLUS_INCLUDE_PATH


CPATH环境变量同时支持C语言和C++语言,并且优先级高于以上2个环境变量。


方法2:通过命令行添加

使用命令行添加,home优先于tmp

gcc -I/home -I/tmp main.c

g++ -I/home -I/tmp main.cpp

发表评论
评论通过审核之后才会显示。