Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6253967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:06:50+00:00 2026-05-24T14:06:50+00:00

I am trying to check whether an #include <file.h> has already been declared using

  • 0

I am trying to check whether an #include <file.h> has already been declared using C++ macro.

Example

In file.h I do:

#ifndef FILE.H
#define FILE.H
class A
{
  //do routines 
};
#endif

In a second file called second.h I want to check if file.h has been included already.

Typical psuedo-code:

#ifndef "file.h"
#include "file.h"
#endif

#ifndef SECOND.H
#define SECOND.H
class second
{
  //do routine
};
#endif

I have tried a few #ifndef directives but no joy. Do anyone know how to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:06:51+00:00Added an answer on May 24, 2026 at 2:06 pm

    Everybody else has covered correct inclusion of include guard macros.

    File.h

    #ifndef   FILE_H
    #define   FILE_H
    // Code here
    #endif
    

    But everybody else missed the second part of your question on correct usage:

    This is an example of bad usage:

    #ifndef  FILE_H  
    #include "file.h"
    #endif
    
    #ifndef SECOND_H
    #define SECOND_H
    class second
    {
      //do routine
    };
    #endif
    

    This is not correct (though correct may be too strong).

    The include guards in each file should surround their entire content. So, in the second file, the #include "file.h" should be inside the include guards.

    Also you do not need to test for inclusion that will be done inside the file itself. Thus the file should look like this:

    #ifndef SECOND_H
    #define SECOND_H
    
    #include "file.h"
    
    class second
    {
      //do routine
    };
    #endif
    

    Also your macro guards are way too small. There is a high likelihood that these will be used by somebody else; this will cause all sorts of weird collisions.

    A good way to make the guards unique is to use a prefix, your full name (or nick name), or if you have your own registered website then uses its domain name. I then also add part of the directory hierarchy that includes the file.

    #ifndef    WEBSITE1_NAMESPACE1_NAMESPACE2_FILENAME_H
    #define    WEBSITE1_NAMESPACE1_NAMESPACE2_FILENAME_H
    
    #endif
    

    Another alternative (especially if you are working on Windows) is to generate a GUID. Just generate a new one for each file.

    Final point: only include other files from header files if their content is absolutely necessary. You only need to include "file.h" if this file has a type definition that class second depends on. If your class only uses that class as a pointer or a reference then prefer to use forward declaration rather including the header file.

    The scenarios in which you must #include are:

    • second is a child of first.
    • second has member(s) that are first objects
    • second has method(s) that take first objects as parameters
    • Second has method(s) that return first objects as a result

    Notice that I use the term "objects". If they are references or pointers then this does not count. In these cases you can use forward declaration to solve the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to check whether val() has a specific value or not inside a
I am trying to find out a way to check whether a file is
I'm trying to check whether or not the second argument in my program is
I'm trying to check whether a URL is from the same domain with jQuery
I'm new to LINQ and I'm trying to check whether a TEXT column is
I am trying to use the following regular expression to check whether a string
I'm trying to check whether the entered chars are digits or not with pattern
I am trying to determine whether a ring is contained by another ring using
I am trying to check whether all of the required info in a windows
I am trying to create an example, which would check the existence of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.