gpt4 book ai didi

c - 如何告诉 Make 忽略尚未更新的文件?

转载 作者:行者123 更新时间:2023-11-30 16:19:01 24 4
gpt4 key购买 nike

我是 Make 的新手,在弄清楚语法时遇到了一些麻烦。我浏览了一些示例,基本上将其中的一些示例组合起来创建了我自己的文件。我不知道如何告诉 make 忽略已编译的源文件或未更改的头文件。如何让 make 只编译已更改的文件?

我查看了GNU网站:https://www.gnu.org/software/make/manual/html_node/Avoiding-Compilation.html

我尝试了其中一些标志,但仍然没有得到我想要的结果。

# specify compiler
CC=gcc
# set compiler flags
CFLAGS=-Igen/display -Igen/logic -Iman -Ilib/include -pipe -march=native
# set linker flags
LDFLAGS=-lglut32 -loglx -lopengl32 -Llib
# include all sources
SOURCES=gen/display/*.c gen/logic/*.c man/*.c
# create objects from the source files
OBJECTS=$(SOURCES:.cpp=.o)
# specify the name and the output directory of executable
EXECUTABLE=win32/demo

all: $(SOURCES) $(EXECUTABLE)

# compile the target file from sources
# $@ = placeholder for target name
$(EXECUTABLE): $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(LDFLAGS) -o $@

.c.o:
$(CC) $(CFLAGS) $< -o $@

我在不同的目录中有许多正在编译的头文件和源文件,但无论我做什么,所有内容都会重新编译。

最佳答案

好吧,让我们开始吧,因为这是应该做的;-)

# set up our variables
# note: you may also prefer := or ?= assignments,
# however it's not that important here
CC=gcc
CFLAGS=-Igen/display -Igen/logic -Iman -Ilib/include -pipe -march=native
# linker's flags are different from compiler's
LDFLAGS=
# these are really libs, not flags
LDLIBS=-Llib -lglut32 -loglx -lopengl32

# 1) make is not your shell - it does not expand wildcards by default
# 2) use := to force immediate wildcard expansion;
# otherwise make could perform it several times,
# which is, at the least, very ineffective
SOURCES:=$(wildcard gen/display/*.c gen/logic/*.c man/*.c)

# use the right extensions here: .c -> .o
OBJECTS=$(SOURCES:.c=.o)

# note: it's okay to omit .exe extension if you're using "POSIX"-like make
# (e.g. cygwin/make or msys/make). However, if your make was built on Windows
# natively (such as mingw32-make), you'd better to add '.exe' here
EXECUTABLE=win32/demo

# don't forget to say to make that 'all' is not a real file
.PHONY: all
# *.c files are what you write, not make
# note: in fact, you don't need 'all' target at all;
# this is rather a common convention
all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

# note: 1) suffix rules are deprecated; use pattern rules instead
# 2) this doesn't add much to the built-in rule, so you can even omit it
# 3) .o files are created in the same directories where .c files reside;
# most of the time this is not the best solution, although it's not
# a mistake per se
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

关于c - 如何告诉 Make 忽略尚未更新的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55737068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com