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

时间:2018-01-07 13:25:50

标签: 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

我想在根Makefile文件夹中有一个src(因此为整个项目运行一个$ 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中定义的变量

1 个答案:

答案 0 :(得分:1)

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

.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

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

相关问题