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>

201301101351858_QKNMBDIF.gif

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);
}

This does not have the same scoping as this:

#include <iostream>

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

The equivalent is:

#include <iostream>

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

The scope of a is the block in which it is declared, so it is not in scope by the time it is used.

Perhaps your original example was meant to be:

#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.

David

Honestly, you’d have more luck on dissecting the C++ standard at comp.lang.c++, not here.

cheers,
–renato

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@linaro.org>

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.

We are all programmers...

You shouldn't trust a "compiler guy" more than folks at comp.lang.c++,
because compiler engineers write all parts of the compiler, and even at the
clang list, there are those that are not that much into dissecting the C++
standard more than they already have to on their normal day-to-day job.

On the other hand, at comp.lang.c++, you'll find many C++ zealots, which
will love to break up your code into tiny pieces and explain, with
references to the standard, for each one. You'll have more luck there, for
this sort of thing.

Here you should make questions about C++ if they're related to Clang, ie.
"it works with CompilerX, but not with Clang" or "Clang generated the wrong
IR for this C++ code" and so on.

cheers,
--renato