4

我试图让我的 makefile 编译一个需要-std=c99运行的文件。在这种情况下,它通过一个“for-loop”。

这是我的代码,(在 "$(CC)" 之前使用了 "tab" ):

CC = gcc
CFLAGS = -c -std=c99

...

Download.o : Download.c
    $(CC) $(CFLAGS) Download.c

Download.c 包含用于从 Web 下载元素的方法

错误信息

$ make
gcc -c -std=c99 Download.c
gcc Download.c -o Program
Download.c: In function ‘downloadImageparts’:
Download.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
Download.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code
Download.c:13:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
make: *** [comp] Error 1

尝试调试

如果我gcc -c -std=c99 Download.c在终端运行它工作正常。

在 Linux 中运行时会出现此问题。

解决了:

我创建了一个虚拟项目来展示我的讲师,试图解决我的问题。在虚拟项目中,所描述的代码一切正常。出于某种原因,我的代码可以在原地工作,但不能在其他地方工作。如果有人读到这个和我有同样的问题并且想看一个示例项目。让我知道,我会在这里写代码。谢谢

4

2 回答 2

11

你在看错误的规则。 Download.c实际上编译很好,但是链接阶段是错误的。

$ 制作
gcc -c -std=c99 Download.c   #编译
gcc Download.c -o Program    #链接

修复链接程序的 make 规则。它应该看起来像这样:

Program: a.o b.o c.o
    $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

当你使用它时,我建议一个更完整的 Makefile 看起来像这样:

all: Program
clean:
    rm -f Program *.o
.PHONY: all clean

# -c is implicit, you don't need it (it *shouldn't* be there)
# CC is also implicit, you don't need it
CFLAGS := -std=c99 -g -Wall -Wextra

Program: a.o b.o c.o
    $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

# Make will automatically generate the necessary commands
# You just need to name which headers each file depends on
# (You can make the dependencies automatic but this is simpler)
a.o: a.c header.h
b.o: b.c header.h header2.h
c.o: c.c header.h

如何做错的例子

链接器标志实际上相当敏感!一定要按照我写的完全正确地输入上面的行,不要假设你写的内容是等价的。以下是一些错误且不应使用的命令略有不同的示例:

# WRONG: program must depend on *.o files, NOT *.c files
Program: a.c b.c c.c
    $(CC) ...

# WRONG: -c should not be in CFLAGS
CFLAGS := -c -std=c99

Program: a.o b.o c.o
    # WRONG: $(CFLAGS) should not be here
    # you are NOT compiling, so they do not belong here
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

    # WRONG: $(LIBS) MUST come at the end
    # otherwise linker may fail to find symbols
    $(CC) $(LDFLAGS) -o $@ $(LIBS) $^

    # WRONG: do not list *.o files, use $^ instead
    # otherwise it is easy to get typos here
    $(CC) $(LDFLAGS) -o $@ a.o b.o c.o $(LIBS)

    # WRONG: $(LDFLAGS) must be at the beginning
    # it only applies to what comes after, so you
    # MUST put it at the beginning
    $(CC) -o $@ $(LDFLAGS) $^ $(LIBS)

    # WRONG: -c flag disables linking
    # but we are trying to link!
    $(CC) $(LDFLAGS) -c -o $@ $^ $(LIBS)

    # WRONG: use $(CC), not gcc
    # Don't sabotage your ability to "make CC=clang" or "make CC=gcc-4.7"
    gcc $(LDFLAGS) -o $@ $^ $(LIBS)

    # WRONG: ld does not include libc by default!
    ld $(LDFLAGS) -o $@ $^ $(LIBS)
于 2012-11-11T04:15:15.480 回答
0

如果我在 makefile 中使用空格而不是制表符,我会看到相同的结果,并且输出make显示未使用该规则:

$ make
cc    -c -o Download.o Download.c
Download.c: In function ‘main’:
Download.c:4:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
Download.c:4:3: note: use option -std=c99 or -std=gnu99 to compile your code
make: *** [Download.o] Error 1

tab在以 .开头的行之前尝试 a gcc


看到原始问题的更新后:

$ make
gcc -c -std=c99 Download.c
gcc Download.c -o Program

第一(编译)行没有显示错误。
这是第二行,它重新编译 Download.c,失败了。正如 Dietrich Epp建议
的那样, 我认为您想在此处链接.o文件以创建可执行文件。Program

于 2012-11-11T03:52:38.983 回答