从编译传递的宏无法在 Eclipse 中解析

macro passed from compile cannot be resolved in eclipse

我正试图在编译时将定义的宏传递给我的程序,如下所示:

#include <stdio.h>
int main() {
    // KEY_CNT is not defined in program but passed at compile time
    printf("KEY_CNT: %d", KEY_CNT); 
    return 0;
}

并用 g++ -DKEY_CNT=2 -O2 test_3.cpp -o test_3 编译,它按预期工作,但问题是 Eclipse 无法解析 'KEY_CNT'。这只是一个玩具示例,在我原来的程序中,许多像这样带有红线的错误错误真的很烦人。

我认为 Eclipse 可能需要通知我的编译选项,以便它知道 'KEY_CNT' 已定义,所以我在 Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous 中添加 -DKEY_CNT=2,但它仍然没有工作。

有什么解决这个问题的建议吗?

========已添加========

Eclipse 版本:面向 C/C++ 开发人员的 Eclipse IDE,Luna Service Release 2 (4.4.2)

Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous

错误的地方。

应该是"C/C++ Build -> Settings -> GCC C++ Compiler -> Preprocessor"

刚刚验证它有效。