473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 8012
On Sat, 07 Feb 2004 19:40:34 GMT, DrUg13 <li******@dieyoung.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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

"DrUg13" <li******@dieyoung.com> wrote in message
news:9p********************************@4ax.com...
#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******@dieyoung.com> wrote in message
news:9p********************************@4ax.com...
#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
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...
3
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...
6
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
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...
9
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...
5
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 ...
6
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...
6
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
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);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.