c - 如何为具有多个文件夹的项目编写单个 Makefile?

标签 c makefile compilation

我有以下项目结构(我只是运行$ tree src):

src
├── client
│   ├── client.c
│   ├── client_functions.c
│   ├── client_functions.h
├── common
│   ├── conf.c
│   ├── conf.h
│   ├── file_list.c
│   └── file_list.h
├── Makefile
└── server
    ├── server.c
    ├── server_functions.c
    └── server_functions.h

我希望在根 src 文件夹中有一个 Makefile(因此为整个项目运行单个 $ make 命令)。

以下是依赖关系(请注意,每个*.c 都依赖于相对的*.h):

  • client.c 依赖于 client_functions.h
  • client_functions.c 依赖于 conf.h
  • server.c 依赖于 server_functions.c
  • server_functions.c 依赖于 conf.hfile_list.h

我正在尝试编写一个Makefile,但我不断收到错误。我从未为如此复杂的项目结构创建过一个项目(通常所有内容都在一个文件夹中)。这是 Makefile 的第一部分:

CC := /usr/bin/gcc
RM := /usr/bin/rm -f
CFLAGS := -Wall -Wextra -Wpedantic -O3

我应该如何编写 Makefile 的其余部分?

请注意,我只需要 2 个可执行文件:client 文件夹内的 clientserver 文件夹内的 server .

编辑:考虑到@melpomene的回答,我编写了以下Makefile

CC := gcc
CFLAGS := -Wall -Wextra -Wpedantic -O3
SRC := client/client.c client/client_functions.c client/client_functions.h server/server.c server/server_functions.c server/server_functions.h common/conf.c common/conf.h common/file_list.c common/file_list.h
OBJ := $(SRC:.c=.o)

.PHONY: all

all: client/client server/server

client/client: client/client.o client/client_functions.o common/conf.o

client/client.o: client/client.c client/client_functions.h
client/client_functions.o: client/client_functions.c client/client_functions.h common/conf.h

server/server: server/server.o server/server_functions.o common/conf.o common/file_list.o

server/server.o: server/server.c server/server_functions.h
server/server_functions.o: server/server_functions.c server/server_functions.h common/conf.h common/file_list.h

common/conf.o: common/conf.c common/conf.h
common/file_list.o: common/file_list.c common/file_list.h

clean:
        rm -f client/*.o server/*.o common/*.o core

cleanall:
        rm -f client/*.o server/*.o common/*.o core client/client server/server

现在会产生以下输出:

gcc -Wall -Wextra -Wpedantic -O3   -c -o client/client.o client/client.c
gcc -Wall -Wextra -Wpedantic -O3   -c -o client/client_functions.o client/client_functions.c
gcc -Wall -Wextra -Wpedantic -O3   -c -o common/conf.o common/conf.c
gcc   client/client.o client/client_functions.o common/conf.o   -o client/client
common/conf.o:(.rodata+0x0): multiple definition of `at'
client/client_functions.o:(.rodata+0x0): first defined here
common/conf.o:(.rodata+0x4): multiple definition of `T'
client/client_functions.o:(.rodata+0x4): first defined here
common/conf.o:(.rodata+0x8): multiple definition of `N'
client/client_functions.o:(.rodata+0x8): first defined here
common/conf.o:(.rodata+0x10): multiple definition of `p'
client/client_functions.o:(.rodata+0x10): first defined here
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'client/client' failed
make: *** [client/client] Error 1

atTNp 都是 conf.h< 中定义的变量

最佳答案

听起来这就是您所需要的:

.PHONY: all

all: client/client server/server

client/client: client/client.o client/client_functions.o common/conf.o

client/client.o: client/client.c client/client_functions.h
client/client_functions.o: client/client_functions.c client/client_functions.h common/conf.h

server/server: server/server.o server/server_functions.o common/conf.o common/file_list.o

server/server.o: server/server.c server/server_functions.h
server/server_functions.o: server/server_functions.c server/server_functions.h common/conf.h common/file_list.h

common/conf.o: common/conf.c common/conf.h
common/file_list.o: common/file_list.c common/file_list.h

我列出了您所描述的所有依赖项(通过一些有根据的猜测进行了修改)。内置的隐式规则应该完成其余的工作。

关于c - 如何为具有多个文件夹的项目编写单个 Makefile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137551/

相关文章:

c - 为什么这种排序不起作用? (C)

c - 错误指针 : is it okay to reserve random data to be used as error pointer?

c - 我的程序在 C 中反转字符串有什么问题?

c++ - 如何使用 mingw 构建 libpng

java - IntelliJ 中的同一个文件夹中是否可以有多个 src 文件夹?

angular - Ng serve 在 Windows 中工作,但在 Ubuntu 中失败

iphone - 是否有用于随机性的 Mersenne Twister 算法的稳定 Objective-C 实现?

C 内存泄漏 fsanitize=address

c - Makefile 编译多个文件

java - 将新类添加到现有 JAR 文件(包含源代码)