1. 程式人生 > >【C】head file (.h)

【C】head file (.h)

cor clu ext 不能 div called from fine pre

幾個要點:

  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

【C】head file (.h)