[英]Generate an executable for each .c file in a directory
My project directory looks like this:我的项目目录如下所示:
/project
Makefile
/src
main.c
foo.c
bar.c
/exec
main
foo
bar
I want that the Makefile genrates an executable file for every .c
file in /src
folder and put the executable in /exec
folder.我希望 Makefile 为/src
文件夹中的每个.c
文件生成一个可执行文件,并将可执行文件放在/exec
文件夹中。
It's my first time using Makefile, I tried the code below but it generates the exec files in the /src
folder and I want them to be in /exec
folder这是我第一次使用 Makefile,我尝试了下面的代码,但它在/src
文件夹中生成 exec 文件,我希望它们在/exec
文件夹中
CFLAGS := -Wall -g
SRC_DIR := src
EXEC_DIR := exec
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
all: $(SRC_FILES:.c=)
.c:
gcc $(CFLAGS) $< -o $(EXEC_DIR)/$@
You need to take the different paths into account.您需要考虑不同的路径。
One possible solution is this:一种可能的解决方案是:
CFLAGS := -Wall -g
SRC_DIR := src
EXEC_DIR := exec
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
all: $(SRC_FILES:$(SRC_DIR)/%.c=$(EXEC_DIR)/%)
$(EXEC_DIR)/%: $(SRC_DIR)/%.c
gcc $(CFLAGS) $< -o $@
Hints:提示:
You can use make -n
to check which commands make would execute, but without actually executing them.您可以使用make -n
检查 make将执行哪些命令,但不实际执行它们。
Also, its options -p
(print data base) and -d
(debug) are helpful to debug your Makefile.此外,它的选项-p
(打印数据库)和-d
(调试)有助于调试您的 Makefile。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.