C语言中的include很简单,但不是你想象中的简单。
之前写过一个《C语言的include没你想的那么简单》,广受大家喜爱,详见《C语言的include没你想的那么简单(图文版)》或《C语言的Include没你想的那么简单(视频版)》。
最近又遇到个新问题,对于同名头文件,编译器是怎么处理的?
我这个踩坑过程就不细说了,说多了都是泪,直接上干货吧!
如果你嫌我啰嗦,你就直接拖到文末看截图的结果汇总吧!
为了讲清楚这个问题,我做了个实验,编了个这样的目录结构和文件:
./ │ main.c │ ├─inc1 │ inc.h │ inc1.c │ └─inc2 inc.h而这些文件内容分别如下: (1)./main.c
// ./main.c #include(2)./inc1/inc.h#include "inc.h" extern int inc1(void); int main(void) { printf("main VAL=%d ",VAL); printf("inc1 VAL=%d ",inc1()); return 0; }
// ./inc1/inc.h #ifndef _INC_H_ #define _INC_H_ #pragma message(__FILE__) #define VAL 1 #endif(3)./inc1/inc1.c
// ./inc1/inc1.c #include "inc.h" int inc1(void) { return VAL; }(4)./inc2/inc.h
// ./inc2/inc.h #ifndef _INC_H_ #define _INC_H_ #pragma message(__FILE__) #define VAL 2 #endif然后,我分别用Cygwin上的GCC、Linux上的GCC、Window上的GreenHills和Windows上的ARMCC做了测试。 首先,直接在Windows上的命令窗口上直接执行(基于Cygwin上的GCC):
gcc .main.c .inc1inc1.c -o main得到的是
.main.c:2:10: fatal error: inc.h: No such file or directory 2 | #include "inc.h" | ^~~~~~~ compilation terminated. .inc1inc1.c:1:10: fatal error: inc.h: No such file or directory 1 | #include "inc.h" | ^~~~~~~ compilation terminated.
gcc .main.c .inc1inc1.c -Iinc1 -Iinc2 -o main得到的是
In file included from .main.c:2: inc1/inc.h:3:11: note: '#pragma message: inc1/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~ In file included from .inc1inc1.c:1: inc1/inc.h:3:11: note: '#pragma message: inc1/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~
gcc .main.c .inc1inc1.c -Iinc2 -o main得到的是
In file included from .main.c:2: inc2/inc.h:3:11: note: '#pragma message: inc2/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~ In file included from .inc1inc1.c:1: inc2/inc.h:3:11: note: '#pragma message: inc2/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~
gcc .main.c .inc1inc1.c -I -Iinc1 -Iinc2 -o main
得到的是
In file included from .main.c:2: inc2/inc.h:3:11: note: '#pragma message: inc2/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~ In file included from .inc1inc1.c:1: inc2/inc.h:3:11: note: '#pragma message: inc2/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~
gcc .main.c .inc1inc1.c -I./ -Iinc1 -Iinc2 -o main
得到的是
In file included from .main.c:2: inc1/inc.h:3:11: note: '#pragma message: inc1/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~ In file included from .inc1inc1.c:1: inc1/inc.h:3:11: note: '#pragma message: inc1/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~凌乱了吧,-I 和-I./是不一样的?!
等等,是不是Windows这“/”和“”不一样,Powershell和Cygwin杂交就不一样了?? 于是,我找了个纯血的Linux+GCC试试。把路径中的换成/:
gcc ./main.c ./inc1/inc1.c -o main
./main.c:2 fatal error: inc.h: No such file or directory 2 | #include "inc.h" | ^~~~~~~ compilation terminated. In file included from ./inc1/inc1.c ./inc1/inc.h:3 note: '#pragma message: ./inc1/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~
gcc ./main.c ./inc1/inc1.c -Iinc2 -o main
In file included from ./main.c:2: inc2/inc.h:3:11: note: '#pragma message: inc2/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~ In file included from ./inc1/inc1.c:1: ./inc1/inc.h:3:11: note: '#pragma message: ./inc1/inc.h' 3 | #pragma message(__FILE__) | ^~~~~~~
全部0条评论
快来发表一下你的评论吧 !