我从项目props-3.3.9
复制了一些代码:readproc.c
文件
然后使用 gcc 编译成功,然后执行访问。
但是现在我想用c++进行编码,但是g++会编译错误!
g++报告错误:
expression of type 'void' is illegal
#define F(x) {#x, sizeof(#x)-1, (long)(&&case_##x-&&base)},
^
readproc.c:122:9:in expansion of macro ‘F’
F(VmHWM)
^
这是代码:
#define LABEL_OFFSET
typedef struct status_table_struct {
unsigned char name[7]; // /proc/*/status field name
unsigned char len; // name length
#ifdef LABEL_OFFSET
long offset; // jump address offset
#else
void *addr;
#endif
} status_table_struct;
#ifdef LABEL_OFFSET
#define F(x) {#x, sizeof(#x)-1, (long)(&&case_##x-&&base)},
#else
#define F(x) {#x, sizeof(#x)-1, &&case_##x},
#endif
#define NUL {"", 0, 0},
static const status_table_struct table[] = {
F(VmHWM)
NUL NUL
F(VmLck)
NUL
F(VmSwap)
F(VmRSS)
NUL
F(VmStk)
NUL
F(Tgid)
F(State)
};
#undef F
#undef NUL
base:
case_Name:
case_ShdPnd:
case_SigBlk:
case_SigCgt:
case_SigIgn:
case_SigPnd:
case_State:
grammer怎么了?
最佳答案
使用g++ 5.4.0,我可以通过以下方式编译readproc.c
(https://gitlab.com/procps-ng/procps/blob/v3.3.9/proc/readproc.c):
restrict
__替换__restrict
(所有39次出现)。虽然
restrict
是C中有效的关键字(随C99引入),但C++标准未定义它。 GCC和clang确实支持__restrict__
关键字作为扩展。有关
restrict
的更多详细信息,请参见http://en.cppreference.com/w/c/language/restrict。 -fpermissive
添加到编译命令行。这缓解了一些编译器错误(例如,无效的指针强制转换),并将其转变为警告。确保检查生成的警告,以免忽略实际错误。 由于上面粘贴的代码是从该文件中提取的,因此我认为它也应该编译(尽管我自己没有看到您报告的错误)。
关于compiler-errors - 有些人在gcc中定义了编译成功,但是g++失败了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47469162/