错误:无效使用void

时间:2017-05-11 00:37:16

标签: c++ c void void-pointers

我用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))

我的main.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)编写的主程序编译它时,它可以工作。

1 个答案:

答案 0 :(得分:3)

查看source code here,我们有:

#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__,并且根据它的名称,它应该是内置的编译器......