473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of #ifndef in header files

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_*/
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
Apr 23 '07 #1
6 50211
Johs wrote:
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_*/
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
yes, in order to prevent multiple inclusion.

Regards,

Zeppe
Apr 23 '07 #2
Johs :
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_*/
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
C++ Primer, Fourth Edition
By Stanley B. Lippman, Josée Lajoie, Barbara E. Moo

2.9. Writing Our Own Header Files

Avoiding Multiple Inclusions

--
Thank you very much! :)
Thank this newsgroup very much! :)
Visual Studio 2005 Professional Edition
Windows XP Professional
Apr 23 '07 #3
On Apr 23, 1:51 pm, Johs <a...@asd.comwr ote:
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_*/

where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
It helps the linker a lot.This decreases build time and risk of facing
multiple inclusion related errors.

Apr 23 '07 #4
"terminator " wrote:

>#ifndef FUNCS_H_
#define FUNCS_H_

// here goes all the code

#endif /*FUNCS_H_*/

where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?

It helps the linker a lot.This decreases build time and risk of facing
multiple inclusion related errors.
So the linker knows about the include files?
Apr 23 '07 #5
"osmium" <r1********@com cast.netwrote in news:59444oF2j4 93pU1
@mid.individual .net:
"terminator " wrote:

>>#ifndef FUNCS_H_
#define FUNCS_H_

// here goes all the code

#endif /*FUNCS_H_*/

where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?

It helps the linker a lot.This decreases build time and risk of facing
multiple inclusion related errors.

So the linker knows about the include files?
and about preprocessor directives?
Apr 23 '07 #6
On Apr 23, 12:51 pm, Johs <a...@asd.comwr ote:
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_*/
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
It depends. You need some sort of include guard, but such a
simple naming convention runs a great risk of name collision,
e.g. in cases like:

#include "myLib/funcs.h"
#include "yourLib/funcs.h"

Within a project or a single company, you'll probably want to
extend the convention to include the subsystem name as well:

#ifndef CompanyName_myL ib_funcs_h
#define CompanyName_myL ib_funcs_h
// ...
#endif

For code meant to be used as a third party library, you'll
likely want to go even further; I use something like:

name=` basename "$filename" `
guard1=${prefix }` basename "$filename" | sed -e 's:[^a-zA-
Z0-9_]:_:g' `
guard2=`date +%Y%m%d%H%M%S`
guard3=`od -tx1 -N 16 /dev/random | head -1 | sed -e 's/
^[0-9]* //' -e 's/ //g' | tr '[a-f]' '[A-F]'`
guard=${guard1} _${guard2}${gua rd3}

echo
echo "#ifndef $guard"
echo "#define $guard"
echo
echo "#endif"
echo "// Local Variables: --- for emacs"
echo "// mode: c++ --- for emacs"
echo "// tab-width: 8 --- for emacs"
echo "// End: --- for emacs"
echo "// vim: set ts=8 sw=4 filetype=cpp: --- for vim"

to generate such headers myself. (I've deleted the code which
generates the copyright, but of course, that will normally be
inserted automatically as well.)

I'm not familiar with eclipse, but I would imagine that you can
configure its editor to generate something along these lines as
well. (The above is part of a Unix shell script, which I've
configured my editor to execute whenever it opens a .hh file
which doesn't already exist.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 24 '07 #7

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

Similar topics

16
12547
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it because it is, as the authors love to say, "implementation specific". If that's the case, how does the compiler commonly handle them? I use Linux and gcc specifically. Basically, I don't understand how a header file being included makes a...
7
1363
by: Daniel | last post by:
I think this question will be really easy or really hard to answer. I have two classes, TestAction and TestObject. Each TestObject has a pointer to a TestAction, and vice versa. So, both header files need the #ifndef compiler directives so there will not be an infinite loop of text substitutions. The problem is, when I use the #ifndef directives, my code won't compile, and if I take them out, my compiler crashes because it tries to...
18
2255
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and the implementation of its member functions in foo.cpp (which obviously #inludes foo.h) and further assume that it depends on a class bar which is defined in bar.h. It seems that there are the following two scenarios:
11
5599
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how does that get linked to our program when we run it??I would appreciate any helpful info..And I would like to thank you for that in advance -ambika
8
3050
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
8
2820
by: Eric | last post by:
I am writing a program with several header files. I would like for some of them to be aware of others. Here is a small example of my problem in three different files: //// main.c++ #include "A.h" #include "B.h" int main () { using namespace std;
36
3853
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
7
1759
by: David T. Ashley | last post by:
Are the standard header files (<stdio.hfor example) always safe to include more than once? Is this just good programming practice on the part of the average person who writes these things, or is this guaranteed in a standard? ------------------------------------------------------------ David T. Ashley (dta@e3ft.com) http://www.e3ft.com (Consulting Home Page) http://www.dtashley.com (Personal Home Page)
1
1380
by: johnnash | last post by:
ok so i was just reading about #ifndef and header files. I have a few doubts. It would be great help if someone can clear my doubts.. #ifndef A_H #define A_H .... code ..... #endif
3
1492
by: Kermit Mei | last post by:
Hello all, I wrote four simple c++ source files for this test. Look please: /*************1***************/ /// file A.h #ifndef A_H #define A_H class A { public: void set(int i);
0
9538
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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...
0
9975
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9788
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.