Include file conflict

When working with various header files, how can you prevent having to worry about which includes which, so you don't accidentally include something twice?

Is there some preprocessor command you can form?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In each include file put

#ifndef __YOUR_FILE_NAME_H_
#define __YOUR_FILE_NAME_H_

//Your .h file code

#endif //__YOUR_FILE_NAME_H_
As amit_g wrote - and 3rd party header files should already come with these so called 'header guards'. The idea basically is to use conditional compilation, once the compiler 'saw'

#define __YOUR_FILE_NAME_H_ the  1st time, it will skip subsequent sections that are enclosed in an '#ifdef __YOUR_FILE_NAME_H_/#endif sequence.

For more info, see also ("Header Guards"):

Header guards are implemented through the use of preprocessor directives. The C/C++ preprocessor directives all start with the # character. You are already familiar with some ( #include, #define). The preprocessor performs some simple textual replacements on a file before handing it off to the compiler.

Some if the preprocessor directives are conditional. The #ifdef SYMBOL directive is true when SYMBOL has been defined in the code seen so far. If the directive is true, then the statements that come between the #ifdef and an #endif directive later on will be used in the program. If the #ifdef is false, then the statements from that point on will be ignored and not sent to the compiler.

(A quick way to comment out large sections of code is to put a #if 0 at the beginning of the code to be commented and a #endif at the end.)

Another usefule preprocessor directive is #ifndef SYMBOL. This directive is true if the symbol has not been defined. Like other conditional directives, if the condition is true then the statements between the #ifndef and an #endif will be used in the program.

Header guards are implemented by using three preprocessor directives in a header file. Two are placed at the beginning of the file, before any pertinent code. The last is placed at the end of the file. The first header guard line is of the form

#ifndef MY_SYMBOL_H

and is followed immediately by the line

#define MY_SYMBOL_H

The line

#endif /* MY_SYMBOL_H */

is placed at the end of the file.

Important Point
To create header guards place
#ifndef unique_symbol
#define unique_symbol
at the beginning of the header file to be protected and #endif /* unique_symbol*/ at the end of the header

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Another shorter version that can be used in VC++ is

// header.h
#pragma once

// Your code comes here
>>>> #ifndef __YOUR_FILE_NAME_H_

Actually "__"  or "_" prefixes/suffixes shouldn't be used for private names or descriptors. They are reserved for names of the compiler's vendor and/or class libraries.

It would be better  you would use name like jkr has recommended  (MY_SYMBOL_H).

I personally use the headerfile name and replace the '.' by '_'.

#ifndef HEADER_H
#define HEADER_H


#endif   // HEADER_H

That allows to use the macro in the including sources as well:

#ifndef HEADER_H
    #include header.h

without having to look into the header file. The above statements prevent the compiler from (re)reading the header file when it already was read once.

As the #pragma once statement isn't portable I never used it.

Regards, Alex
It should have been

    #include "header.h"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.