Why this extern "C" fails ?

If we declare a var inside a function that would be stack variable whose address can’t be decided by linker. But extern “C” is like stating if some C code wants to use this variable don’t demangle name so that this var can be found by other C compilation units during linking.

In short making a portion inside a function as extern “C” and trying to expose a stack variable to other compilation units is incorrect usage.

------- Original Message -------

Sender : Arji Cotarjicot@gmail.com

Title : Re: [cfe-dev] Why this extern “C” fails ?

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

#include

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@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@gmail.com>

Date : Jul 29, 2013 19:26 (GMT+05:30)

Title : [cfe-dev] Why this extern “C” fails ?


#include

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



201301101351858_QKNMBDIF.gif

|

  • |

Regards

Senthil Kumar

201301101351858_QKNMBDIF.gif