0

External variables are not listed by "nm" command because they have been declared as extern so memory for them will not be allocated in this program. Is there any other way to list extern variables? Where stored information about external variables declaration?

os windows 7 compiler mingw or vs2010

Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
  • For what purpose do you want externally available variables listed? I think and IDE debugger might be the way to go. – MartyE Aug 14 '12 at 10:47
  • 1
    No, extern symbols *are* listed by `nm`. After all, they are needed by the link editor. However, unless you link dynamically, the linking process will just resolve the symbol and the resulting executable won’t contain it any more. – Konrad Rudolph Aug 14 '12 at 10:47

1 Answers1

3

They will be there, marked U for undefined.

extern int foo;
int bar() {
  return foo++;
}

Gives:

g++ -c test.cc
nm test.o
00000000 T _Z3barv
         U foo

Note that bar is needed for this example to work. If the variable is unused no reference will be generated in the output.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • yes, if variable is unused no information will be generated. But where this information (that extern variable was declared in this file) is stored? – Alex Hoppus Aug 14 '12 at 11:09
  • @AlexHoppus it's an undefined external reference in the object code that's generated. Mechanics of that vary depending on output platform, but basically there's a list of symbols this object provides and symbols this object requires to link. This list is in (almost) every object file that your compiler produces. (On Linux x86_64 or -fPIC on ia32 it'll use an entry in the GOT - global offset table to find it). It doesn't store that a declaration was seen though, it stores that a symbol is needed, which is why if there's a type mismatch you're unlikely to get a warning. – Flexo Aug 14 '12 at 14:14