From: Alex Vinokur 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: Sun, 09 Sep 2001 08:44:06 +0200 Organization: Scopus Network Technologies Lines: 113 Message-ID: <3B9B0FB6.CFDCBDBF@bigfoot.com> References: <3B957A12 DOT A177E7ED AT chipcity DOT com DOT au> <3B977B69 DOT B8DCB9B9 AT bigfoot DOT com> NNTP-Posting-Host: gateway.scopus.co.il (194.90.203.161) Mime-Version: 1.0 Content-Type: text/plain; charset=koi8-r Content-Transfer-Encoding: 7bit X-Trace: fu-berlin.de 1000014257 7562008 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 Alex Vinokur wrote: > 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. > > [snip] It is necessary to careful with extern declarations in C++ as well. Here is a new example. =============================== ======== Part#1. Files ======== =============================== Note. There is an error in program below : - main.c contains extern declaration of function foo() that takes no parameters; - soo2.c : 1) contains no function definition, 2) contains variable foo__Fv. ------ File main.c ------ extern int foo(void); int main() {return foo();} ------------------------- ------ File soo2.c ------ int foo__Fv; ------------------------- =========================================== gcc version 2.95.3 20010315/djgpp (release) Windows98 gpp : compiles and links C++ sources =========================================== =============================== ===== Part#2. C++-program ===== === Compilation and Linkage === =============================== %gpp main.c soo2.c C++ compiler/linker doesn't detect our error =============================== ===== Part#3. C++-program ===== ========== Analysis =========== =============================== %gpp -c main.c soo2.c %nm main.o soo2.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. soo2.o: 00000000 b .bss 00000000 d .data 00000004 ? .eh_frame 00000000 t .text 00000004 ? ___FRAME_BEGIN__ 00000000 t ___gnu_compiled_cplusplus 00000000 B _foo__Fv 00000000 t gcc2_compiled. We can see that both main.o and soo1.o contain the same symbol _foo__Fv. So, a linker has no problem with these object files. ------------- So, C++ compiler doesn't detect our error. Is it a bug in the C++ compiler? It seems it isn't. Does that problem have s solution ? =========================== Alex Vinokur mailto:alexvn AT bigfoot DOT com mailto:alexvnw AT yahoo DOT com http://up.to/alexvn http://go.to/alexv_math ===========================