【问题标题】:some defines compiled success in gcc, but g++ failed一些在 gcc 中定义编译成功,但 g++ 失败
【发布时间】:2017-11-24 08:27:20
【问题描述】:

我已经从项目 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:

语法有什么问题?

【问题讨论】:

  • 能否贴出编译成功的C代码,提供上下文?该代码本身也无法使用 gcc 编译
  • 好像不能发太多代码,所以可以在github给个url,就是
  • procps-v3.3.9/proc/readproc.c:114的源代码,请帮帮我

标签: compiler-errors g++


【解决方案1】:

使用 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__keyword 作为扩展。

    有关restrict的更多详细信息,请参阅http://en.cppreference.com/w/c/language/restrict

  • -fpermissive 添加到编译命令行。这放宽了一些编译器错误(例如无效的指针转换)并将它们变成警告。请务必查看生成的警告,以免忽略实际错误。

由于上面粘贴的代码是从该文件中获取的,我认为它也应该编译(尽管我自己没有看到你报告的错误)。

【讨论】:

  • 我有一些新的关键字,例如restrict、-fpermissive、attribute ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多