编译和链接C程序时make ignores -std = c99标志

时间:2012-11-11 03:45:30

标签: c compilation makefile

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

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

CC = gcc
CFLAGS = -c -std=c99

...

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

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

错误消息

$ 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

Attemt to debug

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

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

解决:

我创建了一个虚拟项目来展示我的讲师,试图解决我的问题。在虚拟项目中,所有代码都能正常工作。出于某种原因,我的代码在某个地方工作但在另一个地方却没有如果有人读到这个问题与我有同样的问题,并希望看到一个示例项目。让我知道,我会在这里写代码。感谢

2 个答案:

答案 0 :(得分:11)

你看错了规则。 Download.c实际编译正常,但链接阶段是错误的。

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

修复链接程序的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)

答案 1 :(得分: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

在以gcc开头的行之前尝试标签


看到原始问题的更新后:

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

第一行(编译)显示没有错误 这是重新编译 Download.c的第二行,它失败了 我想你想链接.o文件来创建可执行文件Program,就像Dietrich Epp suggests一样。