【发布时间】:2018-05-18 10:18:31
【问题描述】:
对于微控制器,我将宏用于 HAL。现在,为了概括 HAL 的使用,我想做类似的事情
#define UART UART1
#if UART==UART1
# define PIN_TX 9
#elif UART==UART2
# define PIN_TX 2
#else
# warning "UART not correctly defined"
#endif
但是,UART1 是具有类型转换的内存地址(例如 (uint8_t*)0x004000000)。所以编译器会打印一些错误。
我做了一个简单的例子:
#include <stdio.h>
#define v1 (double)1
#define v2 (double)2
int main(int argc, char *argv[])
{
printf("We have: ");
#define VAL (v1)
#if VAL==v1
printf("VAL is 1\n");
#elif VAL==v2
printf("VAL is 2\n");
#else
# warning "VAL not 1 or 2"
printf("Not defined\n");
#endif
}
以下 cmets 也无法使用 gcc 编译:
cc preproc.c -o preproc
preproc.c: In function ‘main’:
preproc.c:3:20: error: missing binary operator before token "1"
#define v1 (double)1
^
preproc.c:10:14: note: in expansion of macro ‘v1’
#define VAL (v1)
^~
preproc.c:12:5: note: in expansion of macro ‘VAL’
#if VAL==v1
^~~
preproc.c:3:20: error: missing binary operator before token "1"
#define v1 (double)1
^
preproc.c:10:14: note: in expansion of macro ‘v1’
#define VAL (v1)
^~
preproc.c:14:7: note: in expansion of macro ‘VAL’
#elif VAL==v2
^~~
preproc.c:17:2: warning: #warning "VAL not 1 or 2" [-Wcpp]
#warning "VAL not 1 or 2"
^~~~~~~
<builtin>: recipe for target 'preproc' failed
make: *** [preproc] Error 1
但是,如果我删除 v1 和 v2 定义中的 (double),它会按预期编译和运行。
请注意,作为我提出的替代解决方案
#define USE_UART1
//#define USE_UART2
#if defined(USE_UART1)
# define UART UART1
# define PIN_TX 9
#elif defined(USE_UART2)
# define UART UART2
# define PIN_TX 2
#else
# warning "UART not correctly defined"
#endif
但这涉及另一个变量[编辑:技术上是一个宏,但实际上是我必须跟踪的另一组字符]。
我很想知道编译错误背后的原因和/或如果可能的话如何解决它。
【问题讨论】:
-
你的问题不清楚。你在寻找什么理由?您是否正在寻找为什么需要使用辅助标识符作为键的理由?那是因为预处理器是有限的。您是否正在寻找预处理器受限的原因?这是因为它是一个历史产物,旨在帮助构建和自定义源代码,而不是完全集成的语言的一部分。
-
我不知道您的最终解决方案有什么困扰。这对我来说似乎很合法。
-
您的直接问题是
double转换无法被评估#if的预处理器 解释。除此之外,我不知道你在问什么。 -
@EricPostpischil,我想了解它为什么不起作用。所以说理。如果可能的话,一种使其按预期工作的方法,但 Ajay 的回答似乎放弃了这种可能性。
-
@малинчекуров 它可以完成这项工作,但我觉得它不太优雅。我猜是味道和一切。
标签: c c-preprocessor preprocessor