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

  1. Compiler takes the source files and outputs object files
  2. 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的更多相关文章

  1. Makefiles 介绍

    http://www-personal.umich.edu/~ppannuto/writings/makefiles.html Makefiles Makefiles (or, the GNU aut ...

  2. Linux Kernel Makefiles Kbuild en

    来自Linux kernel docs,顺便整理了一下排版 Linux Kernel Makefiles This document describes the Linux kernel Makefi ...

  3. VisualGDB Makefiles

    以下内容是VisualGDB官网对VisualGDB编译时获取相关编译信息的说明: When you create a new project using VisualGDB, it will gen ...

  4. library Makefiles

    libpng library Makefile LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LS_C=$(subst $(1)/,,$(wild ...

  5. 【嵌入式】——makefiles

    汇编通用makefile: 命令行编辑: 编译 arm-linux-as -march=armv5te -o led.o led.s -march 指定的指令集的版本 指定架构 连接 arm-linu ...

  6. Makefiles in Linux

    http://www.codeproject.com/Articles/31488/Makefiles-in-Linux-An-Overview

  7. 说说Makefile那些事儿

    说说Makefile那些事儿 |扬说|透过现象看本质 工作至今,一直对Makefile半知半解.突然某天幡然醒悟,觉得此举极为不妥,只得洗心革面从头学来,以前许多不明觉厉之处顿时茅塞顿开,想想好记性不 ...

  8. 7个高性能JavaScript代码高亮插件

    本文由码农网 – 小峰原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 对于喜欢写技术博客的同学来说,一定对代码高亮组件非常熟悉.一款优秀的JavaScript代码高亮插件,将会帮助你渲染 ...

  9. Ubuntu15.04YouCompleteMe插件安装

    0x00. 简介 YouCompleteMe号称Vim的自动补全神器,YouCompleteMe: a code-completion engine for Vim,该项目在github的地址:You ...

随机推荐

  1. 2014年的暑假ACM之旅!

    致未来的我: 回到学校了,又开始了繁忙的生活! 虽然每天都不太轻松,但还是蛮有乐趣的,一起讨论某道题或者某种算法时挺开心的.@我.@姜维波.@曹彦宝.@李岩.@张永宏 继续这样下去,直到这个暑假的结束 ...

  2. matlat之KDTreeSearcher()函数

    Create Kd-tree nearest neighbor searcher(创建kd-树最近邻搜索器). Description KDTreeSearcher model objects sto ...

  3. 封装一个简单的Hibernate SessionFactory

    封装Hibernate框架中的session工厂   ,方便很多,免去了很多重复无用的代码 package com.maya.test; import org.hibernate.*; import ...

  4. python 链接MS SQL

    cnxn = pyodbc.connect(driver='{SQL Server}', host=server, database=db1, trusted_connection=tcon, use ...

  5. 关于MFC资源句柄、ID和对象

    一.资源.句柄和ID 资源: MFC中的资源,如菜单.对话框.图标.工具条.对话框等,是windows创建的,并占用堆内存.windows在创建这些资源时候会给每个资源分配一个句柄,用来标记这些资源, ...

  6. 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】

    就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  7. Cannot find PHPUnit in include path (.;C:\php5\pear)

    --pear channel-discover pear.phpunit.de --pear install phpunit/PHPUnit 此时会显示: No valid packages foun ...

  8. 【LeetCode】060. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  9. centos6.5升级默认的Mysql到5.5方法

    0.用lsb_release -a 查看linux系统的版本 1.官网下载bundle或rpm版2.解压 tar -xvf MySQL-xxx.tar或 MySQL-server-xxx.rpm和My ...

  10. RSA-CRT leaks__因使用中国余数定理计算RSA所引起的私钥泄露

    在heartbleed[1]漏洞后,很多用户打开了PFS[2]功能.但很不幸,之后RedHat又报告出在多个平台上存在RSA-CRT导致的密钥泄露[3]. 中国余数定理(CRT)常被用在RSA的计算中 ...