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;
4
  • possible duplicate of In C++ source, what is the effect of extern "C"? Commented May 22, 2012 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). Commented May 22, 2012 at 18:30
  • @James, global variable names are mangled too. Your point about linkage having further effect on functions still stands, though. Commented May 22, 2012 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.) Commented May 23, 2012 at 7:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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++

3 Comments

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é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.
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.