几个要点:
- 在源文件中使用头文件,等于将头文件的内容原封不动的复制过来;
- 对于源文件“abc.c”,一般会设置一个“abc.h”,将程序中用到的头文件全部集中到“abc.h”中;
- 函数、全局变量的声明(declaration)一般都会放在头文件,方便其他文件调用
- 在一个源文件中,同一个头文件不能被多次调用,故需要保护(include guard)
- 可以导入其他文件夹中的头文件
具体例子:
include guard:
/*Swmm5Extend.h*/ //if this head file is called before, nothing will be reached #ifndef SWMM5EXTEND_H //this can be everything, but SWMM5EXTEND_H is clear #define SWMM5EXTEND_H /* other head files */ #endif // !SWMM5EXTEND_H
导入其他文件夹内的头文件:
/*in the subdirectory*/ #include "/subdirectory/structure.h" /*in another directory*/ #include "../anotherdirectory/structure.h" //Linke: //https://stackoverflow.com/questions/7581408/including-a-header-file-from-another-directory