public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Preprocessing in two or more C sources
@ 2002-07-10  9:58 Radu ION
  2002-07-10 16:09 ` Claudio Bley
  0 siblings, 1 reply; 2+ messages in thread
From: Radu ION @ 2002-07-10  9:58 UTC (permalink / raw)
  To: gcc-help

Hello

Problem : there are given 3 files : header.h, source1.c, source2.c. The
header is included in both source1.c and source2.c and contains some global
definitions of variabiles. After compilation, the linker complained that he
saw redefinitions.
So, the whole header.h is now enclosed by : #ifndef NAME #define NAME (on
separate lines) and #endif
The problem is still there. I can workaround this, but I don't understand.
My compile command was : gcc -Wall -ggdb source1.c source2.c -o runme
My logical assumption was that gcc should remember all the #define NAME
directives and behave accordingly on subsequent preprocessing on sources. It
appears that every source is preprocessed independently and so, the
preprocessing is done once per source file without state information being
exchanged (i.e. in source2.c NAME, should have already been defined) Is that
true ?

Does GNU Make know about this sort of problems ? I wonder if I can use make
to set up some 'project' enviroment like in MSVC++ for instance.

Thank you.
Radu


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Preprocessing in two or more C sources
  2002-07-10  9:58 Preprocessing in two or more C sources Radu ION
@ 2002-07-10 16:09 ` Claudio Bley
  0 siblings, 0 replies; 2+ messages in thread
From: Claudio Bley @ 2002-07-10 16:09 UTC (permalink / raw)
  To: gcc-help

>>>>> "Radu" == Radu ION <radu@racai.ro> writes:

    Radu> Hello Problem : there are given 3 files : header.h,
    Radu> source1.c, source2.c. The header is included in both
    Radu> source1.c and source2.c and contains some global definitions
    Radu> of variabiles. After compilation, the linker complained that
    Radu> he saw redefinitions.  So, the whole header.h is now
    Radu> enclosed by : #ifndef NAME #define NAME (on separate lines)
    Radu> and #endif The problem is still there. I can workaround
    Radu> this, but I don't understand.  My compile command was : gcc
    Radu> -Wall -ggdb source1.c source2.c -o runme My logical
    Radu> assumption was that gcc should remember all the #define NAME
    Radu> directives and behave accordingly on subsequent
    Radu> preprocessing on sources. It appears that every source is
    Radu> preprocessed independently and so, the preprocessing is done
    Radu> once per source file without state information being
    Radu> exchanged (i.e. in source2.c NAME, should have already been
    Radu> defined) Is that true ?

Yes, it is true that the files are processed independently of each
other. You shouldn't define instances of (global) variables in any
header file. You need to declare them 'extern' (forward declaration)
and define them only at one location.

    Radu> Does GNU Make know about this sort of problems ? 

I guess no, it's a programming problem.

    Radu> I wonder if I can use make to set up some 'project'
    Radu> enviroment like in MSVC++ for instance.

I never used MSVC++, I don't know.

Claudio

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-07-10 23:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-10  9:58 Preprocessing in two or more C sources Radu ION
2002-07-10 16:09 ` Claudio Bley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).