Re: Does extern "C" affect compilation mode

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 19 February 2011 13:40, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:
> On 19 February 2011 13:40, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:
>> 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++"); }
>>
>
> Oops, I should have included <stdexcept>, I changed the example and
> didn't correct the header.
>

Thanks for that.  I'll have a moan at the vendor to correct their
headers to be compliant by putting the typdef after the enum.

-- 
Regards,

Graham Bloice



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux