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
19
votes
3 answers

Is it legal C++ to declare main as extern "C"?

Being a low-level programmer, I often work with the module startup code for executables, so I understand pretty well how code like "crt0" work. When writing C++ code, I've generally declared main as extern "C" to match what the C startup code is…
Myria
  • 3,372
  • 1
  • 24
  • 42
19
votes
5 answers

Can local and register variables be declared extern?

I have been wondering whether an extern can be declared locally and a register variable. If it can be what would be the restrictions imposed?
Av03
  • 191
  • 1
  • 3
18
votes
4 answers

How to approach explicit template instantiation in the presence of unpredictable type aliases?

I am attempting to add some extern template into my project, to speed up build times and reduce footprint on disk during build. I've done so by listing specialisations that I use frequently, e.g. extern template class…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
18
votes
2 answers

Is there any reason to use extern "C" on headers without methods?

I frequently come across C header files that contain extern "C" guards, but don't contain any actual functions. For example: /* b_ptrdiff.h - base type ptrdiff_t definition header */ #ifndef __INCb_ptrdiff_th #define __INCb_ptrdiff_th #ifdef…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
18
votes
3 answers

Are global variables extern by default or is it equivalent to declaring variable with extern in global?

I have gone through following two questions: static and extern global variables in C and C++ global variable in C are static or not? Both questions says the two things in different way. Question 1's Answer: Global variables are not extern nor…
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38
18
votes
1 answer

What exactly do I lose when using extern "C" in C++?

I'm trying to develop a dynamic library in C++ to be called by an existing program written in IDL (Interactive Data Language). I know that I need to use extern "C" to disable name mangling so that IDL can call the functions it needs (the rest of the…
user1871183
  • 439
  • 5
  • 11
18
votes
6 answers

Okay to declare static global variable in .h file?

static keyword keeps the scope of a global variable limited to that translation unit. If I use static int x in a .h file and include that .h file every other file, won't they all belong to the same translation unit? Then, won't x be visible…
batman
  • 5,022
  • 11
  • 52
  • 82
17
votes
1 answer

C++ : Extern C Functions inside a Namespace

I have to link two libraries, say A and B. Some of the files are common in both libraries. So, I declare functions in library A inside a namespace, say abc. So, in A and B, a function func looks like below: [ in A] namespace abc { extern…
Dharmendra
  • 384
  • 1
  • 5
  • 22
17
votes
1 answer

friendship with extern "C" function seems to require :: to qualify name

Trying to make a class friends with an extern "C" function, this code works: #include extern "C" { void foo(); } namespace { struct bar { // without :: this refuses to compile friend void ::foo(); bar() : v(666) {} …
Flexo
  • 87,323
  • 22
  • 191
  • 272
17
votes
4 answers

Is extern keyword for function necessary at all in C?

It appears to me that even if I refer to a function in another file with out extern declaration, gcc can still compile that unit. So I am wondering whether the extern declaration is necessary anywhere for function? I know that you need extern for…
Xiaolong Li
  • 211
  • 3
  • 4
17
votes
2 answers

What is default storage class for global variables?

What is default storage class of a global variable? While searching on web I found, some sites say it is static. But, static means internal linkage and the variable can not be available outside the file scope i.e it should not be available to other…
Vinod T. Patil
  • 2,951
  • 3
  • 26
  • 21
17
votes
2 answers

The usage of extern in c++

I've having difficulty understanding how 'extern' works. I've searched Google but there doesn't seem to be the particular example case I'm trying If i have a file main.cpp which references one.h and in it i have a list named LIST1 (which is a double…
Lilz
  • 4,013
  • 13
  • 61
  • 95
16
votes
1 answer

extern on a static function in C++

I don't know why this is driving me nuts but it is. I have a function defined and forward declared in main. static void myFunc(int x); static void myFunc( int x) { //do stuff } main() I want to use myFunc(int x) in another class. So I would…
Dixon Steel
  • 1,021
  • 4
  • 12
  • 27
16
votes
2 answers

How can I prevent overloading for extern "C" functions?

I'm writing a c++ library that exposes some functions which are used only by C# code. However, as I accidently mistyped the paramter, I found that this code can be succesfully compiled and linked even without any warning as long as I don't use the…
karatoga
  • 513
  • 4
  • 14
16
votes
2 answers

Can a variable be redeclared as auto that deduced to the same type?

Is the following allowed by the standard? #include extern int a; auto a = 3; int main(int, char**) { std::cout << a << std::endl; return 0; } clang accepts the code. g++ complains for conflicting declaration.
Jamboree
  • 5,139
  • 2
  • 16
  • 36