c - make 在编译和链接 C 程序时忽略 -std=c99 标志

标签 c compilation makefile

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

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

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

尝试调试

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

在Linux下运行会出现这个问题。

已解决:

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

最佳答案

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

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

Fix the make rule that links the program. It should probably look something like this:

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)

关于c - make 在编译和链接 C 程序时忽略 -std=c99 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328394/

相关文章:

c - 使用静态模式规则会导致单个源更改重建所有

c - 链接期间的 GCcflags

linux - 海湾合作委员会错误 : 'for' loop initial declaration used outside C99 mode

c++ - 什么是 union ?

c - 有没有一种方法可以将 "nickname"赋予具有长名称/路径的变量,而无需在 C 中进行赋值

apache-flex - Intellij/Flex 4.91编译问题

visual-studio - 关于如何在 Visual Studio 中使用 sassy studio 进行编译的说明

makefile - 如何删除通配符Makefile后的多个源文件

c++ - 非常大的数组——C 数组与 C++ 数组。 Visual Studio - 超过最大值 (268435456)

c - 如何正确调整 Windows 窗体控件的大小?