shaanxxx said:
pete said:
Joe said:
pete wrote:
Scope is related to linkage.
Lifetime has nothing to do with linkage.
I think of linkage as how the linker can rationalize two or more
translation units. I see scope as having to do with visibility rather
than linkage.
What do you think linkage is?
N869
6.2.2 Linkages of identifiers
[#1] An identifier declared in different scopes or in the
same scope more than once can be made to refer to the same
object or function by a process called linkage. There are
three kinds of linkage: external, internal, and none.
I have file a f1.c which contain
int i =0 ;
and i have a file f2.c which contains
extern int i;
int fx()
{ return i}
If i combine this two file together (compile and link). things will
work fine .
now i change file f1.c to
static int i =0;
file f2 will compile. and i will get linkage error.
Because you have removed external linkage of int i if f1.c, the
declaration extern int i in f2.c cannot be resolved.
As per my understand . identifier `i` was in scope of file f2.c so f2.c
got compiled.
Yes.
since object has internal linkage, i got error at the time of linking.
Yes, internal linkage in f1.c and extern in f2.c and the linker cannot
resolve int i.
correct me if i am wrong , " linkage defines where object is available.
scope defines where 'identifier name' is available."
Actually they both have to do with names. Linkage has to do with using
functions and variables in other modules or libraries. During the course
of compiling and assembling a particular translation unit, the assembler
figures out which functions and variables are referred to but not
defined in the current translation unit. These unresolved references are
recorded in a symbol table, part of the object file.
Any number of translation units are treated this way and produce object
files with symbol tables.
The last step of building our program is linking. The linker combines
the object files and any library files resolving references as it goes.
Definately, we cant link to object without using identifier name,
like if i remove 'extern int i' statement from f2.c.
any comments ?
But scope is different. It has to do with whether a particular
identifier can be seen from where you are.