为 2 个目标创建 makefile

发布于 2024-08-10 11:56:57 字数 681 浏览 4 评论 0原文

海湾合作委员会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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昵称有卵用 2024-08-17 11:56:57

您还没有解释如何制作 clt,以及其中包含哪些对象。 svr 的对象列表看起来很奇怪——它真的应该有 client.o 吗?我假设 clt 由 client2.o 和 cltcvr_ults.o 组成。该 makefile 应该执行您想要的操作,包括创建目录:

SVR_OBJECT_FILES = server.o client.o cltsvr_ults.o
CLT_OBJECT_FILES = client2.o cltsvr_ults.o  

CFLAGS = -ggdb -Wall -pthread
LIBS = -lpthread 
CC = gcc  

all: client/clt server/svr
client/clt: $(CLT_OBJECT_FILES) 
server/svr: $(SVR_OBJECT_FILES)  

client/clt server/svr:
    @mkdir -p $(dir $@)
    $(CC) $(CFLAGS) $^ $(LIBS) -o $@  

client.o client2.o server.o: cltsvr_ults.h  

%.o: %.c
    $(CC) -c 
lt;  

clean: 
    rm -f client/clt server/svr *.o *~ 

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:

SVR_OBJECT_FILES = server.o client.o cltsvr_ults.o
CLT_OBJECT_FILES = client2.o cltsvr_ults.o  

CFLAGS = -ggdb -Wall -pthread
LIBS = -lpthread 
CC = gcc  

all: client/clt server/svr
client/clt: $(CLT_OBJECT_FILES) 
server/svr: $(SVR_OBJECT_FILES)  

client/clt server/svr:
    @mkdir -p $(dir $@)
    $(CC) $(CFLAGS) $^ $(LIBS) -o $@  

client.o client2.o server.o: cltsvr_ults.h  

%.o: %.c
    $(CC) -c 
lt  

clean: 
    rm -f client/clt server/svr *.o *~ 
溺深海 2024-08-17 11:56:57

让您的第一个(默认)目标依赖于 cltsvr 目标。

all:  svr clt

svr: ...
clt: ...

您还需要让 cltsvr 目标创建子目录。

另一种可能更好的方法是将所有服务器代码移至其自己的目录中,同样将所有客户端代码移至其自己的目录中。 (这假设您可以将代码很好地分成两部分。)然后在顶级目录中有一个小的 Makefile,其目标执行每个子目录中的 Makefile。

附录

您可以通过为每个子目录编写如下编码规则,使顶层 Makefile 调用子目录中的 Makefile:

clt:  cd clt; make $(MAKEFLAGS)
svr:  cd svr; make $(MAKEFLAGS)

这会生成一个子进程来在 clt< 中执行另一个 make /code> 目录,以及 svr 子目录的另一个 make 子进程。

Make your first (default) target depend on the cltandsvr targets.

all:  svr clt

svr: ...
clt: ...

You also will want to have the cltandsvr 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:

clt:  cd clt; make $(MAKEFLAGS)
svr:  cd svr; make $(MAKEFLAGS)

This spawns a subprocess to perform another make in the clt directory, and another make subprocess for the svr subdirectory.

浮世清欢 2024-08-17 11:56:57

您也可以将 clt 的依赖关系图放在那里,并添加第一个规则,如下所示:

all: svr clt

You can put your dependency graph for clt in there as well and add a first rule like this:

all: svr clt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文