【问题标题】:Missing binary operator before token "long"标记 \"long\" 之前缺少二元运算符
【发布时间】:2022-11-13 04:04:30
【问题描述】:

我正在尝试在我的 Arduino Mega 2560 上实现 FreeRTOS,在此过程中,我遇到了 2 个我不理解的错误。而且不知道怎么修。希望这里有人这样做。第一个错误是missing binary operator before token "long"

错误指向 FreeRTOSConfig.h 中的以下行:

#define configCPU_CLOCK_HZ ((unsigned long) 16000000)

这是构建的输出:

C:\Projects\src\config\FreeRTOSConfig.h(36,39): error: missing binary operator before token "long"
     #define configCPU_CLOCK_HZ ((unsigned long) 16000000)
                                           ^

此定义仅在 port.c 中使用,它还有另一个我无法修复的错误。此错误来自自动生成的 makefile。不确定这两个错误是否相关,但如果有人知道如何修复它,最好在此处添加。错误是:

recipe for target 'src/Core/FreeRTOS/port.o' failed

这就是我在 Makefile 中找到的:

src/Core/FreeRTOS/port.o: ../src/Core/FreeRTOS/port.c
@echo Building file: $<
@echo Invoking: AVR/GNU C Compiler : 5.4.0
$(QUOTE)C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe$(QUOTE)  -x c -DDEBUG -DBOARD=USER_BOARD  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include" -I"../src/ASF/common/boards/user_board" -I"../src/ASF/common/boards" -I"../src/ASF/mega/utils/preprocessor" -I"../src/ASF/mega/utils" -I"../src/ASF/common/utils" -I"../src" -I"../src/config"  -O1 -fdata-sections -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -mrelax -g3 -Wall -mmcu=atmega2560 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega2560" -c -std=gnu99 -fno-strict-aliasing -Wstrict-prototypes -Wmissing-prototypes -Werror-implicit-function-declaration -Wpointer-arith -mrelax -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)"   -o "$@" "$<" 
@echo Finished building: $<

【问题讨论】:

  • 这条线看起来完全正常。如果没有更广泛的背景,就不可能说任何话。
  • #define configCPU_CLOCK_HZ 16000000UL 呢?
  • -save-temps 添加到命令行选项并查看预处理输出(*.i 用于 C,*.ii 用于 C++,*.s 用于汇编)。
  • @datafiddler 你能解释为什么这会起作用,而“unsigned long”不会吗?因为这样它确实可以正确编译

标签: c++ arduino freertos microchip


【解决方案1】:

missing binary operator before token "long"

当您尝试在预处理器指令中使用宏时会发生此错误。例如,编译以下 C++ 代码:

#define configCPU_CLOCK_HZ ((unsigned long) 16000000)

#if configCPU_CLOCK_HZ > 0x1000
#endif

avr-g++ 会抱怨:

*.cpp:1:39: error: missing binary operator before token "long"
 #define configCPU_CLOCK_HZ ((unsigned long) 16000000)
                                       ^
*.cpp:3:5: note: in expansion of macro 'configCPU_CLOCK_HZ'
 #if configCPU_CLOCK_HZ > 0x1000
     ^

所以问题很可能是configCPU_CLOCK_HZ 用于上述的预处理指令中,但configCPU_CLOCK_HZ 不适合这种用法。

16000000 已经是 long 类型,因为它不适合 int,这是 avr-gcc 中的 16 位类型。所以在大多数情况下,您可以只使用普通的16000000

如果你想要一些可以在预处理器指令中使用的东西,你可以使用 16000000ul。此外,C99 的stdint.h 提供了产生位宽常量的宏,可以在预处理器宏中使用,例如

#include <stdint.h>
#define configCPU_CLOCK_HZ UINT32_C(16000000)

它将扩展为无符号的 32 位类型。但是请注意,旧版本的 AVR-LibC 对这些宏有一个错误:它只是使用了不符合语言标准的强制转换,该语言标准指出 [U]INTXX_C 必须在前驱宏中可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多