Path: chuka.playstation.co.uk!news From: sosman@terratron.com (Steven Osman) Newsgroups: scea.yaroze.programming.gnu_compiler Subject: Re: Linking Date: Fri, 03 Dec 1999 14:13:23 GMT Organization: PlayStation Net Yaroze (SCEE) Lines: 160 Message-ID: <3847cb8b.418073317@news.playstation.co.uk> References: <8275bv$jq3@scea> <38473a31.380863452@news.playstation.co.uk> <828gl1$jq6@scea> NNTP-Posting-Host: 209.27.57.69 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Newsreader: Forte Agent 1.5/32.452 Antony, The #ifndef will not help you in this case. As I mentioned in my previous post, if you include it in both .c files, it will be defined twice. The reason for this is that things like #defines are not actually compiled into the program. They represent macros, which are used DURING the compilation of a file, but that is where their life ends. consider this #define DOUBLE(x) (x*2) y=DOUBLE(q); When you compile this, the compiler treats it as: y=(q*2); and that is what goes into the object file. There is no reference to the macro DOUBLE whatsoever. As far as the #ifndef, etc. issue, think about it... You compile bg.c separately from main.c. Both of them need to know about Layout1, so you need to declare Layout1 in both files. (hence, you #include bg.h in both files, and the whole thing gets processed). If we use a more arbitrary example, let's say both bg.c and main.c use the printf command. Wouldn't printf need to be declared (not defined, just declared) in both of those file? If it wasn't, then how would bg.c or main.c know what parameters are acceptable for printf? What the #ifndef DOES help you with, is this scenario. Consider an include file called YarozeStuff.h: #ifndef YAROZESTUFF #define YAROZESTUFF #include "bg.h" #define WHATEVER 3 #endif And now, in your file main.c, you have the following: #include "bg.h" #include "yarozestuff.h" . . . You may not have been aware that YarozeStuff.h explicitly #includes bg.h for you, so you included it yourself. The #ifndef will make sure that the stuff in bg.h only gets processed the FIRST time it is #included (in this case, directly from bg.h). As the compiler processes yarozestuff.h, it will skip over the stuff in bg.h because of your #ifndef. Try what I suggested (by using the extern, etc...) and let me know if it works. Steven On Fri, 3 Dec 1999 08:33:31 -0500, "Antony Arciuolo" wrote: >I think BOTH should work because I got the #ifndif in there. So any 2nd >include of BG.h should be completely skipped, otherwise I'd get multiple >definitions for each and every file. but nope, just on statically init'ed >arrays. > >- Tony > >Steven Osman wrote in message >news:38473a31.380863452@news.playstation.co.uk... >> Antony, >> >> I would recommend using something to the effect of: >> >> extern u_short Layout1[6]; >> in the .h file, >> >> and putting the code you have written below in bg.c. I am guessing >> that you #include bg.h in both main.c and bg.c, correct? >> >> The problem is that each time a .c file #includes bg.h, they will >> define a new "copy" of Layout1, hence, your error. >> >> As for your latter comment, I'm not certain why >> u_short Layout1[6]; >> would work. To me, that looks like you are still redefining it. >> Maybe it has something to do with the way the linker works/ including >> statically initialized variables, and all...? (Still, I don't think >> EITHER should work). >> >> On Thu, 2 Dec 1999 20:26:11 -0500, "Antony Arciuolo" >> wrote: >> >> >I have an array to describe a layout (index) for a GsBG. >> > >> >I want to explicitly define it in the header of a BG module: >> > >> >#ifndef BG_H >> >#define BG_H >> > >> >u_short Layout1[] = >> > >> >GsCELLtransparentCell, GsCELLtransparentCell, >> >GsCELLtransparentCell, GsCELLtransparentCell, >> >GsCELLtransparentCell, GsCELLtransparentCell >> >}; >> > >> >... >> >#endif >> > >> >Now I want to compile using this makefile >> > >> > >> >PROGRAM = test.pxe >> >PXE_ADDR = 80140000 >> > >> ># To Optimize use -O3 instead of -g >> >OPTIONS = -c -g >> >LINKER = -Xlinker -Map -Xlinker mapfile.map -Xlinker -Ttext -Xlinker >$(PXE_ADDR) >> >DEPENDENCIES = bg.o main.o >> > >> >all: $(PROGRAM) >> > >> >$(PROGRAM): $(PROGOBJ) $(DEPENDENCIES) >> > gcc $(LINKER) $(DEPENDENCIES) -o $(PROGRAM) >> > >> >main.o: main.c mainTIMS.h Levels.h >> > gcc $(OPTIONS) main.c >> > >> >bg.o: bg.c bg.h Levels.h >> > gcc $(OPTIONS) bg.c >> > >> >The problem is: >> > >> >gcc -Xlinker -Map -Xlinker mapfile.map -Xlinker -Ttext -Xlinker 80140000 >bg.o main.o -o test.pxe >> >main.o(.data+0x0): multiple definition of `Layout1' >> >bg.o(.data+0x0): first defined here >> >make.exe: *** [test.pxe] Error 1 >> > >> >How can I solve this? It just does not seem to me that it is being >defined twice, and if I redeclare Layout1 as: >> > >> > >> >u_short Layout1[6]; >> > >> >Everything is fine. Why? >> > >> >- Tony >> > >> > >> > >> > >> >