0

Say I have the following C++ code:

int x;
some_class y;

extern "C" {
  void foo () {
    // do something with x
    // do something with y
  }
}

Do x and/or y need to be declared with extern "C"?

3
  • Have you tried compiling it? Is there a problem? BTW, variables in headers should usually be declared extern (without the "C"). Commented Jan 10, 2012 at 22:12
  • 3
    Yeah it compiled, but just because something compiles in C++ doesn't mean it is correct. Commented Jan 10, 2012 at 22:20
  • True, but it would improve your question to point these things out in the first place. That way, I wouldn't be left wondering whether yours is one of the countless SO posts that simply didn't bother to describe the problem or include the error message. Commented Jan 10, 2012 at 23:33

4 Answers 4

6

No. There is no restriction on extern "C" functions accessing functions and variables with C++ language linkage.

C++ extern "C" functions are often used to provide a C interface into code with C++ language linkage so it would be more than a little restrictive if this were the case.

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

Comments

1

well, variables aren't needed to be declared using extern "c". in fact, even functions aren't needed to use extern "C" just because you call them from a function with extern "C"

you can freely write code like:

    void bla() {cout<<"bla\n";}
extern "C" void sh() {bla();}

the function "sh" will compile using c++ compiler, so it can freely call bla. extern "C" affect only the name of the function.

Comments

1

C++ methods/functions support overloading which make them link in a slightly more complicated way. What extern "C" does is really to say "don't let me overload and use the good old C way" which makes it compatible with C code.

Edit: Charles Bailey points out that I apparently was wrong about variables linking in an identical way. In other words, if the variables are used by external C code (which they could be since they're not static), you should put them inside extern "C". If you're only using them internally in the file or from external C++ code only, you're fine with keeping them where they are.

2 Comments

Variables don't have to link identically in C and C++, it is up to the implementation. That is why variables with external linkage have language linkage and can be declared extern "C" if need.
Cool, I didn't know that and I've never seen it, I'll edit to reflect the information.
-2

extern "C" is only useful for functions, not for data. It defines the way the function gets decorated or mangled. The decoration also depends on the calling convention used. Classes cannot be used from C, neither do functions that have classes as a parameter, even if inside an extern "C" block.

The extern "C" make sense where a C library is intended to be used by C and C++ code.

In conclusion having a class within an extern "C" does not make sense, x is just a basic type, having is inside or outside does not matter. The normal way to use the feature would be to have all the content of a header inside such a block. Having an int declaration in a header is not a good idea! That will create a variable for each translation unit and cause trouble at link time... Not sure what you intend to do with code similar to what you posted.

5 Comments

"extern "C" is only useful for functions, not for data." That's not true -- e.g. I'd wager that your compiler's standard library implementation has errno declared with C-linkage.
Why isn't extern "C" useful for variables? Both function and variables with external linkage have language linkage.
"Having an int declaration in a header is not a good idea! That will create a variable for each translation unit and cause trouble at link time..." No, it's declared extern, so must still be defined in exactly one translation unit. Having an int definition in a header file would be a bad idea...
Having a declaration of an object with external linkage in a header file is not an issue if that declaration is not a definiton (e.g. extern "C" int x;). Having a declaration of such an object that is a definition (e.g. extern "C" { int x; }) is an error if that header file is included in more than one translation unit.
Thanks for rectifying, I keep this broken answer alive, because of the good comments.

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.