473,761 Members | 4,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifndef define question

#ifndef HEADER_H
#define HEADER_H

blah blal
#endif

So this tells the compiler That if its defined do not define it again.
Could someone help me understand this.

Does this mean that if I use the same clasee in two different classes
in the same exe with out the above, that the exe would be larger
because the code would be linked twice, or basically put into the code
twice??
Jul 22 '05 #1
5 8040
On Sat, 07 Feb 2004 19:40:34 GMT, DrUg13 <li******@dieyo ung.com> wrote
in comp.lang.c++:
#ifndef HEADER_H
#define HEADER_H

blah blal
#endif

So this tells the compiler That if its defined do not define it again.
Could someone help me understand this.
No, it means that if the preprocessor macro HEADER_H is already
defined, ignore the code up until the corresponding #endif. This
prevents the contents of a file from being processed more than once in
compiling the same source file even if it happens to be included more
than once.
Does this mean that if I use the same clasee in two different classes
in the same exe with out the above, that the exe would be larger
because the code would be linked twice, or basically put into the code
twice??


This usage, commonly called an "include guard", prevents the contents
in between the #ifndef and the #endif from being processed more than
once in any given source file. It will still be processed once only
for every individual source file in a program that includes it at
least once.

Note that proper usage for headers is that they should not define
actual variables or contain the bodies of any functions except for
inline member functions of classes. The definitions of type, such as
class definitions, and function prototypes do not in themselves
generate code or data.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

"DrUg13" <li******@dieyo ung.com> wrote in message
news:9p******** *************** *********@4ax.c om...
#ifndef HEADER_H
This means that if, at this point in translation (which is sequential),
there is no symbol 'HEADER_H' defined, then translate everything between
here and the next (matching) #endif. If 'HEADER_H' is already defined,
then skip down to the next (matching) #endif before continuing to translate.
#define HEADER_H

blah blal
#endif
So this tells the compiler That if its defined do not define it again.
It means that if it's defined, then everything between the #ifndef
and #endif should be skipped over by the compiler, not translated.
Could someone help me understand this.
See above.

Does this mean that if I use the same clasee in two different classes
in the same exe with out the above, that the exe would be larger
because the code would be linked twice, or basically put into the code
twice??


Nothing to do with 'exe' or linking. #ifdef, #ifndef, etc. are
'preprocessor
directives, which facilitate 'conditional compilation'.

The form above is often called a 'header guard' which allows one to
#include a header more than once in the same translation without
worry about duplications causing language violations.

-Mike
Jul 22 '05 #3
DrUg13 wrote:
#ifndef HEADER_H
#define HEADER_H

blah blal
#endif

So this tells the compiler That if its defined do not define it again.
Could someone help me understand this.

Does this mean that if I use the same clasee in two different classes
in the same exe with out the above, that the exe would be larger
because the code would be linked twice, or basically put into the code
twice??


No. The above is a so-called include guard. When you include a header
file, the #include line is replaced by the contents of that header. But
in any translation unit, a class must not be defined more than once. A
simple example:

// a.h
class A
{
};
// b.h
#include "a.h"

class B
{
A a;
}
// main.c
#include "a.h"
#include "b.h"

int main()
{
A a;
B b;
}

This won't compile, because a.h is #included twice in main, once
directly and once through b.h, and as I said above, class A must not be
defined more than once in main.c. If you put in the include guard, the
first inclusion of a.h will #define (e.g.) A_H, and when it's included
the second time, that #define is found, and so the contents of the
header are now skipped up to the corresponding #endif.
Jul 22 '05 #4

"DrUg13" <li******@dieyo ung.com> wrote in message
news:9p******** *************** *********@4ax.c om...
#ifndef HEADER_H
#define HEADER_H

blah blal
#endif

So this tells the compiler That if its defined do not define it again.
Could someone help me understand this.

Does this mean that if I use the same clasee in two different classes
in the same exe with out the above, that the exe would be larger
because the code would be linked twice, or basically put into the code
twice??


No nothing like that. Look at this

// file header.h

class AClass
{
};

// file header2.h

#include "header.h"

// file source.cpp

#include "header.h"
#include "header2.h"

Now file source.cpp is including header.h *twice*, once directly and once
via header2.h. So the compiler is going to see the declaration of AClass
twice. That is an error, you can't declare a class twice in one compilation
(even if its the same definition both times). This is the error that the
include guard prevents. The second time that the header file is included it
has already defined HEADER_H so it #ifndef stops the compiler from seeing
the class declaration a second time ON THE SAME COMPILATION (this is the bit
that newbies usually don't understand). If you had another source file which
also included header.h then of course the compiler when separately compiling
that second source file would see the class definition again, but that's OK,
its not an error.

John
Jul 22 '05 #5
This #ifdef pattern is just to guard against multiple inclusions of
the same header file.

The following article will help:

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9754
by: Evan | last post by:
Is there any standard for when to use #if !defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD? Aside from being able to combine multiple things into an #if, is there any difference? My VC++ docs say that thay are equivalent, but is there any convention on when to use one vs. another? Also, how widely is #pragma once supported?
3
17969
by: prettysmurfed | last post by:
Hi all I have this, probably stupid question, how to avoid multiple definitions when a header file is included more than once. I thought, when you wrote the header-file and used the ifndef/define directives, the code between the statements define and endif was only compiled once. But this seems to be incorrect, as I get a whole bunch of errors when I compile the critter. For example if I include header.h more than once the
6
3170
by: Peng Yu | last post by:
I want to define a macro #define FILEWR(FILENAME) which can be expanded to #ifndef OUTFILE #define OUTFILE ofstream out; #endif
25
4920
by: John Hanley | last post by:
I have a program where both my main.c and program.c files use the program.h file. So I #include "program.h" in both the .c files. The program.h file has #ifndef PROGRAM_H #define PROGRAM_H .... #endif Yet when I build my program, the DJGPP compiler tells me there are multiple definitions of each of my functions.
9
28687
by: Qiao Jian | last post by:
I am new to c. Today I just read an h file within which there is statements: #ifndef _RANDOM_H #define _RANDOM_H So what is the meaning or purpose of this statement? When should I use such statement in my own program? Thank you so much!
5
3661
by: vfunc | last post by:
Despite a #ifndef I am getting a redefinition error The offending .h file looks like the following #ifndef DISTRIB_H #define DISTRIB_H double dblpi; // this is getting compiled twice etc...
6
26450
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define DATE_TIME unsigned long #endif // NOT DATE_TIME The #define DATE_TIME I am handling with: public const UInt32 DATE_TIME;
6
50211
by: Johs | last post by:
Each time I make a new .h file in eclipse it starts with this: #ifndef FUNCS_H_ #define FUNCS_H_ // here goes all the code #endif /*FUNCS_H_*/
7
5450
by: plowgrammer2010 | last post by:
Hi, All this is written in most of header files but i dont know its mean.The statements of file prototyp.h are #ifndef PROTOTYP_H #define PROTOTYP_H ArrayAdd(LPARRAY,LPARRAY); ArraySubtract(LPARRAY,LPARRAY); #endif the upper bold words are. what is its purpose and where it is used
0
9353
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.