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

Arji Cot arjicot at gmail.com
Mon Jul 29 22:43:40 PDT 2013


1 ) this works flawlessly, and as far as I know C and C++ have the same
rules for scoping

#include <iostream>

int main() {
  {
    int a = 42;
    { std::cout << a << "\n"; }
  }
  return (0);
}

also the C equivalent version works great too

#include <stdio.h>

int main() {
  {
    int a = 42;
    { printf("\n%d\n", a); }
  }
  return (0);
}

So my point is: as far as the scoping rules are involved in this, they
don't have any role in this error.

2 ) Why the external linkage of a var x should necessarily imply that the
var x must be used or accessed in any way ?

In other words I can't see this strict relation and this constrain with an
extern "C".

2013/7/29 SENTHIL KUMAR THANGAVELU <senthil.t at samsung.com>

>
>
> As I understand 2 potential issues could be here
>
> 1) 'a' maynot be visible to the second '{ }' block containing std::cout
>
> 2) Asking for extern "C" linkage for a stack variable, implying there
> is some C code which needs to see this stack variable
>
>
>
> ------- *Original Message* -------
>
> *Sender* : Arji Cot<arjicot at gmail.com>
>
> *Date* : Jul 29, 2013 19:26 (GMT+05:30)
>
> *Title* : [cfe-dev] Why this extern "C" fails ?
>
>
> --------------
>
> #include <iostream>
>
> int main() {
>   {
>     extern "C" { int a = 42; }
>     { std::cout << a << "\n"; }
>   }
>   return (0);
> }
>
> --------------
>
> this code fails to compile and the compiler message alone
>
> main.cpp:5:12: error: expected unqualified-id
>     extern "C" { int a = 42; }
>            ^
> 1 error generated.
>
> It's not enough to me to explain why this is not working.
>
> There is something that I'm missing ?
>
>
>
>
>
> Regards
>
> Senthil Kumar
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130730/09cfe14f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 201307291954777_44YDXKW4.gif
Type: image/gif
Size: 14036 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130730/09cfe14f/attachment.gif>


More information about the cfe-dev mailing list