gpt4 book ai didi

c - 如何编写单个 makefile,c 文件在 src 中,h 文件在 inc 文件夹中?

转载 作者:行者123 更新时间:2023-12-02 22:10:00 26 4
gpt4 key购买 nike

我正在尝试编写一个 makefile,它应该从 src/中选择源,从 inc/中选择 header

~/Linux/src: 1.c, 2.c, 3.c ...

~/Linuz/inc: abc.h, dyz.h

请帮我创建一个 makefile,它应该在

~/Linuz/some_other_dir/Makefile

PS:尝试为我的 linux 机器编译它。

感谢您的建议。

最佳答案

all: my_program

%.o: ../src/%.c
$(CC) $(CFLAGS) -I../inc/ -c -o $@ $^

my_program: 1.o 2.o 3.o
$(CC) $(LDFLAGS) -o $@ $^

clean:
rm -f *.o my_program

如果您将 Makefile 放在 ~/Linuz/some_other_dir/ 中,则以下规则

%.o: ../src/%.c

将从../src/文件夹(~/Linuz/src/)中获取c文件并在Makefile 的同一文件夹。

-I../inc/ 选项意味着 makefile 可以从 ../inc/ 文件夹(~/Linuz/inc/).

my_program: 1.o 2.o 3.o 规则意味着 makefile 将根据目标文件 1.o 在 Makefile 的同一目录中创建二进制文件> 和 2.o3.o

来自make manual :

$^ The names of all the prerequisites, with spaces between them. For prerequisites which are archive members, only the member named is used (see Archives). A target has only one prerequisite on each other file it depends on, no matter how many times each file is listed as a prerequisite. So if you list a prerequisite more than once for a target, the value of $^ contains just one copy of the name. This list does not contain any of the order-only prerequisites; for those see the `$|' variable, below.

$@ The file name of the target of the rule. If the target is an archive member, then ‘$@’ is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ‘$@’ is the name of whichever target caused the rule's recipe to be run.

关于c - 如何编写单个 makefile,c 文件在 src 中,h 文件在 inc 文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15475310/

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