函式預設具有外部連結,可以從專案中的任何其他檔案呼叫。想進一步了解連結,請參閱連結概念。static函式只能在定義它的原始檔內呼叫。
例如
// file scope, internal linkage
// can only be called from within this file
static void some_function()
{
// code snipped
}
// file scope, external linkage by default
// can be called from any other file in the project
int main()
{
// code snipped
}
即使函式宣告在標頭檔中,只要將它定義為static,就無法從它的原始檔外部呼叫。原始檔可以引入標頭檔、呼叫標頭檔中宣告的static函式並通過編譯,但連結器會找不到static函式的定義。在原始檔中標記為static的函式,不應該在它的標頭檔中宣告。