`extern` — C++ Keyword
`extern` — C++ Keyword
The extern keyword in C++: declares a variable or function defined in another translation unit.
`extern` — C++ Keyword
The extern keyword in C++: declares a variable or function defined in another translation unit.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
externDeclares that a variable or function is defined elsewhere (another translation unit or a C library). Also used with a string literal for language linkage (extern "C").
extern Type name; // declaration (no definition)
extern Type name = value; // definition (extern is redundant here)
extern "C" return-type func(); // C language linkage
extern "C" { /* declarations */ }
// --- config.cpp ---
// int g_timeout = 30; // definition lives here
// --- main.cpp ---
#include <print>
extern int g_timeout; // declaration: defined in config.cpp
// Calling a C library function
extern "C" {
int c_add(int a, int b); // hypothetical C function
}
int main() {
// std::println("{}", g_timeout); // uses definition from config.cpp
(void)g_timeout;
// std::println("{}", c_add(2, 3)); // calls C function without name mangling
}
extern "C" prevents C++ name mangling, required for dlopen/GetProcAddress and interop with C.externint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}