为 2 个目标创建 makefile
海湾合作委员会4.4.1 C99
我正在创建一个客户端服务器应用程序。
我有一个编译所有文件的 Makefile。但是,我想创建 2 个目标(二进制文件),其中一个称为 clt 和 svr。并希望 Makefile 为它们创建 2 个单独的目录,分别称为 ./client 和 ./server?
到目前为止,我已经对我的 Makefile 进行了此操作。这只会创建 svr。我不确定如何添加要创建的客户端的第二个目标。
希望你明白我的意思吗?
非常感谢您的任何建议,
OBJECT_FILES = server.o client.o cltsvr_ults.o
CFLAGS = -ggdb -Wall -pthread
LIBS = -lpthread
CC = gcc
svr: $(OBJECT_FILES)
$(CC) $(CFLAGS) $(OBJECT_FILES) $(LIBS) -o svr
client.o: client.c cltsvr_ults.h
$(CC) -c client.c
server.o: server.c cltsvr_ults.h
$(CC) -c server.c
cltsvr_ults.o: cltsvr_ults.c
$(CC) -c cltsvr_ults.c
clean:
rm svr *.o *~
gcc 4.4.1
C99
I am creating a client server application.
I have a Makefile that compiles all the files. However, I would like 2 create 2 targets (binaries) one called clt and svr. And would like the Makefile to create 2 separate directories for them, called ./client and ./server?
So far I have done this to my Makefile. That only creates the svr. I am not sure how I can add the second target for the client to be created.
Hope you understand my point?
Many thanks for any suggestions,
OBJECT_FILES = server.o client.o cltsvr_ults.o
CFLAGS = -ggdb -Wall -pthread
LIBS = -lpthread
CC = gcc
svr: $(OBJECT_FILES)
$(CC) $(CFLAGS) $(OBJECT_FILES) $(LIBS) -o svr
client.o: client.c cltsvr_ults.h
$(CC) -c client.c
server.o: server.c cltsvr_ults.h
$(CC) -c server.c
cltsvr_ults.o: cltsvr_ults.c
$(CC) -c cltsvr_ults.c
clean:
rm svr *.o *~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还没有解释如何制作 clt,以及其中包含哪些对象。 svr 的对象列表看起来很奇怪——它真的应该有 client.o 吗?我假设 clt 由 client2.o 和 cltcvr_ults.o 组成。该 makefile 应该执行您想要的操作,包括创建目录:
You haven't explained how to make clt, what objects go into it. And the list of objects for svr looks odd-- should it really have client.o? I will assume that clt is made of client2.o and cltcvr_ults.o. This makefile should do what you want, including making the directories:
让您的第一个(默认)目标依赖于
clt
和svr
目标。您还需要让
clt
和svr
目标创建子目录。另一种可能更好的方法是将所有服务器代码移至其自己的目录中,同样将所有客户端代码移至其自己的目录中。 (这假设您可以将代码很好地分成两部分。)然后在顶级目录中有一个小的 Makefile,其目标执行每个子目录中的 Makefile。
附录
您可以通过为每个子目录编写如下编码规则,使顶层 Makefile 调用子目录中的 Makefile:
这会生成一个子进程来在
clt< 中执行另一个
make
/code> 目录,以及svr
子目录的另一个make
子进程。Make your first (default) target depend on the
clt
andsvr
targets.You also will want to have the
clt
andsvr
target create the subdirectories.Another, probably better, approach is to move all the server code into its own directory, and likewise all the client code into its own directory. (This assumes that you can split the code nicely into two portions.) Then have a small Makefile in the top directory whose targets execute the Makefiles in each of the subdirectories.
Addendum
You make the top Makefile invoke the Makefiles in the subdirectories by coding rules like these for each subdirectory:
This spawns a subprocess to perform another
make
in theclt
directory, and anothermake
subprocess for thesvr
subdirectory.您也可以将
clt
的依赖关系图放在那里,并添加第一个规则,如下所示:You can put your dependency graph for
clt
in there as well and add a first rule like this: