将变量的名称更改为makefile之后,其行为发生了巨大变化。从不成功的编译到成功的编译。
我尝试了一切-不同的名称,添加了各种附加条件,但都没有成功。
制造版本:
$make --version
GNU Make 3.82
Built for x86_64-redhat-linux-gnu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
这是源代码文件
int main(int argc, char *argv[])
{
return 0;
}
原始(所需)行为makefile:
SHELL = /bin/sh
.SUFFIXES:
.SUFFIXES: .c .o
CC := gcc
LD := gcc
CFLAGS := -g
STRICT_CFLAGS := -Wall -Wextra -Wpedantic -Werror
INCLUDE_CFLAGS :=
ALL_CFLAGS := $(INCLUDE_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS)
STRICT_LDFLAGS := -Wall -Wextra -Wpedantic -Werror
INCLUDE_LDFLAGS :=
LDFLAGS := $(STRICT_LDFLAGS) $(INCLUDE_LDFLAGS) $(CFLAGS)
SOURCES := $(wildcard *.c)
BINARIES := $(SOURCES:%.c=%)
.PHONY: all
all: $(BINARIES)
%: %.o
@echo "Checking..."
$(LD) $(LDFLAGS) -o $@
.c.o:
@echo "Creating object..."
$(CC) $(ALL_CFLAGS) -c $< -o $@
.PHONY: clean
clean:
@echo "Cleaning up..."
-rm -rvf *.o $(BINARIES)
编译输出:
$make
gcc -g -Wall -Wextra -Wpedantic -Werror -g test.c -o test
test.c: In function ‘main’:
test.c:1:14: error: unused parameter ‘argc’ [-Werror=unused-parameter]
} return 0;nt argc, char *argv[])
^
test.c:1:26: error: unused parameter ‘argv’ [-Werror=unused-parameter]
} return 0;nt argc, char *argv[])
^
cc1: all warnings being treated as errors
make: *** [test] Error 1
重命名变量(LDFLAGS-> T_LDFLAGS)后的Makefile-错误行为:
SHELL = /bin/sh
.SUFFIXES:
.SUFFIXES: .c .o
CC := gcc
LD := gcc
CFLAGS := -g
STRICT_CFLAGS := -Wall -Wextra -Wpedantic -Werror
INCLUDE_CFLAGS :=
ALL_CFLAGS := $(INCLUDE_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS)
STRICT_LDFLAGS := -Wall -Wextra -Wpedantic -Werror
INCLUDE_LDFLAGS :=
T_LDFLAGS := $(STRICT_LDFLAGS) $(INCLUDE_LDFLAGS) $(CFLAGS)
SOURCES := $(wildcard *.c)
BINARIES := $(SOURCES:%.c=%)
.PHONY: all
all: $(BINARIES)
%: %.o
@echo "Checking..."
$(LD) $(T_LDFLAGS) -o $@
.c.o:
@echo "Creating object..."
$(CC) $(ALL_CFLAGS) -c $< -o $@
.PHONY: clean
clean:
@echo "Cleaning up..."
-rm -rvf *.o $(BINARIES)
编译输出:
$make
gcc -g test.c -o test
请有人向我解释为什么在更改名称后,我们会丢失严格代码的标志。
感谢您的时间!
答案 0 :(得分:2)
都没有命中目标。如果查看输出,则会注意到未打印Checking...
,也未打印Creating object...
。因此,您的程序是使用默认目标构建的。此默认目标自动将$(CFLAGS)
和$(LDFLAGS)
添加到构建行。因此,当您重命名LDFLAGS
时,事情就停止了。
此外,您的makefile当前正在尝试为每个.c文件创建可执行文件。
您需要按以下步骤更改目标:
SOURCES := $(wildcard *.c)
OBJECTS := $(SOURCES:%.c=%.o)
BINARY := myprog
.PHONY: all
all: $(BINARY)
$(BINARY): $(OBJECTS)
@echo "Checking..."
$(LD) $(T_LDFLAGS) $(OBJECTS) -o $@
%.o: %.c
@echo "Creating object..."
$(CC) $(ALL_CFLAGS) -c $< -o $@
OBJECTS
变量将包含与每个.c文件相对应的.o文件列表。这将是二进制文件的依赖关系列表以及要链接的对象列表。目标文件的目标说要为每个.c文件创建一个.o文件。