0

I have a little trouble finding how do exactly the C++ compilers decide which symbol to be defined or undefined. What I mean is how do I know in a C++ source code if a variable/function will be compiled into a defined or undefined symbol. First I thought all defined variables/functions will be defined symbols, but seems like that's not what happens. Is there a way to exactly determine this considering static, extern, inline, ... keywords as well?

*update The question is not about definitions and declarations in C++, and not if my code will compile. Right now I think some functions/variables which are only declared in a C++ source become defined symbols if I compile the code and examine the object file with nm. I need this information : how does defined/declared C++ functions/variables compile to defined/undefined symbols to the object files (for example ELF).

cosinus
  • 38
  • 5
  • 2
    You should look up what the difference is between a declaration and definition in C++. Any good book will tell you. – Tony The Lion May 08 '12 at 13:03
  • It's not clear what you're asking here. The easiest way to find out if you have undefined symbols is to try compiling and linking your code. – Oliver Charlesworth May 08 '12 at 13:03
  • @OliCharlesworth: In many cases, if you have undefined references but no code which tries to use those symbols, the compiler & linker may remain silent. – John Dibling May 08 '12 at 13:50
  • The question is not about definitions and declarations in C++, and not if my code will compile. Right now I think some functions/variables which are only declared in a C++ source become defined symbols if I compile the code and examine the object file with nm. I need this information : how does defined/declared C++ functions/variables compile to defined/undefined symbols to the object files (for example ELF). – cosinus May 08 '12 at 14:52
  • another thing relevant: do not compile across different compilers as they generates different symbols. – xis May 08 '12 at 15:05

2 Answers2

0

If you only declare but don't define a variable/function it will not be defined.

// In global/namespace scope
int i; // defined
extern int ei; // not defined
extern int eid = 42; // assignment causes it to be defined

void foo(); // not defined
void bar() { } // defined
Motti
  • 110,860
  • 49
  • 189
  • 262
0

When you compile foo.cpp into foo.o, certain symbols defined in foo.cpp will be present in foo.o, others will be "inlined out". Assuming this is what you mean then you will find that this is compiler specific and implementation-defined. There is an interesting discussion regarding GCCs behaviour toward inline functions for example here:

http://gcc.gnu.org/onlinedocs/gcc/Inline.html

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319