Linux Makefile Howto

*/-->

Linux Makefile Howto

As far as I can remember, I have worked with makefile for quite a while, and I
will look up for the meaning of all kinds of symbols in it, and after a
thorough understanding of one makefile and the combination of a makefile I used
most, I will post my own version of the makefile.

I will borrow the example I found on the
Internet.(A Simple Makefile Tutorial) and (makefile by examples)

The following files are needed to make the compilation correct:

hellomake.h
extern void print_hello(void);
print_fun.c
#include <stdio.h>
#include <stdlib.h>

void print_hello(void)
{
    printf("Hello make world!\n");
}
main.c
#include "hellomake.h"

int main(void)
{
    print_hello();
}

In a typical small or medium sized project, these directories are needed:

name function
src all the c or cpp files
src/obj the compiler generated object files, which are normally correspondents with src files
exe the final output of the project
libs the external libs the project needs
include all the .h files needed by other .c files

The src/Makefile has the following contents:

CC=gcc
IDIR = ../include
CFLAGS=-I$(IDIR)

ODIR = obj
LDIR = ../libs

LIBS = -lm

_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

SOURCES = $(wildcard *.c)
_OBJ = $(SOURCES:.c=.o)
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

EXE = hellomake
EXEDIR = ../exe

$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o [email protected] $< $(CFLAGS)

$(EXEDIR)/$(EXE): $(OBJ)
    gcc -o [email protected] $^ $(CFLAGS) $(LIBS)

.PHONY:clean

clean:
    rm -f $(ODIR)/*.o $(EXEDIR)/$(EXE)

A fe-fined version of the makefile looks like:

CC=gcc
IDIR = ../include
CFLAGS=-I$(IDIR)
ODIR = obj
LDIR = ../libs
EXEDIR = ../exe
LIBS = -lm
DEPS = $(IDIR)/$(wildcard *.h)
SOURCES = $(wildcard *.c)
_OBJ = $(SOURCES:.c=.o)
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

#the name of the executable file should be changed when used in a specific project
EXE = hellomake

$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o [email protected] $< $(CFLAGS)

$(EXEDIR)/$(EXE): $(OBJ)
    gcc -o [email protected] $^ $(CFLAGS) $(LIBS)

.PHONY:clean

clean:
    rm -f $(ODIR)/*.o $(EXEDIR)/$(EXE)

#make project-dir-tree is used to create the necessary directory structure used in this makefile
project-dir-tree:
    mkdir include libs src src/obj exe
    cp Makefile ./src

In order to use this makefile, you should first create a project directory say,
test_project, and you copy the makefile to the newly created directory, then
invoke make project-dir-tree the following directories will appear, and you
can organize your project clearly.

exe include libs src

But if you are working on a relatively small project which involves only a
couple of source files, you can use this simplified version of makefile:

CC=gcc
CFLAGS = -I.
DEFPS = $(wildcard *.h)
SOURCES = $(wildcard *.c)
OBJ = $(SOURCES:.o=.c)
LIBS = -lm

EXE = test

%.o:%.c $(DEPS)
    gcc -c -o [email protected] $< $(CFLAGS)

$(EXE):$(OBJ)
    gcc -o [email protected] $^ $(CFLAGS) $(LIBS)

.PHONY:clean

clean:
    rm -rf *.o $(EXE)

With this file in hand, you can modify the desired output of the EXE to get
the desired exe-file, and that is all you need do, enjoy it!

By the way, if you don‘t want to compile all the source files in a directory,
you can specify the sources files you want to compile by changing the SOURCES
variable, like

SOURCES = a.c b.c

Author: wujing

Created: 2014-08-26 二 10:08

Emacs 24.3.1 (Org mode 8.2.6)

Validate

时间: 2024-08-06 05:50:19

Linux Makefile Howto的相关文章

Linux makefile教程之总述二[转]

Makefile 总述——————— 一.Makefile里有什么? Makefile里主要包含了五个东西:显式规则.隐晦规则.变量定义.文件指示和注释. 1.显式规则.显式规则说明了,如何生成一个或多的的目标文件.这是由Makefile的书写者明显指出,要生成的文件,文件的依赖文件,生成的命令. 2.隐晦规则.由于我们的make有自动推导的功能,所以隐晦的规则可以让我们比较粗糙地简略地书写Makefile,这是由make所支持的. 3.变量的定义.在Makefile中我们要定义一系列的变量,变

Linux makefile教程之概述一[转]

概述—— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些 Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂.这就好像现在有这么多 的HTML的编辑器,但如果你想成为一个专业人士,你还是要了解HTML的标识的含义.特别在Unix下的软件编译,你就不能不自己写makefile 了,会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力. 因为,makefile关系到了整个工

Linux makefile 教程 很具体,且易懂

近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了下面这篇文章.通俗易懂.然后把它贴出来,方便学习. 后记,看完发现这篇文章和<Linux环境下的C编程指南>的makefile一章所讲述的惊人的类似,仅仅是这篇文章从一个实例切入,在有些地方比較好理解.能让人看懂就是好文章. 跟我一起写 Makefile陈皓 (CSDN)概述--什么是makefile?也许非常多Winodws的程序

很详细、很移动的Linux makefile教程:介绍,总述,书写规则,书写命令,使用变量,使用条件推断,使用函数,Make 的运行,隐含规则 使用make更新函数库文件 后序

很详细.很移动的Linux makefile 教程 内容如下: Makefile 介绍 Makefile 总述 书写规则 书写命令 使用变量 使用条件推断 使用函数 make 的运行 隐含规则 使用make更新函数库文件 后序 近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出来,方便学习. 后记,看完发现这篇文章和<Linux环境下的C编程指南>

linux makefile自动生成

一.Linux Makefile介绍 Linux Makefile是用于自动编译和链接的,一个工程有很多文件组成,每一个文件的改变都会导致工程的重新链接,但是不是所有的文件都需要重新编译,Linux Makefile中纪录有文件的信息,在make时会决定在链接的时候需要重新编译哪些文件. Linux Makefile的宗旨就是:让编译器知道要编译一个文件需要依赖其他的哪些文件.当那些依赖文件有了改变,编译器会自动的发现最终的生成文件已经过时,而重新编译相应的模块. Linux Makefile的

Linux makefile教程之隐含规则九[转]

隐含规则 ———— 在 我们使用Makefile时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix下是[.o] 文件,Windows下是[.obj]文件).本章讲述的就是一些在Makefile中的“隐含的”,早先约定了的,不需要我们再写出来的规则. “隐含规则”也就是一种惯例,make会按照这种“惯例”心照不喧地来运行,那怕我们的Makefile中没有书写这样的规则.例如,把[.c]文件编译成[.o]文件这一规则,你根本就不用写出来,ma

Linux makefile教程之更新函数库文件十[转]

使用make更新函数库文件 ——————————— 函数库文件也就是对Object文件(程序编译的中间文件)的打包文件.在Unix下,一般是由命令"ar"来完成打包工作. 一.函数库文件的成员 一个函数库文件由多个文件组成.你可以以如下格式指定函数库文件及其组成: archive(member) 这个不是一个命令,而一个目标和依赖的定义.一般来说,这种用法基本上就是为了"ar"命令来服务的.如: foolib(hack.o) : hack.o ar cr fooli

Linux makefile教程之使用变量五[转]

使用变量 ———— 在 Makefile中的定义的变量,就像是C/C++语言中的宏一样,他代表了一个文本字串,在Makefile中执行的时候其会自动原模原样地展开在所使 用的地方.其与C/C++所不同的是,你可以在Makefile中改变其值.在Makefile中,变量可以使用在“目标”,“依赖目标”,“命令”或是 Makefile的其它部分中. 变量的命名字可以包含字符.数字,下划线(可以是数字开头),但不应该含有“:”.“#”.“=”或是空 字符(空格.回车等).变量是大小写敏感的,“foo”

Linux makefile教程之函数七[转]

使用函数 ———— 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函数的返回值可以当做变量来使用. 一.函数的调用语法 函数调用,很像变量的使用,也是以“$”来标识的,其语法如下: $(<function> <arguments> ) 或是 ${<function> <arguments>} 这 里,<function>就是函数名,m