preprocessor bug?

#define SYSYEM LINUX



#if SYSTEM == LINUX
#define HDR "linux.h"
#elif SYSTEM == WINDOWS
#define HDR "windows.h"
#elif SYSTEM == C51
#define HDR "c51.h"
#else
#define HDR "default.h"
#endif
#include HDR


I would expect this to work, but I just keep getting "warning C322: unknown identifier" errors. whats wrong?

Parents
  • Preprocessor macros always resolve to something, possible through multiple steps, even when "something" is really nothing. There's no such thing as a preprocessor "literal constant" where the preprocessor stops substituting.

    #define SYSTEM LINUX

    means "replace all occurrences of SYSTEM with LINUX. Then go back and replace all occurrences of LINUX with whatever LINUX is #defined to be. If LINUX isn't #defined, replace it with nothing."

    So, if there's no definition for LINUX,

    #if SYSTEM == LINUX

    turns into

    #if LINUX ==

    which then turns into

    #if ==

    after macro substitution, which isn't very useful.

    Wait, I know what you'r about to do:

    #define SYSTEM "Linux"
    #if SYSTEM == "Linux"

    But the preprocessor cannot compare string constants. Not even the stringizing operator can save you. Don't even try it :)

    You'll have to have a standard header with something like:

    #define LINUX 1
    #define WINDOWS 2
    #define RTX51_TINY 3
    #define UCOS 4

    and then you'll be able to write:

    #include <osdefs.h>

    #define SYSTEM LINUX

    #if SYSTEM == LINUX

    to get the effect that you want.

    (The "#define SYSTEM LINUX" most likely belongs in your build environment, not in the source code. It doesn't help portability if you have to edit this one line of text. Pass the definition for SYSTEM in on the compiler command line instead, and then you don't have to touch the code at all.)

Reply
  • Preprocessor macros always resolve to something, possible through multiple steps, even when "something" is really nothing. There's no such thing as a preprocessor "literal constant" where the preprocessor stops substituting.

    #define SYSTEM LINUX

    means "replace all occurrences of SYSTEM with LINUX. Then go back and replace all occurrences of LINUX with whatever LINUX is #defined to be. If LINUX isn't #defined, replace it with nothing."

    So, if there's no definition for LINUX,

    #if SYSTEM == LINUX

    turns into

    #if LINUX ==

    which then turns into

    #if ==

    after macro substitution, which isn't very useful.

    Wait, I know what you'r about to do:

    #define SYSTEM "Linux"
    #if SYSTEM == "Linux"

    But the preprocessor cannot compare string constants. Not even the stringizing operator can save you. Don't even try it :)

    You'll have to have a standard header with something like:

    #define LINUX 1
    #define WINDOWS 2
    #define RTX51_TINY 3
    #define UCOS 4

    and then you'll be able to write:

    #include <osdefs.h>

    #define SYSTEM LINUX

    #if SYSTEM == LINUX

    to get the effect that you want.

    (The "#define SYSTEM LINUX" most likely belongs in your build environment, not in the source code. It doesn't help portability if you have to edit this one line of text. Pass the definition for SYSTEM in on the compiler command line instead, and then you don't have to touch the code at all.)

Children
More questions in this forum