Thread: Header question

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    126

    Header question

    As most of you know, you will get 'Fuction already defined' or 'Class already defined' errors if a self-made header is included more than once in a project. The way I have been preventing this thus far is to use a structure like this:

    Code:
    #ifndef MY_HEADERNAME_HEADER_INC
    
    //All of the function protos, classes, etc.
    
    #define MY_HEADERNAME_HEADER_INC
    #endif
    This feels a bit hackish to me. Is there a better way to do it, or how do you personally prevent multiple input from your headers? Thanks for your time.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    For asfar as i know this is the best way of doing it. mostly because these commands are interpretid by the compiler itself not during run time or anything alike. it is the most common and best way of doing this for asfar as i know.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I think there might be another command like #pragma once, but AFAIK, using the #ifndef method is the most common way of doing it.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This is what MSVC6.0 generates:

    #if !defined(AFX_CALCULATOR_H__16A86382_5D9A_11D7_8F7A _444553540000__INCLUDED_)
    #define AFX_CALCULATOR_H__16A86382_5D9A_11D7_8F7A_44455354 0000__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000


    #endif // !defined(AFX_CALCULATOR_H__16A86382_5D9A_11D7_8F7A _444553540000__INCLUDED_)

    The #pragma once directive means the file doesn't have to even be opened again which is quicker than having to reopen the file and testing the symbol everytime the compiler comes across an include for it.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    I use gcc.

    I tried using #pragma once and it warned me that #pragma once is obsolete...? Hmm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header File question
    By vishalag in forum C Programming
    Replies: 1
    Last Post: 06-17-2009, 07:25 AM
  2. Header files question
    By Programmer_P in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2009, 01:16 PM
  3. Linux header question
    By invisibleghost in forum Linux Programming
    Replies: 5
    Last Post: 02-17-2005, 10:03 AM
  4. Replies: 6
    Last Post: 04-02-2002, 05:46 AM
  5. header file question
    By unanimous in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 08:15 PM