MinGW - __cplusplus not being defined?

This is a discussion on MinGW - __cplusplus not being defined? within the Tech Board forums, part of the Community Boards category; I need your help tracing the source of this problem. A recent thread on the C++ forum about the NULL ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435

    MinGW - __cplusplus not being defined?

    I need your help tracing the source of this problem. A recent thread on the C++ forum about the NULL keyword shown a problem with the macros being defined on my MinGW installation. I'm confident this is my problem. Not the implementation's. I just need to understand what I'm doing wrong.

    Problem:
    The following C++ code generates warning: converting to non-pointer type `int' from NULL
    Code:
    #include <cstdlib>
    int main() { int p = NULL; }
    The code is compiled with:
    mingw32-g++.exe -c -O0 -o "C:/test/Debug/main.o" -IC:/MinGW/lib/gcc/mingw32/3.4.5/include/
    -IC:/MinGW/include/c++/3.4.5/backward/
    -IC:/MinGW/include/c++/3.4.5/mingw32/
    -IC:/MinGW/include/c++/3.4.5/
    -IC:/MinGW/boost/include/boost-1_33_1/
    -IC:/MinGW/include/sqlite/
    -IC:/MinGW/include/
    "main.cpp" -Wall -Wextra -ansi -pedantic -g3
    Delving inside the mingw header files reveals that <cstdlib> includes <cstddef>. And this last one includes stddef.h. And this is what I find around the macro definition for NULL:
    Code:
    #if defined (_STDDEF_H) || defined (__need_NULL)
    #undef NULL		/* in case <stdio.h> has defined it. */
    #ifdef __GNUG__
    #define NULL __null
    #else   /* G++ */
    #ifndef __cplusplus
    #define NULL ((void *)0)
    #else   /* C++ */
    #define NULL 0
    #endif  /* C++ */
    #endif  /* G++ */
    #endif	/* NULL not defined and <stddef.h> or need NULL.  */
    #undef	__need_NULL
    It seems __cplusplus is not being defined anywhere and NULL is being defined in the C form.

    Do you have any idea what is wrong?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    __cplusplus ought to be defined by the compiler itself whenever it does C++ compilation.

    What happens when you compile this?
    Code:
    #ifdef __cplusplus
    #error cplusplus is defined
    #else
    #error cplusplus is NOT defined
    #endif
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Good call CornedBee!

    I get "main.cpp:4:2: #error cplusplus is defined", which is even more strange... some problem in my order of includes?

    I went further and also checked for _STDDEF_H and __need_NULL. _STDDEF_H is defined. So, it must be defining NULL elsewhere.

    EDIT: Ok... __GNUG__ is defined. This means NULL is defined as:

    Code:
    #define NULL __null
    Grep shows nothing on __null and GCC manual only reference to it is:

    -Wstrict-null-sentinel (C++ only)
    Warn also about the use of an uncasted NULL as sentinel. When compiling only
    with GCC this is a valid sentinel, as NULL is defined to __null. Although it is
    a null pointer constant not a null pointer, it is guaranteed to of the same size
    as a pointer. But this use is not portable across different compilers.
    Why is GNUG defined? I'm using g++.
    Last edited by Mario F.; 12-04-2006 at 05:43 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Next two experiments:
    Code:
    #ifdef __GNUG__
    #error GNUG is defined
    #else
    #error GNUG is NOT defined
    #endif
    If it is defined, check the warning that this produces:
    Code:
    int main()
    {
      int i = __null;
    }
    Looks like GCC has something like nullptr as an extension already.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    That's an idea alright. __null gives the same warning as expected. __GNUG__ is defined (check last post edit. Was too slow and your reply came before).

    Since __null is not defined anywhere, surely it's a compiler extension.
    I'm only slightly confused now at GNUG being defined. I didn't expect g++ to define it. If you take a second look at the code inside stddef.h, you see this:

    Code:
    #ifdef __GNUG__
    #define NULL __null
    #else   /* G++ */
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Nevermind. Found the answer in the cpp.info file. GNUG is for g++ and GNUC is for gcc. Both are GNU extensions.

    Thanks for the help CornedBee.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 05:20 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