[cfe-dev] Why this extern "C" fails ?

Ólafur Waage olafurw at gmail.com
Thu Aug 1 06:10:38 PDT 2013


> nobody has offered a real explanation for this.

There have been plenty of explanations for this.

1. extern "C" must be in global scope, otherwise C can't call it since it
doesn't know how to reference things defined within functions
2. Even if that worked, that variable is within another scope of the cout
statement, so it's out of scope for the cout.



On Thu, Aug 1, 2013 at 12:42 PM, Arji Cot <arjicot at gmail.com> wrote:

> Well I was asking here on purpose because I figured out that it was the
> best thing to do, or in better terms, It was something that could have more
> chances to be explained by some compiler-guy rather than a programmer.
>
> I'm not gonna use this in real code, it's just to test some business logic
> about how things works, notice that since now, nobody has offered a real
> explanation for this.
>
>
> 2013/7/30 Renato Golin <renato.golin at linaro.org>
>
>> On 30 July 2013 09:08, David Chisnall <David.Chisnall at cl.cam.ac.uk>wrote:
>>
>>> #include <iostream>
>>>
>>> int main() {
>>>   {
>>>     extern "C" int a = 42;
>>>     { std::cout << a << "\n"; }
>>>   }
>>>   return (0);
>>> }
>>>
>>> This also fails to compile.  I'm not sure what you'd expect the
>>> semantics to be.  The variable a has automatic storage, and so a linkage
>>> specifier makes no sense in this context.
>>>
>>
>> This code fails to compile because: "variable declarations with the
>> extern specifier at block scope refer to declarations that are members of
>> an enclosing namespace, but they do not introduce new names into that
>> scope." 3.3.2-10.
>>
>> Ajit, your example contains several invalid C/C++ semantics, I'm really
>> not sure how you got this code, or why you want to change the linkage
>> semantics of a local variable.
>>
>> Honestly, you'd have more luck on dissecting the C++ standard at
>> comp.lang.c++, not here.
>>
>> cheers,
>> --renato
>>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130801/36782eca/attachment.html>


More information about the cfe-dev mailing list