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. mysql的binlog太多太大占用了空间的解决办法

    现象:网站访问越来越慢,最后无法访问了,经过检查发现磁盘满了 分析过程及解决方案:通常出现这种问题都应该登录服务器检查磁盘.内存和进程使用的情况,通过top.df –h和free –m来检查,发现磁盘 ...

  2. Vim 替换命令

    一,":substitute"的使用 :substitute 命令可以对一个指定范围的区域执行替换操作,可以简写为:s ,它的通用形式如下: :[range]substitute/ ...

  3. poj piggy-bank

                                         Piggy-Bank Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  4. vim配置与使用

    Vim 是一个上古神器,本篇文章主要持续总结使用 Vim 的过程中不得不了解的一些指令和注意事项,以及持续分享一个前端工作者不得不安装的一些插件,而关于 Vim 的简介,主题的选择,以及为何使用 vi ...

  5. Win7.窗口自动滚动回到屏幕

    PS:笔记本 ThinkPad E440 1.前提:窗口 的一部分位于在屏幕的外面,此窗口处于激活的状态 操作:鼠标放置在窗口内部,鼠标继续往屏幕外部移动,鼠标没有任何其他事件(只有 MouseMov ...

  6. invalid constant type: 18

    javassist 3.18以下的版本不支持在JDK1.8下运行

  7. 一种解决 MacBook 里的 App Store 无法登录的问题

    刚刚买回来的 2018 款带有 touchbar 的 MacBook Pro 15 inc 在用 App Store 安装 app 时一直无法登录成功(网络链接都是好的),导致软件都无法更新,折腾了挺 ...

  8. Mule ESB学习【转-结合了网络上的一些资源】

    1.SOA标准之一:SCA架构思想 SOA在Java领域有两套标准:一个是SUN推出的JBI(没有得到BEA和IBM的承认),另外一个是:IBM和BEA等公司推出的SCA和SDO标准. JBI之关注J ...

  9. myod中遇到的问题

    一.准备工作 首先在编程之前遇到的第一个问题就是要了解需要编出一个怎样的代码,了解od -tx -tc的具体意思,并观察其输出结果. -tc代表着输出ASCII字符,而-tx则是代表着输出ASCII字 ...

  10. xdebug浏览器调试参数

    XDEBUG_SESSION_START=phpstorm-xdebug 找到对应PHP版本的 Xdebug ,后面带 TS 的为线程安全,本机环境为 win7 64 + php-5.5.1-Win3 ...