Does extern "C" affect compilation mode
Jonathan Wakely
jwakely.gcc@gmail.com
Sat Feb 19 13:41:00 GMT 2011
On 19 February 2011 09:41, Graham Bloice wrote:
> I'm getting problems with g++ compiling some headers:
>
> #ifdef __cplusplus
> extern "C" {
> #endif
>
> typedef enum abc xyz;
> enum abc {
> p1 = 0,
> p2,
> p3
> };
>
> #ifdef __cplusplus
> }
> #endif
>
> This code compiles with the mbed cloud compiler, Keil RV and VS2010,
> but not g++ 4.4.5. The errors are:
>
> test.cpp:5: error: use of enum ‘abc’ without previous declaration
> test.cpp:5: error: invalid type in declaration before ‘;’ token
>
> From a previous query I understand that the code isn't legal c++, but
> is legal c, as demonstrated when I compile the code using the -x c
> option.
>
> I'm guessing here, but do some compilers switch into a C compilation
> mode when they hit an extern "C"? I know extern "C" affects the
> linkage but is it meant to affect compilation mode?
G++ doesn't, and a conforming compiler shouldn't - but this is the
wrong place to ask about "somt compilers".
For a conforming compiler extern "C" only tells the C++ compiler to
use different linkage for the enclosed functions. It absolutely does
not cause it to compile as C, as can be shown by the fact that C++
features are allowed inside extern "C" blocks:
#include <string>
extern "C" void f() { throw std::runtime_error("I am C++"); }
More information about the Gcc-help
mailing list