预处理器 #if 相等指令未按预期工作

问题描述 投票:0回答:1

这里有人可以帮助我理解为什么我看到以下错误而不是

GATEWAY
错误吗?谢谢你。

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#
c++ compiler-errors compilation c-preprocessor preprocessor-directive
1个回答
0
投票

修复:

#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

感谢273k的回答。

© www.soinside.com 2019 - 2024. All rights reserved.