C/C++ 專案通常將程式碼分為兩類檔案:
.h,存放宣告(declaration)。.c,C++ 慣用 .cpp(或 .cc、.cxx),存放定義/實作(definition/implementation)。置於標頭檔與實作檔開頭,包含:
/*
* Copyright (c) 2001, 某公司
* All rights reserved.
*
* 檔案名稱:filename.h
* 摘 要:簡要描述本檔案內容
*
* 目前版本:1.1
* 作 者:張三
* 完成日期:2001年7月20日
*
* 取代版本:1.0
* 原作者:李四
* 完成日期:2001年5月10日
*/
標頭檔由三部分組成:
#ifndef/#define/#endif 防止重複引入。
#include <filename.h>。
#include "filename.h"。
extern int value 等宣告。// 版權聲明省略
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <math.h> // 標準庫
#include "myheader.h" // 非標準庫
void Function1(...); // 全域函式宣告
class Box { // 類別結構宣告
...
};
#endif
// 版權聲明省略
#include "graphics.h" // 引入對應標頭檔
// 全域函式實作
void Function1(...) { ... }
// 類別成員函式實作
void Box::Draw(...) { ... }
標頭檔超過 10 個時,建議分目錄管理:
include/:公開標頭檔source/:實作檔(可多層目錄)