【问题标题】:C global variable compilation error with -D flag: expected identifier or '('带有 -D 标志的 C 全局变量编译错误:预期标识符或 '('
【发布时间】:2015-09-13 10:25:45
【问题描述】:

我有一个基本的 C 文件。它编译并运行良好,提示如下:

gcc program.c -D N=100 -O4 -Ofast

但是,当我为第二个变量 (z) 添加 -D 标志时:

gcc program.c -D N=100 -D z=2 -O4 -Ofast

我收到以下错误:

program.c:14:5: error: expected identifier or '('
int z = 2;
    ^
<command line>:2:11: note: expanded from here
#define z 2
          ^
1 error generated.

program.c代码如下:

#include "program.h"
#include <stdio.h>

double A[N][N];
double B[N][N];
int z;


int min(int x, int y){
    return (x < y) ? x : y;
}

void standard() {
    int i,j;
    for (i=0; i < N; i++)
        for (j=0; j < N; j++)
            A[i][j] += B[j][i];
}

void tiling() {
    int i,j,x,y;
    for (i=0; i < N; i+=z)
        for (j=0; j < N; j+=z)
            for (x=i; x < min((i + z),N); x++)
                 for (y=j; y < min((j + z),N); y++)
                     A[x][y] += B[y][x];
}

int main(int argc, const char * argv[]) {

    printf("The number is: %d. The next number is: %d", N, z);
    printf("The min is: %d.", min(13,20));
    B[40][50] = 21.0;
    tiling();
    //standard();
    printf("The matrix is: %f.",A[50][40]);
    return 0;
}

自从我用 C 编码以来已经有一段时间了,所以我生锈了,很难过。任何帮助将不胜感激!

【问题讨论】:

  • 好吧,宏在 C 中已经存在了一段时间,它们的语义并没有太大变化。所以你应该能够区分宏和变量。
  • 你是对的。谢谢。

标签: c gcc compiler-errors global-variables


【解决方案1】:

#defineing z 变为2 预处理器更改

int z;

int 2;

你可能不想要。

z 的所有其他匹配项相同。

你可能想要改变

int z;

成为

int z = Z;

并使用

-D Z=2

而不是

-D z=2

供您进一步阅读:https://gcc.gnu.org/onlinedocs/cpp/

【讨论】:

  • 感谢您快速而全面的回答!这解决了我的问题...我现在明白我误解了 -D 标志。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多