[Webinar] Streamline your web hosting managementRegister Today


Prevent including a header file twice.

Posted on 1999-11-05
Medium Priority
Last Modified: 2013-11-18
How can I prevent a header file from being included twice. Are there some kind of header guards you can put on top of your file and what is the syntax?
Question by:HVDC
1 Comment

Accepted Solution

bcwhite earned 120 total points
ID: 2187770
Yes and No.

The standard, portable way to do this is by putting something like..

    #ifndef _MYHEADER_H
    #define _MYHEADER_H

    [... header body ...]


This will achieve, in effect, what you want.  Of course, the most compilers will still read the file.  They'll just ignore everything the second time.

Some compilers (GCC, for example) notice this structure and actually don't read the file the second time an long as all the body of the header file is between the ifndef/endif pair.

Other compilers support something like

    #pragma once

to be included in the file (along with the above standard) to say that this file should never be read more than once.  It's basically just a hint for the compiler.  Check your compiler manual for more information about supported pragmas.

Objective-C has an "#import" directive that functions like "#include" except that it will only include a given file once.  This is generally frowned upon since it should be up to the author of the header file to decide if it should be included more than once, not the job of the person writing the program that uses that header file.

So, the bottom line...  Use the "#ifndef" bit always (for portability).  Add the "#pragma" thing if you want.

-- Brian

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question