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
10
votes
2 answers

Alias for function

I want to import some functions from kernel32.dll, but I want to use different names. Example function: [DllImport("kernel32.dll")] private static extern bool ReadProcessMemoryProc64 (...); private static bool BetterReadableAndWriteableName (...)…
Cubi73
  • 1,891
  • 3
  • 31
  • 52
10
votes
3 answers

Compiling program containing extern "C"

I'm trying to use a makefile to compile a program someone else has written, using cygwin. I get a lot of error messages, of which many complain error: template with C linkage. After searching around for a bit it seems the problem is connected to…
jorgen
  • 3,425
  • 4
  • 31
  • 53
10
votes
5 answers

What is the best strategy for sharing variables between source files in c/c++?

I frequently have to write c/c++ programs with 10+ source files where a handful of variables need to be shared between functions in all the files. I have read before that it is generally good practice to avoid using global variables with extern.…
baconeer
  • 211
  • 1
  • 2
  • 7
10
votes
5 answers

Correct way to declare/define custom cout-like object

I created my own std::cout-like object that writes both to std::cout and to a log file. I'm currently defining it like this in a header file, but I'm getting unused variable warnings. Header file static LOut { }; static LOut…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
10
votes
4 answers

Extern in multiple files and possible double definition

I was running the following codes compiled together as: gcc A.c B.c -o combined Program A: #include int a=1; int b; int main() { extern int a,b; fun(); printf("%d %d\n",a,b); } Program B: int a; int b=2; int fun() { printf("%d %d\n",a,b);…
tapananand
  • 4,392
  • 2
  • 19
  • 35
10
votes
2 answers

Is extern optional?

I am sure I am going crazy, but consider the following C code: // file1.c int first; void f(void) { first = 2; } // file2.c #include int first; void f(); int main(void) { first = 1; f(); printf("%d", first); } These two…
Chris Jefferson
  • 7,225
  • 11
  • 43
  • 66
10
votes
6 answers

Using the 'extern' keyword properly

There are sources (books, online materials) that explain the usage of extern as following: extern int i; // declaration - has 'extern' int i = 1; // definition - specified by the absence of 'extern' And there are sources that…
peter.slizik
  • 2,015
  • 1
  • 17
  • 29
9
votes
3 answers

Are static class variables the same as extern variables, only with class scope?

It seems to me that a static class variable is identical to an extern variable, because you only declare it in the static int x / extern int x statement, and actually define it elsewhere (usually in a .cpp file) static class variable // .h…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
9
votes
3 answers

Why can extern be applied to definitions?

Why is this legal? extern int foo = 0xF00; // Gets a warning, still compiles extern void bar() { // No warning int x; } Is there a reason to why this is allowed?
Pubby
  • 51,882
  • 13
  • 139
  • 180
9
votes
3 answers

Is it possible to typedef a pointer-to-extern-"C"-function type within a template?

I want to add a public typedef to a template for a pointer to a function-taking-one-argument that uses "C" language linkage. I tried: extern "C" { template struct test { typedef return_t_…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
9
votes
0 answers

Intellisense warning that it can't find function definition for assembly function

In my MSVC 2015 project I have a function, int foo(int, int) which is implemented in an .asm file. When I extern "C" declare this function in a .cpp file in the same project, Intellisense complains that Function definition for 'foo' not found.: I'm…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
9
votes
5 answers

Is extern keyword really necessary?

... #include "test1.h" int main(..) { count << aaa <
httpinterpret
  • 6,409
  • 9
  • 33
  • 37
9
votes
1 answer

C: undefined reference to a variable when using extern

I try to declare a global variable config: //general.h struct config_t { int num; }; extern struct config_t config; //The global variable Then I define config variable in general.c: //general.c #include "general.h" struct config_t config =…
Halona
  • 1,475
  • 1
  • 15
  • 26
9
votes
2 answers

What's is the idea behind C99 inline?

I am confused about inline in C99. Here is what I want: I want my function get inlined everywhere, not just limited in one translation unit (or one compilation unit, a .c file). I want the address of the function consistent. If I save the address…
zsh
  • 91
  • 3
9
votes
2 answers

where memory for extern variable will be stored and by which file

I read about extern variable, but no where found answer related to its memory allocation, My question is Who will allocate memory for Extern variable, and in which memory segment. int a; // file 1 extern int a; // file 2 here file 1 will allocate…
anand
  • 267
  • 1
  • 3
  • 16