Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!sdsu!ucselx.sdsu.edu!petunia!polyslo!kwong From: kwong@polyslo.CalPoly.EDU (Ka Chin Wong) Newsgroups: comp.sys.ibm.pc Message-ID: <26096a1b.51fa@polyslo.CalPoly.EDU> Date: Fri, 23 Mar 90 0:13:15 GMT Reply-To: kwong@polyslo.CalPoly.EDU (Ka Chin Wong) Organization: Cal Poly State University -- San Luis Obispo Distribution: na Lines: 89 > Hi, I have trouble porting a C program from UNIX to the TurboC. > It contains several header files and source files. Some header > files appear in more than one sources file. The source files are > compiled separately. At link time, tcc or tlink complain that > all variables in the above header files are defined more than once. > The same program runs OK under UNIX using a similar makefile. > Is there any EASY way to get around with this? I have the TurboC > manual with me, but it doesn't seem helpful. How does MSC handle > this problem? Thank in advance. > Hong-Men Su Usually, multiple occurrances of header files cause no problem. Problem arise if the follow occurs: header.h ------------------------------------- char data[100]; ... ------------------------------------- source1.c ------------------------------------- #include "header.h" ... ------------------------------------- source2.c ------------------------------------- #include "header.h" ... ------------------------------------- when source1.c and source2.c are compiled separately there are no problem. But when you want to link them together you have linker error message saying multiple declaration of global variable 'data'. Think of it this way, during compilation, header.h is merged into both source1.c and source2.c. Each source file declares a global variable. Unfortunately, C ( not Turbo C or MSC, but C ) does not allow global variables to be declared more than once. If Microsoft C lets you get away with it, it has a BUG! The proper way to do it is to have all global variables declared as extern in header files. Then you declare this global variable in only ONE of your source files as below: header.h ---------------------------------- extern char data[]; ... ---------------------------------- source1.c ---------------------------------- #include "header.h" ... ---------------------------------- source2.c ---------------------------------- #include "header.h" ... char data[100]; ... ---------------------------------- This guarentees to work. The C compiler you used in UNIX probably does not conform to the new ANSI standard. Try following the above suggestion and your code will be more portable. Rick Wong Computer Science California Polytechnic State University