Please support our C advertiser: Programming Forums
Views: 2363 | Replies: 11
![]() |
•
•
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation:
Rep Power: 4
Solved Threads: 50
A word on unresolved symbol:
This is the oppisite of "multiple definitions". In this case you have put extern declaration in some file, (so compiler is happy and leaves the work of finding it's definition to liner). But because none of the translation units you
May the concept of symbols and linkage and scope isn't clear to you.
- Say you have a.h/a.c b.h/b.c and main.c
- Say main.c #includes a.h and b.h and uses the functions declared there.
- You need to have a variable "int global_var ;" that you need to use in all 3 places (main, a, b).
So the question is where should you put int global_var ; ?
- You are going to compile 3 files a/b/main.c and create a/b/main.o. These 3 are your translation units (or program units as Ancient Dragon refer to it)
- You should define (for a non-static basic type independent variable declaration is also definition) only ONE int global_var; because you want all 3 of them to use the same variable not a different one.
- So what you do is, define it in ONE place and put some special statement in other two places that promises compiler that this variable will be defined at link time, so ignore for the time being.
- This special statement consists of keyword extern. When you prefix some declaration with this word it tells compiler that it is NOT defined in this translation unit, but it'll be defined at link time in one of the other translation unit with which I'll link this translation unit.
In short now to solve problem I described,
* put "int global_var;" in main.c.
* put "extern int global_var ;" in a.c
* put "extern int global_var ;" in b.c
A word on "multiple definitions of a symbol": When you miss out on the word extern, all your .c files will compile without errors, but now you will have 3 compiled units main/a/b.o each containing definition of a symbol global_var. So when linker is trying link them together it sees that same symbol (int global_var) is defined multiple times, which isn't allowed and so it cribs..
Whether you "directly put int global_var in a/b.c" or by "putting int global_var in a/b.h and then #including a/b.h in a/b.c" does not matter end result is the same (multiple definitions of same symbol at link time)
A word on "unresolved symbol":
This is the oppisite of "multiple definitions". In this case you have put extern declaration in some file, (so compiler is happy and leaves the work of finding it's definition to liner). But because none of the translation units you gave to linker for linking has defined it.
"extern int global_var" is declaration/promise.
"int global_var" is definition.
This is the oppisite of "multiple definitions". In this case you have put extern declaration in some file, (so compiler is happy and leaves the work of finding it's definition to liner). But because none of the translation units you
May the concept of symbols and linkage and scope isn't clear to you.
- Say you have a.h/a.c b.h/b.c and main.c
- Say main.c #includes a.h and b.h and uses the functions declared there.
- You need to have a variable "int global_var ;" that you need to use in all 3 places (main, a, b).
So the question is where should you put int global_var ; ?
- You are going to compile 3 files a/b/main.c and create a/b/main.o. These 3 are your translation units (or program units as Ancient Dragon refer to it)
- You should define (for a non-static basic type independent variable declaration is also definition) only ONE int global_var; because you want all 3 of them to use the same variable not a different one.
- So what you do is, define it in ONE place and put some special statement in other two places that promises compiler that this variable will be defined at link time, so ignore for the time being.
- This special statement consists of keyword extern. When you prefix some declaration with this word it tells compiler that it is NOT defined in this translation unit, but it'll be defined at link time in one of the other translation unit with which I'll link this translation unit.
In short now to solve problem I described,
* put "int global_var;" in main.c.
* put "extern int global_var ;" in a.c
* put "extern int global_var ;" in b.c
A word on "multiple definitions of a symbol": When you miss out on the word extern, all your .c files will compile without errors, but now you will have 3 compiled units main/a/b.o each containing definition of a symbol global_var. So when linker is trying link them together it sees that same symbol (int global_var) is defined multiple times, which isn't allowed and so it cribs..
Whether you "directly put int global_var in a/b.c" or by "putting int global_var in a/b.h and then #including a/b.h in a/b.c" does not matter end result is the same (multiple definitions of same symbol at link time)
A word on "unresolved symbol":
This is the oppisite of "multiple definitions". In this case you have put extern declaration in some file, (so compiler is happy and leaves the work of finding it's definition to liner). But because none of the translation units you gave to linker for linking has defined it.
"extern int global_var" is declaration/promise.
"int global_var" is definition.
Last edited by thekashyap : Mar 29th, 2007 at 3:22 am. Reason: Added unresolved part
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode