int x = 5; // declaration and definition of my global variable
心肺复苏
// I want to use `x` here, too.
// But I need b.cpp to know that it exists, first:
extern int x; // declaration (not definition)
void foo() {
cout << x; // OK
}
通常,您会将 extern int x;放在一个头文件中,这个头文件包含在 心肺复苏中,也包含在最终需要使用 x的任何其他 TU 中。