即使我使用它,也要使用std c99

时间:2016-03-05 20:04:28

标签: c gcc makefile

我试过写一个makefile,但告诉我:

$ make -f makefile
cc    -c -o source.o source.c
source.c: In function ‘main’:
source.c:5:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for(unsigned u = 0; u < 5; u++)
     ^
source.c:5:5: note: use option -std=c99 or -std=gnu99 to compile your code
make: *** [source.o] Errore 1

source.c 是我的计划:

#include <stdio.h>

int main(void)
{
    for (unsigned u = 0; u < 5; u++)
        printf("%u", u);

    int *p;

    if (p == NULL)
        return -1;

    return 0;
}

makefile 是我的makefile:

all: file

file: source.o
    gcc -std=c99 -g -Wall source.o -o file

source.o: source.c
    gcc -c -std=c99 -g -Wall source.c

为什么它告诉我使用c99(或gnu99),即使我把该参数放在makefile中?

2 个答案:

答案 0 :(得分:4)

啊哈。我想我知道你的问题是什么。

Makefile中的命令必须使用制表符而不是空格缩进。您可能需要调整文本编辑器的设置才能使用&#34; real&#34;这里有标签。

(使用空格将产生您已描述的结果,其中读取规则但忽略命令。)

答案 1 :(得分:-2)

make的输出显示C99模式未用于编译源代码。 请添加行

CFLAGS=-std=c99 -g -Wall

在目的地之前全部