LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2012, 02:40 AM   #1
mnowhere
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Rep: Reputation: Disabled
how #ifndef in header file works for multiple inclusion?


I have a header file with following contents,

Code:
#ifndef __MY_HEADER_
#defeine __MY_HEADER_

<..........header content......>

#endif
As per my understanding first time when the preprocessor includes this header in some .c file it checks if __MY_HEADER_ is defined, since its not defined it will include the following code into the .c file along with defining __MY_HEADER_.

my doubt is, when I include this header in another .c file, how would it takes __MY_HEADER_ definition. According to me, as __MY_HEADER_ is already defined the contents of the header file should not be included in any further .c files.

Please clarify me about this.
 
Old 08-07-2012, 03:05 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
No, the pre-processor does not know about __MY_HEADER_ in the other C-files; the pre-processor works on a single file (and the files that are included in that file) at a time.
 
Old 08-07-2012, 03:23 AM   #3
mnowhere
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi Wim Sturkenboom,

So you mean that the, "#ifndef __MY_HEADER_ #define __MY_HEADER_" is applied for every file freshly?
I mean the macro __MY_HEADER_ is kind of local to each .c files which includes the header file "my_header.h" (the header file containing the above mentioned code snippet)?
 
Old 08-07-2012, 05:23 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Yes, that's what I mean.
 
Old 08-07-2012, 09:05 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-08-2012, 09:57 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This construct is used when you have a lot interdependence between modules in a project. As an example, you may have a project with 3 C source files (this.c, that.c, theOther.c). Each C source file has a corresponding header file (this.h, that.h, theOther.h) which is #included in the .c source file.
In this.h, you could then have
Code:
#ifndef THIS_H
#define THIS_H

#include "that.h"
#include "theOther.h"

//...  Other declarations, etc. here
#endif
And in that.h, you could have
Code:
#ifndef THAT_H
#define THAT_H

#include "this.h"
#include "theOther.h"

//...  Other declarations, etc. here
#endif
Notice that without the conditionals, there would be a circular reference (this refers to that, which refers to this...), which would be a Bad Thing.

--- rod.

Last edited by theNbomr; 08-08-2012 at 10:02 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Header file inclusion jayadhanesh Linux - Software 2 05-15-2012 08:38 AM
[SOLVED] C++ multiple variable declaration error, even after I set #ifndef preprocessor chinho Programming 4 01-18-2011 04:37 AM
problem with inclusion of qt header files namus123 Linux - Desktop 3 06-04-2010 03:06 PM
problem with inclusion of qt header files namus123 Linux - Mobile 2 06-04-2010 05:34 AM
automatic header inclusion rajorshi Programming 2 11-02-2003 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration