您的位置:首页 > 理论基础 > 计算机网络

makefiles快速上手教程 原文地址:http://mrbook.org/tutorials/make/

2011-12-29 13:46 591 查看
原文地址:http://mrbook.org/tutorials/make/

译者自述:

最开始接触开源软件时,觉得make很神秘,打开makefile,更加深了这种感觉。好在上学时不需要写大型程序,并且C/C++基本只在VC6下写过,所以一直没有勇气和动力来弄。工作后,尝试使用vim/vimgdb,在里面用gcc很不方面,还是将其写入makefile,直接输入个make命令来的简单。上网查了下,makefile入门也不是很难,找了很多文章,属这个简单,甚喜。独乐乐不如众乐乐,况翻译一遍能加深印象,故有此文。

书写长长的编译命令是很枯燥无聊而又费时的,尤其是包含多个文件的编译命令。令人高兴的是这种人们必须在命令行中敲入编译的命令的时代已经结束了,因为我们将使用makefie,在make工具的帮助下实现自动化编译。Makefiles是遵循make工具可以识别的语法规则编写的文件,可以看作是make的脚本程序。此文中将用到下面这些文件:

main.cpp
hello.cpp
factorial.cpp
functions.h

我建议您将这些文件放到一个单独的文件夹中来使用。

备注:文中我使用了g++。您可以使用任意的编译器来代替文中的g++。

make工具的使用

如果您在命令行中执行

make

那么make工具将在当前目录中查找是否有名为“makefile”的文件并执行。如果您有多个makefiles文件,那么您可以使用-f参数来指定将要执行的makefile:

make -f mymakefile

make还有一些其它的参数,若需更多信息,请man make

Build过程

编译器将源文件编译成object文件
连接器将object文件链接成可执行文件

手动编译

命令行中直接调用编译器/连接器来的方式:

g++ main.cpp hello.cpp factorial.cpp -o hello

Makefile基本结构

Makefile的基本结构包括:

target: dependencies

[tab] system command

按照这种格式,我们的makefile可以写成这样:

all:

g++ main.cpp hell.cpp factorial.cpp -o hello

[Download here]

若执行这个makefile,在命令行中输入:

make -f Makefile-1

在第一个例子中,我们定义了一个名为all的target。这是Makefile的默认target,如果没有指定其它target的话,make将会执行这个target。在这里,target all没有指定dependencies,所以make就直接执行g++命令了。

使用依赖

很多时候使用多个target是很有好处的。如果你仅仅是修改了工程中的一个文件,那么make只需修改那个文件相关的target,而不需要将工程中的文件全部重新编译一遍。

下面是使用多个target的Makefile:

all: hello

hello: main.o factorial.o hello.o

g++ main.o factorial.o hello.o -o hello

main.o: main.cpp

g++ -c main.cpp

factorial.o: factorial.cpp

g++ -c factorial.cpp

hello.o: hello.cpp

g++ -c hello.cpp

clean:

rm -rf *.o hello

[Download here]

在这里,target all 只有一个dependencies,没有系统命令。make会首先满足all的依赖,方法是搜索依赖的target,并执行。依赖的target也许有自己的依赖和命令,那么这个过程递归进行,知道所有的依赖都得到满足。在这个例子中,我们看到有个名为clean的target。这个一般都是用于清理所有的objects和可执行文件。

使用变量和注释

在Makefile中,您也可以定义和使用变量。当需要批量修改编译器参数时,使用变量是很方便的。

#I am a comment, and I want to say that the variable CC will be

#the compiler to use

CC=g++

#Hey! I am comment nuber 2, I want to say that CFLAGS wiil be the

#options I'll pass to the compiler

CFLAGS=-c -Wall

all: hello

hello: main.o factorial.o hello.o

$(CC) main.o factorial.o hello.o -o hello

main.o: main.cpp

$(CC) $(CFLAGS) main.cpp

factorial.o: factorial.cpp

$(CC) $(CFLAGS) factorial.cpp

hello.o: hello.cpp

$(CC) $(CFLAGS) hello.cpp

clean:

rm -rf *.o hello

[Download here]

正如上面所示,变量是十分有用的。定义和使用变量的方式很简单,在写target之前将一个值赋给一个变量即可完成定义,使用$(VAR)可以获得变量值。

Where to go from here

With this brief introduction to Makefiles, you can create some very sophisticated mechanism for compiling your projects. However, this is just a tip of the iceberg. I don't expect anyone to fully understand the example presented below without
having consulted some Make documentation (which I had to do myself) or read pages 347 to 354 of your Unix book.
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp hello.cpp factorial.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=hello

all: $(SOURCES) $(EXECUTABLE)
	
$(EXECUTABLE): $(OBJECTS) 
	$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
	$(CC) $(CFLAGS) $< -o $@

[Download here]

If you understand this last example, you could adapt it to your own personal projects changing only 2 lines, no matter how many additional files you have !!!.
下一步的学习
完成文中的例子后,您已经可以写一个基本的makefile了。然而,这里的东西仅仅是冰山一角,我没指望所有人都能在不查Make documentation的情况下完全理解下面的代码(即使我自个儿也得查了才明白)。

CC=g++

CFLAGS=-c -Wall

LDFLAGS=

SOURCES=main.cpp hello.cpp factorial.cpp

OBJECTS=$(SOURCES: .cpp =.o)

EXECUTABLE=hello

all: $(SOURCES): $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)

$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:

$(CC) $(CFLAGS) $< -O $@

[Download here]

如果您能理解这段代码,那么无论您的工程含多少个文件,只需修改其中两行这个makefile便可用于您的工程。

参考:
http://wiki.ubuntu.org.cn/%E8%B7%9F%E6%88%91%E4%B8%80%E8%B5%B7%E5%86%99Makefile:MakeFile%E4%BB%8B%E7%BB%8D

http://web.mit.edu/gnu/doc/html/make_3.html

http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐