c++compiler-errorscompilationc-preprocessorpreprocessor-directive

Preprocessor #if equality directive not working as expected


Could someone here please help me understand why I am seeing the following error rather than the GATEWAY error? Thank you.

root@GATEWAY-0x0000001E:~/gateway/experiments# cat compile_define.cpp
#define DEVICE_TYPE GATEWAY

#if DEVICE_TYPE == NODE
    #error "NODE"
#elif DEVICE_TYPE == GATEWAY
    #error "GATEWAY"
#endif
root@GATEWAY-0x0000001E:~/gateway/experiments# g++ compile_define.cpp -o compile_define.o
compile_define.cpp:4:6: error: #error "NODE"
    4 |     #error "NODE"
      |      ^~~~~
root@GATEWAY-0x0000001E:~/gateway/experiments# g++ --version
g++ (Ubuntu 13.2.0-4ubuntu3) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@GATEWAY-0x0000001E:~/gateway/experiments#

Solution

  • Fixed with:

    #define DEVICE_TYPE_GATEWAY 0
    #define DEVICE_TYPE_NODE    1
    
    #define DEVICE_TYPE DEVICE_TYPE_GATEWAY
    
    #if DEVICE_TYPE == DEVICE_TYPE_NODE
        #error "NODE"
    #elif DEVICE_TYPE == DEVICE_TYPE_GATEWAY
        #error "GATEWAY"
    #endif
    

    Thanks to 273k for the answer.