Thread: #ifndef vs. #if !defined

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    #ifndef vs. #if !defined

    Hello all,
    I was coding one day a while back, and discovered that #ifndef DEFINITION_NAME was not supported in the compiler I was using through the IDE Code::Blocks, IIRC, and therefore I was forced to change it to #if !defined DEFINITION_NAME instead. I was wondering if the #if !defined version is supported in every C++ standard compliant compiler, in which case I will just always use the #if !defined version.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That's odd. Perusing the relevant chapter of the standard, both are to be supported as part of the language. Neither

    #ifndef

    or

    #if !defined ( identifier )

    is an extension to the preprocessor. If one doesn't work, use the other, but there's no good answer here.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    That's odd. Perusing the relevant chapter of the standard, both are to be supported as part of the language. Neither

    #ifndef

    or

    #if !defined ( identifier )

    is an extension to the preprocessor. If one doesn't work, use the other, but there's no good answer here.
    Hmm...is there a way to detect which version the compiler which compiles the code supports, and use the one that is, or if both are supported, use the version I pick for default?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I really doubt you are seeing what you think you are seeing.

    If not, throw the compiler in the garbage.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by brewbuck View Post
    I really doubt you are seeing what you think you are seeing.

    If not, throw the compiler in the garbage.
    Ok, maybe you're right. I just tested it again in the compiler of Code::Blocks, and #ifndef worked just fine. Maybe I was mistaken, though I could have sworn I saw a compiler error at that a while back.

    Oh well. I guess I'll stick with #ifndef then for all header files then, just to be consistent.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM