[comp.sys.atari.st] MWC/CSD bug?

andyc@hplsla.HP.COM (Andy Cassino) (01/04/89)

I'm having trouble with the -VCSD option in Mark Williams C, version 3.05.

The problem was discovered trying to debug a complex GEM application,
but can be demo'd with some very simple files:

foo.h - local include file that defines global variable "foo"
mod1.c - source module which #includes foo.h
main.c - another source module which #includes foo.h

The program compiles and runs when I compile with "cc mod1.c main.c".

However, with "cc -VCSD mod1.c main.c" there will be a complaint
similar to:

  "ld: foo redefined in main.c"

The resulting object code does not run (hangs, warm-boot required!)
and CSD is all confused if I try to use it.

If I compile only one of the source modules with -VCSD the link is
okay and I can run CSD (but this means I can debug only one module at
a time, whoopee).

This doesn't happen when a header file like stdio.h is included in
both files.

So, somehow the extra debug stuff generated by -VCSD causes trouble in
the linker. Does anyone know of a workaround? (Yes, I'll be writing
MWC soon!).

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Andy Cassino                                                  %
    % uucp: hplabs!hplsla!andyc  domain: andyc%hplsla@hplabs.hp.com %
    % Hewlett-Packard              Lake Stevens Instrument Division %
    % 8600 Soper Hill Road                   Everett, WA 98205-1298 %
    % (206) 335-2211                                                %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

leo@philmds.UUCP (Leo de Wit) (01/05/89)

In article <5440009@hplsla.HP.COM> andyc@hplsla.HP.COM (Andy Cassino) writes:
|I'm having trouble with the -VCSD option in Mark Williams C, version 3.05.
|
|The problem was discovered trying to debug a complex GEM application,
|but can be demo'd with some very simple files:
|
|foo.h - local include file that defines global variable "foo"
|mod1.c - source module which #includes foo.h
|main.c - another source module which #includes foo.h
|
|The program compiles and runs when I compile with "cc mod1.c main.c".
|
|However, with "cc -VCSD mod1.c main.c" there will be a complaint
|similar to:
|
|  "ld: foo redefined in main.c"

It looks as if you try to define foo twice; that is not allowed in C,
although some compilers may be a bit more tolerant than I 8-). Your
foo.h entry for "foo" probably looks something like:

int foo;

The linker is confused and doesn't know which storage to use: that
created by the "mod1", or that created by the "main" definition.

The correct way is never to use include files for definitions, only
for external declarations (and symbol definitions and typedefs).

Short lesson:
An include file (module header file) is used to export the global
definitions of one module source file, or for common used definitions
(project / application wide, or standard header files). The file is
included by the defining module as well as by the modules that use the
external references. Don't use it for storage, either program or data.
End of short lesson ( 8-).

The best strategy (I think) is to declare foo in foo.h and define it in
one of the modules: typically the one that defines operations upon
it (note the difference between definition and declaration; I use the
K&R terminology). So if you put (mutatis mutandis) into foo.h:

extern int foo;

and in main.c:

int foo;

your problem should be solved. Yes, I know there are some compilers that
don't like a extern reference and a definition of a variable in the same
file; they're just broken.

Hope this helps - success!

										  Leo.

apratt@atari.UUCP (Allan Pratt) (01/05/89)

In article <5440009@hplsla.HP.COM> andyc@hplsla.HP.COM (Andy Cassino) writes:
> I'm having trouble with the -VCSD option in Mark Williams C, version 3.05.

> foo.h - local include file that defines global variable "foo"
> mod1.c - source module which #includes foo.h
> main.c - another source module which #includes foo.h

This is not strictly legal.  K&R section 11.2, on p. 206 (near the bottom):

	"[I]n a multi-file program, an external data definition without the
	'extern' specifier must appear in exactly one of the files.  Any other
	files which wish to give an external definition for the identifier
	must include the 'extern' in the definition."

Many compilers allow what you have done, and ANSI recognizes the "common
extension" that "int foo;" may appear in many source files and all refer
to the same storage, but strict ANSI C follows the quotation above. 

(In fact, the .common directive of most assemblers is what takes care of
this situation: the linker ultimately resolves all these declarations to
refer to the same storage.)

============================================
Opinions expressed above do not necessarily	-- Allan Pratt, Atari Corp.
reflect those of Atari Corp. or anyone else.	  ...ames!atari!apratt

andyc@hplsla.HP.COM (Andy Cassino) (01/07/89)

Many thanks to those that have replied with help on this problem, both here
and by e-mail. I'll be revising the program in question following the
suggestions given. 

Interestingly enough, the source which I am compiling is a package called 
"Digital Darkroom Software", copyright 1988 Bell Telephone Laboratories, Inc.
This was written at Murray Hill. I purchased the source code for appx. $25;
it is described in a book called "Beyond Photography - The Digital Darkroom"
published by Prentice-Hall. The author's name eludes me at the moment.

I thought maybe I'd pick up some good programming techniques from this stuff.
I guess I did, in a way!


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Andy Cassino                                                  %
    % uucp: hplabs!hplsla!andyc  domain: andyc%hplsla@hplabs.hp.com %
    % Hewlett-Packard              Lake Stevens Instrument Division %
    % 8600 Soper Hill Road                   Everett, WA 98205-1298 %
    % (206) 335-2211                                                %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%