This article is an original article, reprint need to indicate the link
The directory structure is as follows:
Comm/inc/apue.h
Comm/errorhandler.c
Atexit.c
Makefile
The contents of the file are as follows:
Apue.h
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <errno.h>//For definition of Erron 5#include <stdarg.h>//ISO C variable Arguments 6 #defineMAXLINE 4096//Max line Length 7 voidErr_dump (Const Char*fmt, ...); 8 voidErr_msg (Const Char*FMT, ...);
Errorhandler.c
1#include"apue.h" 2 3 #defineErr_message_need 14 #defineErr_message_no 05 6 Static voidErr_doit (intErrnoflag,intErrorConst Char*FMT, va_list AP); 7 8 //print a message, DUPM core, and terminate 9 voidErr_dump (Const Char*FMT, ...) Ten { Oneva_list ap; AVa_start (AP, FMT); -Err_doit (Err_message_need, errno, FMT, AP); -Va_end (AP); theabort (); -Exit1); -}
Atexit.c
1#include"apue.h" 2 3 Static voidMy_exit1 (void); 4 Static voidMy_exit2 (void); 5 6 intMainvoid) 7 { 8 if(0!=atexit (my_exit2))9Err_sys ("can ' t register my_exit2"); Ten if(0!=atexit (my_exit1)) OneErr_sys ("can ' t register my_exit1"); A if(0!=atexit (my_exit1)) -Err_sys ("can ' t register my_exit1"); -printf"Main is done\n"); the return 0; -}
Makefile
1CC =GCC2CFLAGS =-wall-o-g3Cxxflags =4INCLUDE =-I./comm/Inc5TARGET =atexit6 #search paths forerrorhandler.c, you can use spaces or: to split these paths when there are multiple paths 7Vpath%.c./Comm8#下行是为依赖项 Apue.h prepared, as in [Errorhandler.o:errorhandler.c apue.h] .9Vpath%.h./comm/IncTen OneOBJS =ERRORHANDLER.O ATEXIT.O Aall:$ (OBJS) -$ (CC) $ (CFLAGS) $ (INCLUDE)-O $ (TARGET) $^ -@echo---Target:[email protected] the@echo---depend:$^ - #下行的 apue.h, you don't have to write it. -errorhandler.o:errorhandler.c apue.h -$ (CC) $ (CFLAGS) $ (INCLUDE)-C $^ +@echo---Target:[email protected] -@echo---depend:$^ +atexit.o:atexit.c apue.h A$ (CC) $ (CFLAGS) $ (INCLUDE)-C $^ at@echo---Target:[email protected] -@echo---depend:$^ -Clean : -Rm-f *. O -Rm-f $ (TARGET)
In the Makefile.
INCLUDE =-I/comm/inc is used when compiling files for GCC
Vpath%.c./comm vpath%.h./comm.inc is used for make programs
@echo---target:[email protected] is to test what [email protected] is @echo---depend:$^ is to test what $^ is.
Automatic variables for Makefile
[Email protected] indicates the target file
$^ represents all dependent files
$< represents the first dependent file
$? Represents a new list of dependent files than the target
Makefile Instance Practice