[comp.sys.ibm.pc] Separate Compilation in TurboC

hong@uicsrd.csrd.uiuc.edu (03/21/90)

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

richard@calvin.spp.cornell.edu (Richard Brittain) (03/22/90)

In article <42900068@uicsrd.csrd.uiuc.edu> hong@uicsrd.csrd.uiuc.edu writes:
>
>
>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

I have found code which does this (the ro text formatter recently posted).
It seems that most unix linkers allow multiply declared global variables
even though it is illegal - they presumably use the last version defined
and throw away the others.  Microsoft C/link emulates some of the bugs in the
unix version apparently (actually it is just the linker, not the compiler)
Fix.
In one file only, that includes the header containing this stuff, (say
main.c or wherever main() is)
#define MAIN
#include <offending.header.h>
#undef MAIN

Then in the offending header file, insert an "extern" at the start of each line
declaring a global variable, and put
#ifdef MAIN
#define extern
#endif
somewhere before this stuff, and #undef extern after it.

Richard Brittain,                   School of Elect. Eng.,  Upson Hall   
                                    Cornell University, Ithaca, NY 14853
ARPA: richard@calvin.spp.cornell.edu	
UUCP: {uunet,uw-beaver,rochester,cmcl2}!cornell!calvin!richard