【问题标题】:errors as i use the restrict qualifier我使用限制限定符时出现错误
【发布时间】:2012-10-20 17:46:49
【问题描述】:

当我编译以下程序时出现错误:

gcc tester.c -o tester

tester.c: In function ‘main’:
tester.c:7:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_X’
tester.c:7:17: error: ‘ptr_X’ undeclared (first use in this function)
tester.c:7:17: note: each undeclared identifier is reported only once for each function it appears in
tester.c:10:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_Y’
tester.c:10:17: error: ‘ptr_Y’ undeclared (first use in this function)

#include <stdio.h>

int main() {
  int x = 10;
  int y = 20;

  int *restrict ptr_X;
  ptr_X = &x;

  int *restrict ptr_Y;
  ptr_Y = &y;

  printf("%d\n",*ptr_X);

  printf("%d\n",*ptr_Y);
}

为什么会出现这些错误?

【问题讨论】:

    标签: c pointers restrict-qualifier


    【解决方案1】:

    Restrict 是 C99 的一部分,因此您必须通过为 gcc 指定 -std=c99 标志将其编译为 C99 程序。

    gcc -std=c99 tester.c -o tester
    

    【讨论】:

    • 为什么我需要将 c99 指定为编译器的标志? c99 是 1999 年推出的,现在是 2012 年。
    • @grassPro gcc 的默认设置是:编译为非标准垃圾。始终使用 -std=c99、-std=c89 或即将使用 -std=c11。
    【解决方案2】:

    并非所有编译器都符合 C99 标准。比如微软的编译器,根本不支持C99标准。如果您在 x86 平台上使用 MSVC,您将无法访问此关键优化选项。

    使用 GCC 时,请记住通过在编译标志中添加 -std=c99 来启用 C99 标准。在无法使用 C99 编译的代码中,使用 __restrict__restrict__ 将关键字启用为 GCC 扩展。

    来自here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 2018-11-02
      • 2015-05-02
      相关资源
      最近更新 更多