简体   繁体   English

Makefile中的多个可执行文件

[英]Multiple executables from makefile

I am trying to write a makefile that will compile a simple simulated Real time operating system written in c. 我正在尝试编写一个makefile,它将编译用c编写的简单的模拟实时操作系统。

The directory structure looks something like this 目录结构看起来像这样

  **KB** SOURCE ------- - Keyboard.c (helper process) Incl -------- - Include files needed by keyboard.c **CRT** SOURCE ------- - crt.c (helper process) Incl -------- - Include files needed by crt.c **RTX** SOURCE ------- - SOURCE Files INCLUDE -------- - Include Files 

The RTX requires that the helper processes be compiled into two executable, it then uses these executable to run. RTX要求将帮助程序进程编译为两个可执行文件,然后使用这些可执行文件运行。

I have no idea on how to generate three different executable in the same Makefile, or even on how to include the files in the different include files for even one executable. 我不知道如何在同一个Makefile中生成三个不同的可执行文件,甚至不知道如何针对一个可执行文件将文件包含在不同的包含文件中。 I've never created a makefile before and am having problems following tutorials online. 我以前从未创建过makefile,并且在在线学习教程后遇到问题。

Here is what I have so far for the RTX (how do I add in different paths?) 这是到目前为止我对RTX的了解(如何添加其他路径?)

 #For RTX Exexutable OBJS = api.o init_table.o k_rtx.o msg.o pcb.o queue.o user_proc.o main.o iRTX-G29 : $(OBJS) gcc -o iRTX-G29 $(OBJS) api.o : api.h gcc -c api.c init_table.o : init_table.h gcc -c init_table.c k_rtx.o : unistd.h string.h k_rtx.h gcc -c k_rtx.c main.o : fcntl.h sys/mman.h sys/wait.h stdio.h stdio.h sys/types.h sys/ipc.h sys/shm.h errno.h string.h stdlib.h signal.h k_rtx.h buffer.h global.h gcc -c main.c msg.o : msg.h gcc -c msg.c queue.o : queue.h gcc -c queue.c user_proc.o : user_proc.h gcc -c user_proc.c 

Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

First let's simplify your existing makefile. 首先,让我们简化您现有的makefile。 You have many object rules that have the same form: 您有许多具有相同形式的对象规则:

msg.o : msg.h
    gcc -c msg.c

A cleaner way to write it is like this: 一种更简洁的编写方式是这样的:

msg.o : msg.c msg.h
    gcc -c $< -o $@

(I've added the dependency msg.c , put in some automatic variables and specified the name of the output.) Now that all of these object rules look alike, we can make a pattern rule for this, but I'll separate the header dependency for now because main.o doesn't depend on main.h : (我已经添加了依赖项msg.c ,放入了一些自动变量,并指定了输出的名称。)现在,所有这些对象规则都相似,我们可以为此创建一个模式规则 ,但是我将把头文件依赖,因为main.o不依赖于main.h

api.o : api.h
init_table.o : init_table.h
k_rtx.o : unistd.h string.h k_rtx.h
main.o : fcntl.h sys/mman.h sys/wait.h stdio.h stdio.h sys/types.h sys/ipc.h sys/shm.h errno.h string.h stdlib.h signal.h k_rtx.h buffer.h global.h
msg.o : msg.h
queue.o : queue.h
user_proc.o : user_proc.h

%.o : %.c
    gcc -c $< -o $@

Most of these objects have a header dependency, so we can separate those out and make it a little shorter: 这些对象大多数都具有标头依赖性,因此我们可以将它们分离开并使其更短一些:

api.o init_table.o k_rtx.o msg.o queue.o user_proc.o : %.o : %.h

k_rtx.o : unistd.h string.h
main.o : fcntl.h sys/mman.h sys/wait.h stdio.h stdio.h sys/types.h sys/ipc.h sys/shm.h errno.h string.h stdlib.h signal.h k_rtx.h buffer.h global.h

%.o : %.c
    gcc -c $< -o $@

This will work within RTX/, but to get it to work when executed in the parent directory, we have to change a couple of things. 这将在RTX /中运行,但是要使其在父目录中执行时能够正常工作,我们必须进行一些更改。 I'll assume that there is a danger of filename collision (ie there is a main.c in RTX/, and a different main.c in CRT/, so we mustn't get the main.o s mixed up). 我假设存在文件名冲突的危险(即RTX /中有一个main.c ,而CRT /中有一个不同的 main.c ,因此我们不要main.o )。 So let's keep the RTX object files in RTX/ (and I'll take main.o out of the object list for convenience, and omit the sys/*** dependencies because I don't know where you keep sys/ ). 因此,让我们将RTX对象文件保留在RTX /中(为了方便起见,我将main.o从对象列表中main.o ,并省略sys/***依赖项,因为我不知道您将sys/保留在哪里)。 And this answer is getting long, so I'll fold in the other executables and a master target to build them all: 而且答案越来越长,因此我将折叠其他可执行文件和一个主要目标来构建它们:

# This is the first target, so it will be the default: run "make", and you'll get this.
.PHONY: all
all: iRTX-G29 iCRT-G29 iKBD-G29
    @echo all done

#For RTX Executable
RTX_OBJS = $(addprefix RTX/, api.o init_table.o k_rtx.o msg.o pcb.o queue.o user_proc.o)
iRTX-G29 : $(RTX_OBJS) RTX/main.o

# This is a little clumsy, but it can't (easily) be helped.
RTX/k_rtx.o : RTX/unistd.h RTX/string.h
RTX/main.o : $(addprefix RTX/, fcntl.h stdio.h stdio.h errno.h string.h stdlib.h signal.h k_rtx.h buffer.h global.h)

#Now we can throw in the CRT and Keyboard executables:

CRT_OBJS = $(addprefix CRT/, this.o that.o theOther.o)
iCRT-G29 : $(CRT_OBJS) CRT/main.o

CRT/this.o: CRT/whatever.h

KBRD_OBJS = $(addprefix SOURCE/, red.o green.o blue.o)    
iKBD-G29 : $(KBRD_OBJS) SOURCE/main.o

SOURCE/green.o: SOURCE/chlorophyll.h

# Now for the stuff common to all three

iRTX-G29 iCRT-G29 iKBD-G29 :
    gcc -o $@ $^

$(RTX_OBJS) $(CRT_OBJS) $(KBRD_OBJS): %.o : %.h

%.o : %.c
    gcc -c $< -o $@

In makefile you can write target that not correspond to real file: 在makefile中,您可以编写与实际文件不对应的目标:

all: executable1 executable2

executable1: a.o b.o
<TAB HERE>gcc -o $@ $^

executable2: c.o d.o
<TAB HERE>gcc -o $@ $^

I have no idea on how to generate three different executable in the same Makefile, or even on how to include the files in the different include files for even one executable. 我不知道如何在同一个Makefile中生成三个不同的可执行文件,甚至不知道如何针对一个可执行文件将文件包含在不同的包含文件中。

This is the perfect time to start using automake. 这是开始使用自动制作的最佳时机。 Sarah George has written a good tutorial. 莎拉·乔治(Sarah George )写了一篇不错的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM