本文为原创文章,转帖需指明该文链接

目录结构如下:

comm/inc/apue.h

comm/errorhandler.c

atexit.c

Makefile

文件内容如下:

apue.h

   #ifndef __apue_h__
  2 #define __apue_h__ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> //for definition of erron
#include <stdarg.h> //ISO C variable arguments
#define MAXLINE 4096 //max line length
void err_dump(const char *fmt, ...);
1 void err_msg(const char *fmt, ...);   #endif

errorhandler.c

   #include "apue.h"

   #define ERR_MESSAGE_NEED    1
#define ERR_MESSAGE_NO 0 static void err_doit(int errnoflag, int error, const char *fmt, va_list ap); //print a message, dupm core, and terminate
void err_dump(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(ERR_MESSAGE_NEED, errno, fmt, ap);
va_end(ap);
abort();
exit();
}

atexit.c

   #include "apue.h"

   static void my_exit1(void);
static void my_exit2(void); int main(void)
{
if( != atexit(my_exit2))
err_sys("can't register my_exit2");
if( != atexit(my_exit1))
err_sys("can't register my_exit1");
if( != atexit(my_exit1))
err_sys("can't register my_exit1");
printf("main is done\n");
return ;
}

Makefile

   CC       = gcc
CFLAGS = -Wall -O -g
CXXFLAGS =
INCLUDE = -I ./comm/inc
TARGET = atexit
#search paths for errorhandler.c,当存在多个路径时,可以使用 空格 或 : 来分割这些路径
vpath %.c ./comm
8 #下行是为依赖项 apue.h 准备的,比如 [errorhandler.o:errorhandler.c apue.h] 里的
vpath %.h ./comm/inc OBJS = errorhandler.o atexit.o
all:$(OBJS)
$(CC) $(CFLAGS) $(INCLUDE) -o $(TARGET) $^
@echo ---target:$@
@echo ---depend:$^
16 #下行的 apue.h,可以不必写出来
errorhandler.o:errorhandler.c apue.h
$(CC) $(CFLAGS) $(INCLUDE) -c $^
@echo ---target:$@
@echo ---depend:$^
atexit.o:atexit.c apue.h
$(CC) $(CFLAGS) $(INCLUDE) -c $^
@echo ---target:$@
@echo ---depend:$^
clean:
rm -f *.o
rm -f $(TARGET)

在 Makefile 里

INCLUDE  = -I ./comm/inc 是为 gcc 编译文件时使用的

vpath %.c ./comm  vpath %.h ./comm.inc 是为 make 程序使用

@echo ---target:$@  是为了测试 $@ 是什么内容   @echo ---depend:$^ 是为了测试 $^ 是什么内容

Makefile 的自动变量

$@  表示目标文件
    $^    表示所有的依赖文件
    $<    表示第一个依赖文件
    $?    表示比目标还要新的依赖文件列表

Makefile 实例实践的更多相关文章

  1. makefile实例(1)-helloworld

    简单makefile实例 1,源文件: main.cpp #include <stdio.h> int main() { printf("Hello World\n") ...

  2. Ubuntu下比较通用的makefile实例

    本文转自http://blog.chinaunix.net/uid-20608849-id-360294.html  笔者在写程序的时候会遇到这样的烦恼:一个项目中可能会有很多个应用程序,而新建一个应 ...

  3. Linux下GCC和Makefile实例(从GCC的编译到Makefile的引入)

    一.确认已经装好了GCC和Make的软件包 可以使用whereis命令查看: 如果whereis  gcc和whereis  make命令有结果,说明安装了这两个软件,可以继续往下做. 二.使用GCC ...

  4. makefile实例(3)-多个文件实例优化

    我们先看一下make是如何工作的在默认的方式下,也就是我们只输入make命令.那么,1.make会在当前目录下找名字叫“Makefile”或“makefile”的文件.2.如果找到,它会找文件中的第一 ...

  5. makefile实例(2)-多个文件实例

    1,源文件依赖关系 defs.h command.h buffer.h main.cpp * util.cpp * kde.cpp * * command.cpp * * display.cpp * ...

  6. RxJava/RxAndroid 使用实例实践

    原文地址 RxAndroid Tutorial响应式编程(Reactive programming)不是一种API,而是一种新的非常有用的范式,而RxJava就是一套基于此思想的框架,在Android ...

  7. Linux下GCC和Makefile实例(从GCC的编译到Makefile的引入) 转

    http://www.crazyant.net/2011/10/29/linux%E4%B8%8Bgcc%E5%92%8Cmakefile%E5%AE%9E%E4%BE%8B%EF%BC%88%E4% ...

  8. makefile实例

    #.PHONY:cleanall cleanobj cleandiff #cleanall:cleandiff cleanobj # rm program #cleanobj: # rm obj.c ...

  9. Makefile 实例

    CROSS_COMPILE = HI_CFLAGS= -Wall -O2 -g -march=armv7-a -mcpu=cortex-a9 -mfloat-abi=softfp -mfpu=vfpv ...

随机推荐

  1. hue启用ldap

    [desktop] [[auth]] …… …… backend=desktop.auth.backend.LdapBackend .….. http://gethue.com/ldap-or-pam ...

  2. 开发 nodejs REST 个人感想

    本来想拿 nodejs 做个 ip 查询小应用的,做的时候想着把基础弄好再做应用,没想到做着做着就变成 spring 了 可能太多数人觉得不知道怎么用,以后我会写详细点使用教程 个人感觉自己做出来的东 ...

  3. (转)mysql的单向复制

    mysql的单向复制操作很简单:大概只需要二十分钟看完这篇文章就能搞定了.http://11837782.blog.51cto.com/11827782/1885967 为了提高主从服务器的健壮性,我 ...

  4. automake--关于两个文件configure.in和Makefile.am的编写

    http://blog.csdn.net/shanzhizi/article/details/30251763 automake主要通过编辑Makefile.am来控制它的行为,下面就常用的三个Mak ...

  5. IOS 开发学习33 使用sqlite3

    sqlite3 命令行简单使用 sqlite3 路径 //打开数据库路径连接 select * from sqlite_master where type="table"; //显 ...

  6. GetCursorPos

      获取桌面坐标 using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...

  7. RTP Tools

    RTP Tools (Version 1.20) https://wiki.wireshark.org/RTP_statistics Here is a small example: Install ...

  8. vue - dist

    描述:打包后准备上线的文件(需要服务器环境才能运行!!!)

  9. vue2.0快速构建项目

    准备工作:已经安装了nodejs,已经安装了vue-cli $ mkdir gankbook $ cd gankbook $ vue init webpack-simple 按照需要写好信息,这将会写 ...

  10. 《C++ Primer》 第四版 第7章 函数

    <C++ Primer> 第四版 第7章 函数 思维导图笔记 超级具体.很具体,图片版,有利于复习查看 http://download.csdn.net/detail/onlyshi/94 ...