Reputation: 6625
What does the author mean about "extern linkage" and "C language linkage" in the following paragraph, extracted from [1].
"There are two different forms of the extern C
declaration: extern C
as used above, and extern C
{ … } with the declarations between the braces. The first (inline) form is a declaration with extern linkage and with C language linkage; the second only affects language linkage. The following two declarations are thus equivalent:"
Can you further elaborate what he's trying to explain with this example?
[1] http://tldp.org/HOWTO/C++-dlopen/thesolution.html
Upvotes: 1
Views: 1904
Reputation: 140639
Clumsy wording, yeah. What he's trying to get at is,
extern "C" int foo();
is the same as
extern "C" { extern int foo(); }
but not the same as
extern "C" { int foo(); }
... except that there is no practical difference between "extern int foo();
" and "int foo();
" at file scope in C++, so you are entirely forgiven for scratching your head. Here's a case where it actually makes a difference:
extern "C" const int x = 12;
extern "C" { const int y = 12; }
x
will be visible outside the translation unit, y
won't.
Upvotes: 2
Reputation: 881563
What the author is saying relates to these two lines:
extern "C" int foo;
extern "C" { int bar; }
foo
is a variable declared but not defined. It exists elsewhere. On the other hand, bar
is both declared and defined.
Think of a declaration as just stating that something exists somewhere but not actually creating it. Definition is therefore declaration plus bringing that thing into existence.
The latter one is exactly the same as int bar;
but will "publish" the variable with C linkage. For example, a function int max (int a, int b);
may be published as _max
in C language linkage and _max$$int$int
in C++ language linkage (to allow more than one function with the same name).
Note that "publishing" in this context is how the function looks to the linker, so that your code can link to it. Without C language linkage, it's usually rather difficult to link C code with C++ libraries.
Upvotes: 4