A tutorial by example(转载)
转自:http://mrbook.org/blog/tutorials/make/
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-
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 . 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 !!!.
A tutorial by example(转载)的更多相关文章
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
In my recent post on How To Choose the Best XML Parser for Your iPhone Project, Saliom from the comm ...
- (转载)XML Tutorial for iOS: How To Choose The Best XML Parser for Your iPhone Project
There are a lot of options when it comes to parsing XML on the iPhone. The iPhone SDK comes with two ...
- OpenFOAM Tutorial Standard Solvers【转载】
转载自:http://www.cnblogs.com/fortran/articles/1996927.html boundaryFoam Steady-state solver for 1D tur ...
- 【转载】C# Tutorial - Simple Threaded TCP Server
http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server In this tutorial I'm going t ...
- [转载] CMake Official Tutorial——教程还是官方的好
CMake官方教程传送门:https://cmake.org/cmake-tutorial/ 以下的内容跟官方教程基本一致,少数地方根据自己的测试有所改动: A Basic Starting Poin ...
- 【转载】Pytorch tutorial 之Datar Loading and Processing
前言 上文介绍了数据读取.数据转换.批量处理等等.了解到在PyTorch中,数据加载主要有两种方式: 1.自定义的数据集对象.数据集对象被抽象为Dataset类,实现自定义的数据集需要继承Datase ...
- OpenGL.Tutorial文章转载
ZC:本来以为没有中文版的,原来有中文版,网址为: ZC: OpenGL3.0教程 _ 泰然网.html(http://www.tairan.com/archives/6126/) ZC: OpenG ...
- 【转载】Ogre:Beginner Tutorial 1: SceneNode, Entity,和SceneManager 结构
原文:Beginner Tutorial 1: SceneNode, Entity,和SceneManager 结构 先决条件 这个教程假设你有C++编程的基础并且可以配置并编译OGRE应用程序 ...
- Tutorial: Reverse debugging with GDB 7 (转载)
Tutorial: Reverse debugging with GDB 7 Tutorial: Reverse debugging with GDB 7 by Jay Conrod posted o ...
- A re-introduction to JavaScript (JS Tutorial) 转载自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
A re-introduction to JavaScript (JS Tutorial) Redirected from https://developer.mozilla.org/en-US/do ...
随机推荐
- Tips/Tricks in Deep Neural Networks
转自: http://lamda.nju.edu.cn/weixs/project/CNNTricks/CNNTricks.html
- scrollReveal 使用
传统的layzload只能适用于图片懒加载,而我们现在需要的是全部元素的懒加载! 官网:https://scrollrevealjs.org/ gitHub:https://github.com/jl ...
- scss - 语法
1.在一个样式导入另一个样式(@import "example.css") 2.scss单行注释不会显示出来 3.强大的变量(定义后,全局可使用) 4.全局默认变量(加!defau ...
- 集成CCFlow工作流与GPM的办公系统驰骋CCOA介绍(三)
通过组织结构能够对项目的岗位.部门.人员进行增删改操作. 加入新部门.并为新部门加入人员: 选中部门后,点击鼠标右键,能够选择加入平级部门或下属部门. 新建部门时,须要给部门设置部门编号.名称.与部门 ...
- 如何正确地在React中处理事件
1.构造器内绑定this class MyComponent extends React.Component { constructor(props) { super(props); this.sta ...
- binary-tree-preorder-traversal——前序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Linux内存管理之mmap详解 (可用于android底层内存调试)
注:将android底层malloc换为mmap来获取内存,可将获取到的内存添加tag,从而再利用meminfo进行分析,可单独查看该tag的内存,从而进行分析. 一. mmap系统调用 1. mma ...
- Python源代码--整数对象(PyIntObject)的内存池
[背景] 原文链接:http://blog.csdn.net/ordeder/article/details/25343633 Python整数对象是不可变对象,什么意思呢?比如运行例如以下pytho ...
- MongoDB与MySQL的插入性能测试【转】
1.1 MongoDB的简单介绍 在当今的数据库市场上,MySQL无疑是占有一席之地的.作为一个开源的关系型数据库,MySQL被大量应用在各大网站后台中,承担着信息存储的重要作用.2009年,甲骨文 ...
- js中创建html标签、加入select下默认的option的value和text、删除select元素节点下全部的OPTION节点
<pre name="code" class="java"> jsp 中的下拉框标签: <s:select name="sjx&qu ...