【C】head file (.h)

几个要点:

  1. 在源文件中使用头文件,等于将头文件的内容原封不动的复制过来;
  2. 对于源文件“abc.c”,一般会设置一个“abc.h”,将程序中用到的头文件全部集中到“abc.h”中;
  3. 函数、全局变量的声明(declaration)一般都会放在头文件,方便其他文件调用
  4. 在一个源文件中,同一个头文件不能被多次调用,故需要保护(include guard)
  5. 可以导入其他文件夹中的头文件

具体例子:

  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

猜你喜欢

转载自www.cnblogs.com/kimilin/p/10367132.html