R. *_*sen 4 c makefile compilation
我试图让我的makefile编译一个需要-std=c99运行的文件.在这种情况下,它通过"for-loop"获得.
这是我的代码,(在"$(CC)"之前使用了"tab"):
CC = gcc
CFLAGS = -c -std=c99
...
Download.o : Download.c
$(CC) $(CFLAGS) Download.c
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如果我gcc -c -std=c99 Download.c在终端运行它工作正常.
在Linux中运行时会出现此问题.
解决了:
我创建了一个虚拟项目来展示我的讲师,试图解决我的问题.在虚拟项目中,所有代码都能正常工作.出于某种原因,我的代码在某个地方工作但在另一个地方却没有 如果有人读到这个问题与我有同样的问题,并希望看到一个示例项目.让我知道,我会在这里写代码.谢谢
Die*_*Epp 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)
Run Code Online (Sandbox Code Playgroud)
当你在它的时候,我建议一个更完整的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
Run Code Online (Sandbox Code Playgroud)
链接器标志实际上相当敏感!请务必完全按照我所写的方式键入上面的行,并且不要假设您所写的内容是等效的.以下是一些错误且不应使用的略有不同命令的示例:
# 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)
Run Code Online (Sandbox Code Playgroud)