错误:无效使用无效

Error: Invalid use of void

本文关键字:无效 错误      更新时间:2023-10-16

我正在使用C标头(hps_linux.h(编译C++源代码(main.cpp(。hps_linux.h 中的代码是:

#ifndef HPS_LINUX_H_
#define HPS_LINUX_H_
#include <stdbool.h>
#include <stdint.h>
#include "socal/hps.h"
int fd_dev_mem = 0;
void   *h2f_lw_axi_master     = NULL;
size_t h2f_lw_axi_master_span = ALT_LWFPGASLVS_UB_ADDR -ALT_LWFPGASLVS_LB_ADDR + 1;
size_t h2f_lw_axi_master_ofst = ALT_LWFPGASLVS_OFST;
#endif

hps_linux.h 包括 hps.h,它具有下一个定义:

#define ALT_LWFPGASLVS_OFST        0xff200000
#define ALT_LWFPGASLVS_ADDR        ALT_CAST(void *, (ALT_CAST(char *, ALT_HPS_ADDR) + ALT_LWFPGASLVS_OFST))
#define ALT_LWFPGASLVS_LB_ADDR     ALT_LWFPGASLVS_ADDR
#define ALT_LWFPGASLVS_UB_ADDR     ALT_CAST(void *, ((ALT_CAST(char *, ALT_LWFPGASLVS_ADDR) + 0x200000) - 1))

我的主要.cpp包括hps_linux.h。我像这样编译:

gcc -Wall -std=gnu99 hps_linux.c -o hps_linux.o
g++ -Wall -std=c++0x main.cpp -o main 

它抛出下一个错误:

hps_linux.h: error: invalid use of 'void'

行内:

size_t h2f_lw_axi_master_span = ALT_LWFPGASLVS_UB_ADDR -ALT_LWFPGASLVS_LB_ADDR + 1;

当我用 C 编写的主文件(main.c(编译它时,它可以工作。

查看此处的源代码,我们有:

#ifdef __ASSEMBLY__
#    define ALT_CAST(type, ptr)  ptr // <-- not (ptr)???
#else
#    define ALT_CAST(type, ptr)  ((type) (ptr))
#endif  /* __ASSEMBLY__ */

现在,我们有:

size_t h2f_lw_axi_master_span = ALT_LWFPGASLVS_UB_ADDR - ALT_LWFPGASLVS_LB_ADDR + 1;

部分扩展宏,我们有:

size_t h2f_lw_axi_master_span = ALT_CAST(void *, ALT_CAST(char *, ...)) - ALT_CAST(void *, ALT_CAST(char *, ...)) + 1;

现在,如果设置了__ASSEMBLY__,则此行为:

size_t h2f_lw_axi_master_span = ... - ... + 1;

两个...表达式都是整数,所以这没关系。但是,如果未设置__ASSEMBLY__,则此行为:

size_t h2f_lw_axi_master_span = (void *) (...) - (void *) (...) + 1;

没有定义减去两个 void 指针,因此编译器放弃了。

因此,您需要确保定义了__ASSEMBLY__;一种方法是:

g++ -Wall -D__ASSEMBLY__ -std=c++0x main.cpp -o main

但是,这可能会导致问题,因为您应该依靠早期的头文件来正确设置它。

更新:快速搜索 git 存档不会显示正在设置__ASSEMBLY__,并且给定名称表明它应该是内置编译器......