Questions tagged [extern]

extern is an access-specifier in C and C++ which defines a global variable that is visible to all object modules.

The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. The extern declaration in C/C++ is to indicate the existence of, and the type of, a global variable or function. A global variable, or a global function, is one that is available to all C/C++ modules. An extern is something that is defined externally to the current module.

schematic representation of global variable declared with extern keyword (source)

1475 questions
1226
votes
19 answers

How do I use extern to share variables between source files?

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope? This is related to sharing variables across source files, but how does that work precisely? Where do I…
shilpa
149
votes
5 answers

using extern template (C++11) to avoid instantiation

Figure 1: function templates TemplHeader.h template void f(); TemplCpp.cpp template void f(){ //... } //explicit instantation template void f(); Main.cpp #include "TemplHeader.h" extern template void f(); //is…
codekiddy
  • 5,897
  • 9
  • 50
  • 80
144
votes
1 answer

"FOUNDATION_EXPORT" vs "extern"

I would like to ask what's the reason behind using FOUNDATION_EXPORT instead of extern in Objective C projects. I've checked this question and using FOUNDATION_EXPORT has earned whopping 340 points (1st place) whereas using extern only 74 points…
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
116
votes
7 answers

What does extern inline do?

I understand that inline by itself is a suggestion to the compiler, and at its discretion it may or may not inline the function, and it will also produce linkable object code. I think that static inline does the same (may or may not inline) but will…
wilbur_m
89
votes
7 answers

How to declare constexpr extern?

Is it possible to declare a variable extern constexpr and define it in another file? I tried it but the compiler gives error: Declaration of constexpr variable 'i' is not a definition in .h: extern constexpr int i; in .cpp: constexpr int i = 10;…
coldbrew
  • 903
  • 1
  • 6
  • 5
82
votes
6 answers

Forward-declare enum in Objective-C

I'm having trouble with enum visibility in an Objective-C program. I have two header files, and one defines a typedef enum. Another file needs to use the typedef'd type. In straight C, I would simply #include the other header file, but in…
Stephen Touset
  • 2,562
  • 2
  • 25
  • 24
79
votes
6 answers

Mixing extern and const

Can I mix extern and const, as extern const? If yes, does the const qualifier impose it's reign only within the scope it's declared in or should it exactly match the declaration of the translational unit it's declared in? I.e. can I declare say…
legends2k
  • 31,634
  • 25
  • 118
  • 222
78
votes
4 answers

How does extern work in C#?

Whenever I look deeply enough into reflector I bump into extern methods with no source. I read the msdn documentation at http://msdn.microsoft.com/en-us/library/e59b22c5(v=vs.80).aspx. What I got from that article is that methods with the extern…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
73
votes
8 answers

How do I share a global variable between c files?

If I define a global variable in a .c file, how can I use the same variable in another .c file? file1.c: #include int i=10; int main() { printf("%d",i); return 0; } file2.c: #include int main() { //some data…
peter_perl
  • 733
  • 1
  • 5
  • 5
72
votes
4 answers

Why won't extern link to a static variable?

Why does extern int n not compile when n is declared (in a different file) static int n, but works when declared int n? (Both of these declarations were at file scope.) Basically, why is int n in file scope not the same as static int n in the…
Jared Pochtar
  • 4,925
  • 2
  • 29
  • 39
60
votes
5 answers

C++ extern keyword on functions. Why no just include the header file?

If I understand it correctly this means extern void foo(); that the function foo is declared in another translation unit. 1) Why not just #include the header in which this function is declared? 2) How does the linker know where to look for function…
user199421
  • 1,850
  • 2
  • 18
  • 18
59
votes
3 answers

Does a declaration using "auto" match an extern declaration that uses a concrete type specifier?

Consider the following program: extern int x; auto x = 42; int main() { } Clang 3.5 accepts it (live demo), GCC 4.9 and VS2013 do not (live demo for the former). Who is right, and where is the correct behavior specified in the C++ Standard?
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
57
votes
3 answers

What does the extern keyword mean?

What does the extern keyword mean? I've seen that in front of an function declaration like extern void DoFoo ...
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
53
votes
6 answers

Should functions be made "extern" in header files?

Should functions be made extern in header files? Or are they extern by default? For example, should I write this: // birthdays.h struct person find_birthday(const char* name); or this: // birthdays.h extern struct person find_birthday(const char*…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
47
votes
1 answer

How to detect if C code (which needs 'extern C') is compiled in C++

I have a C header as part of a C++ library. This C header would only make sense compiled by a C compiler, or by a C++ compiler within an extern "C" { ... } block, otherwise unresolved link errors would happen. I thought to add a block such…
fferri
  • 18,285
  • 5
  • 46
  • 95
1
2 3
98 99