2

I am familiar with the extern keyword, it is used to declare a variable present in some other file, but what does the following statement mean??

extern "C" const IMAGE_DOS_HEADER __ImageBase;
bmargulies
  • 97,814
  • 39
  • 186
  • 310
user1232138
  • 5,451
  • 8
  • 37
  • 64
  • possible duplicate of [In C++ source, what is the effect of extern "C"?](http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c) – Bo Persson May 22 '12 at 18:26
  • Wnat is `IMAGE_DOS_HEADER`? Language linkage normally only applies to function types (included nested function types, such as pointer to function). – James Kanze May 22 '12 at 18:30
  • @James, global variable names [are mangled too](http://stackoverflow.com/q/2937273/464709). Your point about linkage having further effect on functions still stands, though. – Frédéric Hamidi May 22 '12 at 18:37
  • @FrédéricHamidi Mangling is up to the implementation; it may mangle nothing, everything or anything in between. In practice, most compilers, for most languages, have mangled everything; what has traditionally set C++ apart is the complexity of the mangling scheme (including information concerning the types of parameters) for functions. Traditionally, C and C++ have mangled variable names in the same manner. (A quick check with the compilers I have handy show that after `extern "C"`, I can't even find the name in the object file. Which I definitely don't understand.) – James Kanze May 23 '12 at 07:55

2 Answers2

5

It means the __ImageBase global variable uses C linkage and that its name should be mangled using the rules for C instead of C++.

EDIT: It just so happens that Raymond Chen recently published an article that demonstrates my original answer was plain wrong: extern "C" does not disable name mangling, it only changes the rules used to perform it. C names can be mangled too.

Community
  • 1
  • 1
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 1
    C linkage involves more than just mangling; it's possible for the arguments to be passed differently, for example. Also, the language linkage is part of the type; a function with `extern "C"` linkage cannot be assigned to a pointer to a C++ function. – James Kanze May 22 '12 at 18:30
1

It means do not mangle the symbol name __ImageBase that follows the extern "C". In short it ensures you can use the variable in C++ code.

extern "C" specify's the linkage to be applied. In short a Linkage specification.
It tells the C++ compiler to apply linkage of the type of C to the symbol that follows.

Good Read:
Using extern to Specify Linkage
How to mix C and C++

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • The now-deleted comment was right, as `extern "C"` is indeed a C++-only construct, but it still means C code that links to the C++ module defining this variable will be able to refer to it... So, it actually works both ways, C to C++ and C++ to C (section 32.6 of the FAQ seems to agree :). – Frédéric Hamidi May 22 '12 at 18:22
  • @FrédéricHamidi: A C compiler won't understand the `extern "C"` construct, and so one must wrap the `extern "C" {` and` }` lines in an `#ifdef` so they won't be seen by the C compilers. – Alok Save May 22 '12 at 18:25
  • Yes, we agree that it cannot be used outside of C++ modules. But it both allows to use the decorated symbol if it is defined in a C module, or allows C modules to use the symbol if the C++ module itself defines it. That's what I meant by `both ways` (but I don't know if I was actually clearer this time around :) – Frédéric Hamidi May 22 '12 at 18:31