Robby
2009-12-02 17:25:01 UTC
Hello,
I am in the process of porting a large C program from a non- ansi C
compilant embedded compiler to an ansi C compliant emebedded compiler. I see
that the rules are not quite the same.
I am reviewing some specifics of extern in a book called "Teach yourself C"
3rd edition from Herbert Schildt and there is a short sample code on p.339
which explains the use of the extern specifier. Here is the sample code:
================================F1.c
#include <stdio.h>
int count;
void f1();
int main(void)
{
int i;
f1();
for(i=0; i<count; i++) printf("%d ", i);
return 0;
}
==============================
============================F2.c
#include <stdlib.h>
int count;
void f1(void)
{ count = rand(); }
===============================
According to the book, the above example is not supposed to work as it
explains on quote:
"If you declare count a second time, many linkers will report a duplicate
symbol error, which means that count is defined twice and the linker doesn't
know which to use."
Now, I tried this in VC++ as a simple .c program as shown above and I get no
errors or warnings and VC++ promts a build succeeded in the output box ???
But the book goes on to suggest that the correct way to do this is by using
the extern specifier in F2.c like this:
============================F2.c
#include <stdlib.h>
extern int count;
void f1(void)
{ count = rand(); }
===============================
This also compiles without errors or warnings. But why does the original
sample code (F1.c and F2.c) still work when the book says I should get an
duplicate symbol error?
All help appreciated! Excuse the simple question!
I am in the process of porting a large C program from a non- ansi C
compilant embedded compiler to an ansi C compliant emebedded compiler. I see
that the rules are not quite the same.
I am reviewing some specifics of extern in a book called "Teach yourself C"
3rd edition from Herbert Schildt and there is a short sample code on p.339
which explains the use of the extern specifier. Here is the sample code:
================================F1.c
#include <stdio.h>
int count;
void f1();
int main(void)
{
int i;
f1();
for(i=0; i<count; i++) printf("%d ", i);
return 0;
}
==============================
============================F2.c
#include <stdlib.h>
int count;
void f1(void)
{ count = rand(); }
===============================
According to the book, the above example is not supposed to work as it
explains on quote:
"If you declare count a second time, many linkers will report a duplicate
symbol error, which means that count is defined twice and the linker doesn't
know which to use."
Now, I tried this in VC++ as a simple .c program as shown above and I get no
errors or warnings and VC++ promts a build succeeded in the output box ???
But the book goes on to suggest that the correct way to do this is by using
the extern specifier in F2.c like this:
============================F2.c
#include <stdlib.h>
extern int count;
void f1(void)
{ count = rand(); }
===============================
This also compiles without errors or warnings. But why does the original
sample code (F1.c and F2.c) still work when the book says I should get an
duplicate symbol error?
All help appreciated! Excuse the simple question!
--
Best regards
Roberto
Best regards
Roberto