简体   繁体   English

我到底如何使用makefile?

[英]How exactly do I use a makefile?

I'm really confused at the moment. 此刻我真的很困惑。

So I have 5 files: main.c, flight.c, flight.h, passenger.c, and passenger.h 所以我有5个文件:main.c,flight.c,flight.h,passenger.c和passenger.h

flight.h has function prototypes for flight.c, and passenger.h has function prototypes for passenger.c flight.h具有用于flight.c的函数原型,而passenger.h具有用于passenger.c的函数原型

flight.c and passenger.c have definitions for these functions. flight.c和passenger.c具有这些功能的定义。

main.c is the program I'll interact with, calling functions from both .c files main.c是我将与之交互的程序,同时从两个.c文件中调用函数

I'm not really sure what .o files are for, somebody please care to explain? 我不太确定.o文件是做什么用的,请有人解释。 Here is my Makefile: 这是我的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

EDIT: I then use the command "make Makefile", and get an error: make: Nothing to be done for `Makefile'. 编辑:然后我使用命令“ make Makefile”,并得到一个错误:make:对`Makefile'不需要做任何事情。

In my file there is a tab before gcc. 在我的文件中,gcc之前有一个选项卡。 I'd appreciate any help, I'm sure there are lots of things I'm doing wrong. 我将不胜感激,我确定我做错了很多事情。 Thanks. 谢谢。

Your problem is that you're using the wrong command. 您的问题是您使用了错误的命令。 I would add the following line to the top of your makefile: 我将以下行添加到makefile的顶部:

all: reservations.out

Then just type make at the command line. 然后,在命令行中键入make If you don't want to edit the file, just use make reservations.out and you should get the right behaviour. 如果您不想编辑该文件,只需使用make 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. 至于.o文件是什么:它是一个“目标”文件,意味着它包含编译后的(但在这种情况下为未链接)代码。 Your final makefile rule takes the object files and links them together into a final executable called reservations.out . 最终的makefile规则获取目标文件,并将它们链接在一起,成为最终的可执行文件,称为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

In addition, you have passenger.o as a prerequisite of passenger.o , which is for sure going to cause you problems. 此外,您还passenger.o作为一个先决条件passenger.o ,这是肯定会造成问题。 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)

I recommend checking out the GNU make manual to learn more about all of the fancy stuff you can do. 我建议您查阅GNU make手册,以了解有关您可以做的所有奇妙事情的更多信息。

I'm not really sure what .o files are for, somebody please care to explain? 我不太确定.o文件是做什么用的,请有人解释。

.o are object files, the temporary files which are created by the compiler, which contains the object code for the class. .o是目标文件,即由编译器创建的临时文件,其中包含类的目标代码。 The object codes will be linked by the linker to create an executable binary file. 目标代码将由链接器链接以创建可执行的二进制文件。

make has implicit rules for making binaries out of C source, you don't need to specify explicit rules for this. make具有使用C源制作二进制文件的隐式规则,因此无需为此指定显式规则。

So your minimalistic Makefile would look like 所以您的简约Makefile看起来像

CFLAGS = -Wall -g
.PHONY all clean

all: myprogram

myprogram: source1.c source2.c

clean:
    rm -f myprogram

It has one useful side effect - all intermediary files ( *.o files in your case) will be deleted automatically after the compilation. 它有一个有用的副作用-编译后,所有中间文件(在您的情况下为*.o文件)都会自动删除。

Options from CFLAGS will be added to gcc command line - make implicit rule does it. CFLAGS选项将添加到gcc命令行- make隐式规则执行此操作。

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

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