6

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

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);
}

为什么我会收到这些错误?

4

2 回答 2

5

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

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

这里

于 2012-10-31T09:47:34.303 回答
1

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

gcc -std=c99 tester.c -o tester
于 2012-10-31T09:46:52.823 回答