究竟如何我用一个makefile? [英] How exactly do I use a makefile?

查看:132
本文介绍了究竟如何我用一个makefile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那一刻真的很困惑。

I'm really confused at the moment.

所以我有5个文件:main.c中,flight.c,flight.h,passenger.c和passenger.h

So I have 5 files: main.c, flight.c, flight.h, passenger.c, and passenger.h

flight.h有flight.c函数原型,并passenger.h有passenger.c函数原型

flight.h has function prototypes for flight.c, and passenger.h has function prototypes for passenger.c

flight.c和passenger.c对这些函数的定义。

flight.c and passenger.c have definitions for these functions.

main.c中是节目我会和互动,呼吁从两个.c文件的功能。

main.c is the program I'll interact with, calling functions from both .c files

我真的不知道什么.o文件是,有人请小心解释?这是我的Makefile:

I'm not really sure what .o files are for, somebody please care to explain? Here is my Makefile:

flight.o: flight.c flight.h
    gcc -Wall -g -c flight.c    
passenger.o: passenger.c passenger.o
    gcc -Wall -g -c passenger.c    
main.o: main.c
    gcc -Wall -g -c main.c     
reservations.out: main.o flight.o passenger.o
    gcc -Wall -g flight.o passenger.o main.o -o reservations.out

编辑:然后我用命令使Makefile文件,并得到一个错误:让:对`的Makefile'必须做什么

I then use the command "make Makefile", and get an error: make: Nothing to be done for `Makefile'.

在我的文件中,有GCC前的标签。
我倒是AP preciate任何帮助,我敢肯定有很多事情我做错了。谢谢你。

In my file there is a tab before gcc. I'd appreciate any help, I'm sure there are lots of things I'm doing wrong. Thanks.

推荐答案

您的问题是,你使用了错误的命令。我想下面的行添加到您的Makefile顶部:

Your problem is that you're using the wrong command. I would add the following line to the top of your makefile:

all: reservations.out

然后只需键入制作在命令行。如果你不想编辑文件,只需要使用使reservations.out ,你应该得到正确的行为。

Then just type make at the command line. If you don't want to edit the file, just use make reservations.out and you should get the right behaviour.

至于什么是的.o 文件是:这是一个对象文件,这意味着它包含编译(但在这种情况下,不链接)code。您最终的makefile规则取目标文件,然后将这些链接到一个名为 reservations.out

As to what a .o file is: it's an 'object' file, meaning it contains compiled (but in this case not linked) code. Your final makefile rule takes the object files and links them together into a final executable called reservations.out.

现在,我看它多一些,它看起来像你有你的最终规则的一些怪异的行为。我认为它应该看起来更像是:

Now that I look at it some more, it looks like you have some weird behaviour on your final rule. I think it should look more like:

reservations.out: main.o flight.o passenger.o
    gcc -Wall -g main.o flight.o passenger.o -o reservations.out

此外,你有 passenger.o passenger.o 的prerequisite,这是肯定会造成问题。我会做的是这样的事情(也可能是更严格,但我试图使这个简单):

In addition, you have passenger.o as a prerequisite of passenger.o, which is for sure going to cause you problems. What I would do is something like this (it could be even tighter, but I'm trying to make this straightforward):

.PHONY: all clean

all: reservations.out

flight.o: flight.c flight.h
    gcc -Wall -g -c flight.c    

passenger.o: passenger.c passenger.h
    gcc -Wall -g -c passenger.c    

main.o: main.c
    gcc -Wall -g -c main.c     

reservations.out: main.o flight.o passenger.o
    gcc -Wall -g main.o flight.o passenger.o -o reservations.out

clean:
    rm -f *.o reservations.out

但我会的真正的做的,如果你想获得更多的进入细节,如下。这可能是这样一个小项目有点大材小用,但它也更容易调整,并在必要摆弄。

But what I would really do, if you want to get a bit more into the details, is below. It might be a bit overkill for such a small project, but it's also easier to tweak and fiddle with as necessary.

.PHONY: all clean

BIN = reservations
OBJ = flight.o passenger.o main.o

all: $(BIN)

%.o: %.c %.h
    gcc -Wall -g -c $<

main.o: main.c
    gcc -Wall -g -c $<

$(BIN): $(OBJ)
    gcc -Wall -g -o $@ $^

clean:
    rm -f $(OBJ) $(BIN)

我建议您查看 GNU进行手动更多地了解所有的花哨的东西你可以做的。

I recommend checking out the GNU make manual to learn more about all of the fancy stuff you can do.

这篇关于究竟如何我用一个makefile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆