linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* #include problem
@ 2004-10-15 16:48 C-16 
  2004-10-15 17:04 ` Eric Bambach
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: C-16  @ 2004-10-15 16:48 UTC (permalink / raw)
  To: linux-c-programming

Howdy,
i have a file main.c in my program that needs to include four headers
aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
include another header xxx.h . With this implementation i get a
"redefinition" compilation error. How can i solve this problem ? Thanks
in advance.

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

* RE: #include problem
@ 2004-10-15 16:55 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 9+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-10-15 16:55 UTC (permalink / raw)
  To: linux-c-programming


>Howdy,
>i have a file main.c in my program that needs to include four headers
>aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
>include another header xxx.h . With this implementation i get a
>"redefinition" compilation error. How can i solve this problem ? Thanks
>in advance.

each header file should have the form:

#ifndef <some symbol>
#define <some symbol>

... rest of header file code

#endif

where <some symbol> is a unique name across your project.  I tend to use
the form: _<filename>_h_.  The way this works is that the first time the
header is included the symbol is not defined so the header is processed, on 
subsequent inclusions the symbol is defined ad the header is not processed.

George

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

* Re: #include problem
  2004-10-15 16:48 #include problem C-16 
@ 2004-10-15 17:04 ` Eric Bambach
  2004-10-15 17:04 ` pdovera
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Eric Bambach @ 2004-10-15 17:04 UTC (permalink / raw)
  To: C-16; +Cc: linux-c-programming

On Friday 15 October 2004 11:48 am, C-16 wrote:
> Howdy,
> i have a file main.c in my program that needs to include four headers
> aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
> include another header xxx.h . With this implementation i get a
> "redefinition" compilation error. How can i solve this problem ? Thanks
> in advance.
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

You want to use whats called header guards.

-----------------aaa.h----------

#ifndef AAA_H
#define AAA_H

#include "xxx.h"

...some code

#endif
----------------end aaa.h---------
----------------xxx.h--------------

#ifndef XXX_H
#define XXX_H

.....some code

#endif
----------------end xxx.h----------

Notice that the endif must be the very last line of the file or you defeat the 
whole purpose of header guards. Others may or may not actually call them 
header guards, but thats the phrase I was taught. Its good practice to 
*always* use these in your header files. It solves many simple double 
inclusion and inclusion loop problems.

----------------------------------------
EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

		--Alan Cox 2000-12-08 

----------------------------------------

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

* Re: #include problem
  2004-10-15 16:48 #include problem C-16 
  2004-10-15 17:04 ` Eric Bambach
@ 2004-10-15 17:04 ` pdovera
  2004-10-15 17:12   ` Eric Bambach
  2004-10-15 21:15 ` Jan-Benedict Glaw
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: pdovera @ 2004-10-15 17:04 UTC (permalink / raw)
  To: C-16, linux-c-programming

C-16 wrote:

>Howdy,
>i have a file main.c in my program that needs to include four headers
>aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
>include another header xxx.h . With this implementation i get a
>"redefinition" compilation error. How can i solve this problem ? Thanks
>in advance.
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>  
>
Hi,
If I understand well the redefinition problem is generated by xxx.h, so 
you need to include xxx.h only once.

into each file aaa.h, bbb.h, ccc.h, try the following code:

#ifndef xxx_h
#include "xxx.h"
#endif

so you will include xxx.h only once !
xxx_h will not be included only the first time, the second time the 
ifndef will be false (since xxx.h is already included) and so on ...

Ciao,
Paolo Dovera



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

* Re: #include problem
  2004-10-15 17:04 ` pdovera
@ 2004-10-15 17:12   ` Eric Bambach
  0 siblings, 0 replies; 9+ messages in thread
From: Eric Bambach @ 2004-10-15 17:12 UTC (permalink / raw)
  To: pdovera; +Cc: linux-c-programming

On Friday 15 October 2004 12:04 pm, you wrote:
> C-16 wrote:
> >Howdy,
> >i have a file main.c in my program that needs to include four headers
> >aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
> >include another header xxx.h . With this implementation i get a
> >"redefinition" compilation error. How can i solve this problem ? Thanks
> >in advance.
>>
> Hi,
> If I understand well the redefinition problem is generated by xxx.h, so
> you need to include xxx.h only once.
>
> into each file aaa.h, bbb.h, ccc.h, try the following code:
>
> #ifndef xxx_h
> #include "xxx.h"
> #endif
>
> so you will include xxx.h only once !
> xxx_h will not be included only the first time, the second time the
> ifndef will be false (since xxx.h is already included) and so on ...

	This approach assumes he defines xxx_h in xxx.h. From the description I dont 
think he does. Anyways, this approach is backwards and the #ifndef business 
shouldn't be in each header file that needs xxx.h but rather in xxx.h itself. 
There have been many great replies to his problem that illustrate this. 
Imagine if he had 60-70 .cc/.h files needing xxx.h...... its impractical to 
maintain and write the #ifndefs in each file instead of writing it once in 
the xxx.h file.

> Ciao,
> Paolo Dovera
>
-- 
----------------------------------------
EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

		--Alan Cox 2000-12-08 

----------------------------------------

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

* Re: #include problem
@ 2004-10-15 17:29 C-16 
  0 siblings, 0 replies; 9+ messages in thread
From: C-16  @ 2004-10-15 17:29 UTC (permalink / raw)
  To: linux-c-programming

> On Friday 15 October 2004 11:48 am, C-16 wrote:
> > Howdy,
> > i have a file main.c in my program that needs to include four headers
> > aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
> > include another header xxx.h . With this implementation i get a
> > "redefinition" compilation error. How can i solve this problem ? Thanks
> > in advance.
> > -
> > To unsubscribe from this list: send the line "unsubscribe
> > linux-c-programming" in the body of a message to
majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> You want to use whats called header guards.
> 
> -----------------aaa.h----------
> 
> #ifndef AAA_H
> #define AAA_H
> 
> #include "xxx.h"
> 
> ...some code
> 
> #endif
> ----------------end aaa.h---------
> ----------------xxx.h--------------
> 
> #ifndef XXX_H
> #define XXX_H
> 
> .....some code
> 
> #endif
> ----------------end xxx.h----------
> 
> Notice that the endif must be the very last line of the file or you
defeat the 
> whole purpose of header guards. Others may or may not actually call them 
> header guards, but thats the phrase I was taught. Its good practice to 
> *always* use these in your header files. It solves many simple double 
> inclusion and inclusion loop problems.
> 
> ----------------------------------------
> EB
> 
> > All is fine except that I can reliably "oops" it simply by trying to
read
> > from /proc/apm (e.g. cat /proc/apm).
> > oops output and ksymoops-2.3.4 output is attached.
> > Is there anything else I can contribute?
> 
> The latitude and longtitude of the bios writers current position, and
> a ballistic missile.
> 
> 		--Alan Cox 2000-12-08 
> 
It worked !! :) thanks a lot

"Exploration is knowledge"

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

* Re: #include problem
  2004-10-15 16:48 #include problem C-16 
  2004-10-15 17:04 ` Eric Bambach
  2004-10-15 17:04 ` pdovera
@ 2004-10-15 21:15 ` Jan-Benedict Glaw
  2004-10-18  7:03 ` Ranga Makireddy
  2004-11-07  3:39 ` Wayne Wu
  4 siblings, 0 replies; 9+ messages in thread
From: Jan-Benedict Glaw @ 2004-10-15 21:15 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 1267 bytes --]

On Fri, 2004-10-15 17:48:47 +0100, C-16  <pmr17184@students.fct.unl.pt>
wrote in message <200410151648.i9FGmlqM026477@students.fct.unl.pt>:
> Howdy,
> i have a file main.c in my program that needs to include four headers
> aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
> include another header xxx.h . With this implementation i get a
> "redefinition" compilation error. How can i solve this problem ? Thanks
> in advance.

Could you please post the exact error message, along with the code lines
it belongs to? I think you've actually got two problems.

One was already explained by various list readers, you'd better use
appropriate header guards.

But "redefinition" and a new declaration are basically two different
things, so I think there's also the error around that your header file
possibly contains actual definitions of variable (instead of only
declarations for those...).

MfG, JBG

-- 
Jan-Benedict Glaw       jbglaw@lug-owl.de    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: #include problem
  2004-10-15 16:48 #include problem C-16 
                   ` (2 preceding siblings ...)
  2004-10-15 21:15 ` Jan-Benedict Glaw
@ 2004-10-18  7:03 ` Ranga Makireddy
  2004-11-07  3:39 ` Wayne Wu
  4 siblings, 0 replies; 9+ messages in thread
From: Ranga Makireddy @ 2004-10-18  7:03 UTC (permalink / raw)
  To: C-16; +Cc: linux-c-programming

Define your headers as follows in xxx.h file

#ifndef MY_HEADER
#define <my stuff>
#endif

Cheers,
Ranga


On Fri, 15 Oct 2004 17:48:47 +0100, C-16 <pmr17184@students.fct.unl.pt> wrote:
> Howdy,
> i have a file main.c in my program that needs to include four headers
> aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
> include another header xxx.h . With this implementation i get a
> "redefinition" compilation error. How can i solve this problem ? Thanks
> in advance.
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

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

* Re: #include problem
  2004-10-15 16:48 #include problem C-16 
                   ` (3 preceding siblings ...)
  2004-10-18  7:03 ` Ranga Makireddy
@ 2004-11-07  3:39 ` Wayne Wu
  4 siblings, 0 replies; 9+ messages in thread
From: Wayne Wu @ 2004-11-07  3:39 UTC (permalink / raw)
  To: linux-c-programming

C-16  <pmr17184 <at> students.fct.unl.pt> writes:

> 
> Howdy,
> i have a file main.c in my program that needs to include four headers
> aaa.h , bbb.h ,ccc.h and ddd.h . Each one of these headers need to
> include another header xxx.h . With this implementation i get a
> "redefinition" compilation error. How can i solve this problem ? Thanks
> in advance.
> -
> 
> 

you should use define a macro in xxx.h, for example __XXX_H_
then use the following codes in other headr files:
#ifndef __XXX_H_
#include "xxx.h"
#endif



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

end of thread, other threads:[~2004-11-07  3:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-15 16:48 #include problem C-16 
2004-10-15 17:04 ` Eric Bambach
2004-10-15 17:04 ` pdovera
2004-10-15 17:12   ` Eric Bambach
2004-10-15 21:15 ` Jan-Benedict Glaw
2004-10-18  7:03 ` Ranga Makireddy
2004-11-07  3:39 ` Wayne Wu
  -- strict thread matches above, loose matches on Subject: below --
2004-10-15 16:55 Huber, George K RDECOM CERDEC STCD SRI
2004-10-15 17:29 C-16 

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 NNTP newsgroup(s).