#ifndef vs. #if !defined

This is a discussion on #ifndef vs. #if !defined within the C++ Programming forums, part of the General Programming Boards category; Hello all, I was coding one day a while back, and discovered that #ifndef DEFINITION_NAME was not supported in the ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    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, 03:25 AM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01: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, 06:16 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 03:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21