| delorie.com/archives/browse.cgi | search |
| From: | Alex Vinokur <alexvn AT bigfoot DOT com> |
| Newsgroups: | alt.comp.lang.learn.c-c++,comp.os.msdos.djgpp |
| Subject: | Re: use of extern -- please help |
| Followup-To: | alt.comp.lang.learn.c-c++ |
| Date: | Thu, 06 Sep 2001 15:34:33 +0200 |
| Organization: | Scopus Network Technologies |
| Lines: | 191 |
| Message-ID: | <3B977B69.B8DCB9B9@bigfoot.com> |
| References: | <3B957A12 DOT A177E7ED AT chipcity DOT com DOT au> |
| NNTP-Posting-Host: | gateway.scopus.co.il (194.90.203.161) |
| Mime-Version: | 1.0 |
| X-Trace: | fu-berlin.de 999779692 6137605 194.90.203.161 (16 [79865]) |
| X-Mailer: | Mozilla 4.7 [en] (Win98; I) |
| X-Accept-Language: | en |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
| Reply-To: | djgpp AT delorie DOT com |
Luke Vanderfluit wrote:
> Hi,
>
> I've been trying to work out how to use "extern"
>
> As far as I've been able to gather, I can compile 2 files seperately and
> get one to access the others variables/functions.
>
[snip]
It is necessary to careful with extern declarations in C.
Here is an example.
===============================
======== Part#1. Files ========
===============================
Note#1. There is an error in program below :
- foo() in main.c takes no parameters
- foo() in soo1.c takes one parameter
Note#2. See also Part#6
------ File main.c ------
extern int foo(void);
int main()
{
return foo();
}
-------------------------
------ File soo1.c ------
int foo (int a)
{
return a;
}
-------------------------
===========================================
gcc version 2.95.3 20010315/djgpp (release)
Windows98
gcc : compiles and links C sources
gpp : compiles and links C++ sources
===========================================
===============================
====== Part#2. C-program ======
=== Compilation and Linkage ===
===============================
%gcc main.c soo1.c
<COMMENT> No problem </COMMENT>
<NOTE>C-compiler/linker doesn't detect our error</NOTE>
===============================
===== Part#3. C++-program =====
=== Compilation and Linkage ===
===============================
%gpp main.c soo1.c
main.o(.text+0x7):main.c: undefined reference to `foo(void)'
collect2: ld returned 1 exit status
<NOTE>C++-compiler/linker does detect our error</NOTE>
===============================
====== Part#4. C-program ======
========== Analysis ===========
===============================
%gcc -c main.c soo1.c
%nm main.o soo1.o ### nm - list symbols from object files
main.o:
00000020 b .bss
00000020 d .data
00000000 t .text
00000000 t ___gnu_compiled_c
U _foo
00000000 T _main
00000000 t gcc2_compiled.
soo1.o:
00000020 b .bss
00000020 d .data
00000000 t .text
00000000 t ___gnu_compiled_c
00000000 T _foo
00000000 t gcc2_compiled.
<COMMENT>
We can see that both main.o and soo1.o contain
the same symbol _foo.
So, a linker has no problem with these object files.
</COMMENT>
===============================
===== Part#5. C++-program =====
========== Analysis ===========
===============================
%gpp -c main.c soo1.c
%nm main.o soo1.o
main.o:
00000020 b .bss
00000020 d .data
00000020 ? .eh_frame
00000000 t .text
00000020 ? ___FRAME_BEGIN__
00000000 t ___gnu_compiled_cplusplus
U _foo__Fv
00000000 T _main
00000000 t gcc2_compiled.
soo1.o:
00000020 b .bss
00000020 d .data
00000020 ? .eh_frame
00000000 t .text
00000020 ? ___FRAME_BEGIN__
00000000 t ___gnu_compiled_cplusplus
00000000 T _foo__Fi
00000000 t gcc2_compiled.
<COMMENT>
We can see that both main.o and soo1.o don't contain
the same symbol _foo.
main.o contains symbol _foo__Fv
(because foo() in main.c takes no parameters)
soo1.o contains symbol _foo__Fi
(because foo() in soo1.c takes one int-parameter)
So, a linker does have a problem with these object files.
</COMMENT>
===============================
====== Part#6. C-program ======
============ Style ============
===============================
Style used in Part#1 is correct but not recommended.
Here is a recommended style (using header file)
------ File hoo1.h ------
int foo (int a);
-------------------------
------ File main.c ------
#include "hoo1.h"
int main()
{
return foo();
}
-------------------------
------ File soo1.c ------
#include "hoo1.h"
int foo (int a)
{
return a;
}
-------------------------
%gcc main.c soo1.c
main.c: In function `main':
main.c:4: too few arguments to function `foo'
<COMMENT>
So, a compiler (not a linker) detects our error.
</COMMENT>
===========================
Alex Vinokur
mailto:alexvn AT bigfoot DOT com
http://up.to/alexvn
http://go.to/alexv_math
===========================
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |