A tutorial for Makefile

转载 2012年03月27日 04:39:30

http://oucsace.cs.ohiou.edu/~bhumphre/makefile.html


Makefile Getting Started

What is Make?

Make is a program that looks for a file called "makefile" or"Makefile", within the makefile are variables and things calleddependencies. There are many things you can do with makefiles, if allyou've ever done with makefiles is compile C or C++ then you aremissing out. Pretty much anything that needs to be compiled(postscript, java, Fortran), can utilize makefiles.

Format of Makefiles -- Variables

First, lets talk about the simpler of the two ideas in makefiles,variables. Variable definitions are in the following format:
VARNAME = Value
So lets say i want to use a variable to set what compiler i'm going touse. This is helpful b/c you may want to switch from cc to gcc or tog++. We would have the following line in our makefile
CC = gcc
this assigns the variable CC to the string "gcc". To expandvariables, use the following form:
${VARNAME}
So to expand our CC variable we would say:
${CC}

Format of Makefiles -- Dependencies

Dependencies are the heart of makefiles. Without them nothing wouldwork. Dependencies have the following form:
dependecy1: dependencyA dependencyB ... dependencyN
    command for dependency1

Very Important:

The commands underneath the dependencies must have a tab before them.This lets make know it is a command and not some kind of crazydependency.
So how do we read what was just written above? Dependency1 isdependent upon dependencyA, and dependencyB, up to however manydependencies you have. The program make, looks at dependency1 andsees it is dependent upon dependencyA, so then it looks later in themakefile for how dependencyA is defined. Lets make a sample makefile:
myprogram: mainprog.cc myclass.cc
    gcc mainprog.cc myclass.cc
That is probably one of the simplest makefiles that could be made.When you type make, it automatically knows you want to compile the'myprogram' dependency (because it is the first dependency it found inthe makefile). It then looks at mainprog.cc and sees when the lasttime you changed it, if it has been updated since last you typed'make' then it will run the 'gcc mainprog.cc ..." line. If not, thenit will look at myclass.cc, if it has been edited then it will executethe 'gcc mainprog.cc ..." line, otherwise it will not do anything forthis rule.

Another example might be easier to see for dependencies, lets say youhave a class, and more than one file depends upon objects of thatclass type. Then it makes sense to create a .o file of that class andcompile it in with the file. Here is a sample of what I'm talkingabout

myprogram: mainprog.cc subfile1.o myclass.o
    gcc mainprog.cc subfile1.o myclass.o

subfile1.o: subfile1.cc myclass.o
    gcc -c mainprog.cc subfile1.o myclass.o

myclass.o: myclass.cc
    gcc -c myclass.cc
Notice the dependencies here, when 'make' is run, the dependencycalled "myprogram" is run first, and then the dependency subfile1.o isfound, and then the myclass.o dependency is foudn withint subfile1.o.So myclass.cc is compiled, then subfile1.o is compiled, then we arefinally back up to the myprogram dependency. Finally all threedependencies are compiled in to create a.out.

Tying variables and dependencies together

You can use variables within your dependencies very easily. Lets takethat last example, and use two variables, one for the compiler we aregoing to use, and another for the options to that compiler. Thismakes life a lot simpler if say we want to turn off debugging for allthe code, or turn it on, whichever is preferred. Here is an exampletying it together.
COMPILER = gcc
CCFLAGS = -g
myprogram: mainprog.cc subfile1.o myclass.o
    ${COMPILER} ${CCFLAGS} mainprog.cc subfile1.o myclass.o

subfile1.o: subfile1.cc myclass.o
    ${COMPILER} ${CCFLAGS} -c mainprog.cc subfile1.o myclass.o

myclass.o: myclass.cc
    ${COMPILER} ${CCFLAGS} -c myclass.cc
notice how clean this is? It is easy to add extra options andlibraries to your compiler if need be. Life is a whole lot simplerwith makefiles, for a final addition to our makefile lets makesomething that cleans out all our code, starting with what we hadbefore, we simply add a new dependency, named "clean". The additiongoes as follows:
COMPILER = gcc
CCFLAGS = -g
myprogram: mainprog.cc subfile1.o myclass.o
    ${COMPILER} ${CCFLAGS} mainprog.cc subfile1.o myclass.o

subfile1.o: subfile1.cc myclass.o
    ${COMPILER} ${CCFLAGS} -c mainprog.cc subfile1.o myclass.o

myclass.o: myclass.cc
    ${COMPILER} ${CCFLAGS} -c myclass.cc

clean: 
    rm -rf *.o a.out 
So when we type 'make clean' at the command line, it will remove allour .o files and the executable that we have.

Conclusions

Makefiles are very very helpful when dealing with projects. The firstthing you should do when making a project is create a makefile, itmakes your life a lot easier. They are very powerful things, if youhave any questions feel free to email me at bhumphre@cs.ohiou.edu have fun!

Last modified: Mon Oct 16 13:21:58 EDT 2000





Docker的基本使用

Ubuntu14.04中Docker的基本使用 1、安装 1.1 使用apt-get安装(非最新版本) sudo apt-get install docker.io 1.2 下载安装(...
  • wang_san_shi
  • wang_san_shi
  • 2015年08月26日 15:49
  • 1330

【Java Tutorial】: Getting Started(开始入门)【官方原版教程】

入门: 这个页面内容提供了你需要了解并开始使用Java编程语言的一切。
  • RuingMan
  • RuingMan
  • 2016年02月05日 17:58
  • 1543

Unity实例.003官方示例Survival Shooter Tutorial核心代码学习

Unity官方教学实例《噩梦射手》(Survival Shooter Tutorial)的一些核心代码的学习,包括如何实现角色的移动,摄像机的跟随,敌人的寻路,玩家的生命控制等等内容。...
  • Constant23
  • Constant23
  • 2017年05月02日 16:15
  • 941

【Java Tutorial中文版 开篇综述】 Oracle(甲骨文)公司Java最新官方教程【译文】

此篇为我博客【Java Tutorial】系列开篇综述。JavaTutorial是Oracle公司的官方教学教程,内容从认识Java到如何开发Java有着非常系统的体系,而且叙述清楚明了,非常适合Ja...
  • RuingMan
  • RuingMan
  • 2016年02月04日 17:28
  • 4359

makefile 详细用法之二

最近在学习Linux下的C编程,买了一本叫《Linux环境下的C编程指南》读到makefile就越看越迷糊,可能是我的理解能不行。             于是google到了以下这篇文章。通俗易懂...
  • dapengbusi
  • dapengbusi
  • 2016年09月18日 16:37
  • 1140

Linux下C++的通用Makefile与解析

本文给出万能Makefile的具体实现,以及对其中的关键点进行解析。所谓C++万能Makefile,即可编译链接所有的C++程序,而只需作很少的修改。 号称万能Makefile,一统江湖。我对原版的...
  • zhoujiaxq
  • zhoujiaxq
  • 2014年05月16日 13:54
  • 10381

多层级的makefile编写——递归调用makefile

文件层级结构: │  Makefile │  tmp │ ├─include │      public.h │ └─src     ├─moda     │      Makefile     │ ...
  • wangyuling1234567890
  • wangyuling1234567890
  • 2014年03月25日 23:52
  • 2410

makefile常用写法和简易写法

先说长用到的-l: 小写的l就是依赖的lib库,lib首字母,很好记。 -L: 引用的三方lib库所在目录,lib首字母大写就是目录,也好记。 -I: 少不了头文件目录,还是老路子,include...
  • persitail
  • persitail
  • 2016年09月20日 12:36
  • 675

Linux下Makefile的编写及四个特殊符号的意义@、$@、$^、$<

我们先看三段C++程序: 一、line1的源码 line1.h #ifndef _LINE_1_H #define _LINE_1_H void line1_print(const char...
  • runfarther
  • runfarther
  • 2015年11月25日 14:47
  • 4673

Makefile 实际用例分析(一) ------- 比较通用的一种架构

这里不再说Makefile的基本知识,如果需要学习,那么请参考: 下载:makefile 中文手册 或者 点击打开链接 或者 跟我一起写Makefile( 陈皓 ) 这里说的是一般的实际的一个工...
  • shanshanpt
  • shanshanpt
  • 2013年12月08日 12:20
  • 5450
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:A tutorial for Makefile
举报原因:
原因补充:

(最多只允许输入30个字)