Makefiles
A tutorial by example
Compiling your source code files can be tedious, specially when you want
to include several source files and have to type the compiling command
everytime you want to do it.
Well, I have news for you… Your days of command line compiling are (mostly) over, because YOU will learn how to write Makefiles.
Makefiles are special format files that together with the make utility will help you to automagically build and manage your projects.For this session you will need these files:
I recommend creating a new directory and placing all the files in there.
note: I use g++ for compiling. You are free to change it to a compiler of your choice
The make utility
If you run
make
this program will look for a file named makefile in your directory, and then execute it.
If you have several makefiles, then you can execute them with the command:
make -f MyMakefile
There are several other switches to the make utility. For more info, man make.
Build Process
- Compiler takes the source files and outputs object files
- Linker takes the object files and creates an executable
Compiling by hand
The trivial way to compile the files and obtain an executable, is by running the command:
g++ main.cpp hello.cpp factorial.cpp -o hello
The basic Makefile
The basic makefile is composed of:
target: dependencies
[tab] system command
This syntax applied to our example would look like:
all:
g++ main.cpp hello.cpp factorial.cpp -o hello
[Download here]
To run this makefile on your files, type:
make -f Makefile-1
On this first example we see that our target is called all. This is the default target for makefiles. The make utility will execute this target if no other one is specified.
We also see that there are no dependencies for target all, so make safely executes the system commands specified.
Finally, make compiles the program according to the command line we gave it.
Using dependencies
Sometimes is useful to use different targets. This is because if you
modify a single file in your project, you don’t have to recompile
everything, only what you modified.
Here is an example:
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 *o hello
[Download here]
Now we see that the target all has only dependencies, but no system commands. In order for make to execute correctly, it has to meet all the dependencies of the called target (in this case all).
Each of the dependencies are searched through all the targets available and executed if found.
In this example we see a target called clean. It is useful to have such target if you want to have a fast way to get rid of all the object files and executables.
Using variables and comments
You can also use variables when writing Makefiles. It comes in handy in situations where you want to change the compiler, or the compiler options.
# 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 number 2. I want to say that CFLAGS will 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 *o hello
[Download here]
As you can see, variables can be very useful sometimes. To use them, just assign a value to a variable before you start to write your targets. After that, you can just use them with the dereference operator $(VAR).
Where to go from here
With this brief introduction to Makefiles, you can create some very sophisticated mechanisms for compiling your projects. However, this is just the 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 !!!.
Makefiles的更多相关文章
- Makefiles 介绍
http://www-personal.umich.edu/~ppannuto/writings/makefiles.html Makefiles Makefiles (or, the GNU aut ...
- Linux Kernel Makefiles Kbuild en
来自Linux kernel docs,顺便整理了一下排版 Linux Kernel Makefiles This document describes the Linux kernel Makefi ...
- VisualGDB Makefiles
以下内容是VisualGDB官网对VisualGDB编译时获取相关编译信息的说明: When you create a new project using VisualGDB, it will gen ...
- library Makefiles
libpng library Makefile LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LS_C=$(subst $(1)/,,$(wild ...
- 【嵌入式】——makefiles
汇编通用makefile: 命令行编辑: 编译 arm-linux-as -march=armv5te -o led.o led.s -march 指定的指令集的版本 指定架构 连接 arm-linu ...
- Makefiles in Linux
http://www.codeproject.com/Articles/31488/Makefiles-in-Linux-An-Overview
- 说说Makefile那些事儿
说说Makefile那些事儿 |扬说|透过现象看本质 工作至今,一直对Makefile半知半解.突然某天幡然醒悟,觉得此举极为不妥,只得洗心革面从头学来,以前许多不明觉厉之处顿时茅塞顿开,想想好记性不 ...
- 7个高性能JavaScript代码高亮插件
本文由码农网 – 小峰原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 对于喜欢写技术博客的同学来说,一定对代码高亮组件非常熟悉.一款优秀的JavaScript代码高亮插件,将会帮助你渲染 ...
- Ubuntu15.04YouCompleteMe插件安装
0x00. 简介 YouCompleteMe号称Vim的自动补全神器,YouCompleteMe: a code-completion engine for Vim,该项目在github的地址:You ...
随机推荐
- mysqldump导出格式
#导出大表:mysqldump --add-drop-table --single-transaction --triggers -R -quick --disable-keys -utest -pt ...
- HDU 4714 Tree2cycle:贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...
- php数据结构课程---3、队列(队列实现方法)
php数据结构课程---3.队列(队列实现方法) 一.总结 一句话总结: 1.数据实现:适用于功能不复杂的情况 2.链表实现:受限链表,只能队头队尾操作:适用于功能复杂情况 1.队列的数组实现注意点? ...
- EF学习 开始操作02
历史版本和未来版本 本文介绍有关实体框架 (EF5) 最新版本的信息,其中大部分内容也适用于旧版本.有关完整版本列表以及各版本引入的功能的详细信息,请参阅 “版本历史”. “历史版本”页面包含实体框架 ...
- ListOperations
RedisOperations<K,V> getOperations() V index(K key, long index) V leftPop(K key) V leftPop( ...
- codeforces 598E E. Chocolate Bar(区间dp)
题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 2016北京集训 小Q与进位制
题目大意 一个数每一位进制不同,已知每一位的进制,求该数的十进制表达. 显然有 $$Ans=\sum\limits_{i=0}^{n-1}a_i \prod\limits_{j=0}^{i-1}bas ...
- Spring MVC表单提交
实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式. <form id="form ...
- CodeForces - 605C 凸包+直线与凸包判交
题目大意: 要完成两种属性p,q的需求,给定n个双属性物品及其单位个物品中含有的属性,要求选择最少的物品来达成属性需求.(可以选择实数个物品) 题解: 实际上是一种属性混合问题 我们知道对于两种双属性 ...
- Undefined exploded archive location(在myeclipse中TOMCAT不能发布程序。)
原因: 在工程转移过程中,导致工程的配置文件出错: 解决方法: 1.在工程目录下的.mymetadata文件中可能webrootdir被改无效了(把下面内容拷到你的 ...