我能看懂的MakeFile(自命名,多文件,多目标)

类图是没有了,今天老师上课上到了我就马上写一下,后面会补。

public = b_pthread_base.o c_pthread_mutex.o d_pthread_cond.o e_pthread_pool.o f_shm.o i_packet_base.o i_packet.o 
    
fother = a_epoll_base.o g_macro.o h_file.o j_epoll_tcp.o k_sendtcp.o l_heart_beat.o m_front.o

bother = n_db.o o_backserver.o

lother = g_macro.o h_shm.o p_log.o

sother = q_first_db.o

all : front back log sql
.PHNOY : all

front : $(object) $(fother)
	g++ -o front $(object) $(fother) -lpthread -lrt
    
back : $(object) $(bother)
	g++ -o back $(object) $(bother) -lpthread -lrt -lsqlite3
    
log : $(lother)
	g++ -o log $(lother) -lrt
    
sql : $(sother)
	g++ -o sql $(sother) -lsqlite3

d_pthread_cond.o : c_pthread_mutex.h
e_pthread_pool.o : d_pthread_cond.h
h_file.o : g_macro.h
i_packet.o : i_packet_base.h I_Packet_Public.h

k_sendtcp.o : b_pthread_base.h d_pthread_cond.h e_pthread_pool.h f_shm.h i_packet.h
j_epoll_tcp.o : a_epoll_base.h c_pthread_mutex.h e_pthread_pool.h g_macro.h h_file.h i_packet.h I_Packet_Public.h 
l_heart_beat.o : b_pthread_base.h j_epoll_tcp.h
m_front.o : h_file.h j_epoll_tcp.h k_sendtcp.h l_heart_beat.h

n_db.o : I_Packet_Public.h
o_backserver.o : b_pthread_base.h d_pthread_cond.h e_pthread_pool.h f_shm.h i_packet.h I_Packet_Public.h n_db.h
    
p_log.o : f_shm.h g_macro.h

clean : 
	rm -f $(object) $(bother) $(fother) $(sother) p_log.o

好,我解释一下。
我们的目标文件(就是拿来运行的那个)其实就是通过文件之间的依赖关系,对源文件进行编译而得到的,但是这个依赖关系系统不知道,所以需要我们告诉系统。Makefile就是一种很优秀的方法。

在all那一行上面的部分属于量赋值部分,右边的(*.o)文件是目标中间文件,后面需要将它实现。系统对源文件进行转换成中间文件,而后将这些中间文件合成可执行文件。

all : front back log sql
.PHNOY : all
这两行的意思是:.PHNOY:all将all定义为一个伪目标,不用管那么多,只要知道这样就可以生成多个目标了。all:front back log sql里面的front back log sql这四个都是目标,可以一次性生成。
当然,你不用伪目标的话应该是不行了。

front : $(object) $(fother)
	g++ -o front $(object) $(fother) -lpthread -lrt
    
back : $(object) $(bother)
	g++ -o back $(object) $(bother) -lpthread -lrt -lsqlite3
    
log : $(lother)
	g++ -o log $(lother) -lrt
    
sql : $(sother)
	g++ -o sql $(sother) -lsqlite3

这一串的意思就是对各个目标文件的依赖关系进行解释,$( )的意思是对括号中的值进行铺开,取出里面的东西。
g++ -o front $(object) $(fother) 这个是格式化写法了,当然还有别得写法。
-lpthread -lrt 这两个是动态库,调用动态库之前要加-l 是L,不是一,也不是大写的i。

然后后面到clean之前为止是对(*.o)文件的依赖关系解释。
其中有两个小技巧,
1、比方说你要生成a.o,那么你可以不用写a.h,Makefile会自动推导规则。如果你的a.cpp只需要一个a.h的头文件,那么这个依赖你可以省了。
2、本来每个依赖下面都需要一步编译步骤,不过也可以省略不写,所以我就不说是什么了,自动推导。

下面那个clean,也是一个伪目标。
rm -rf ***** 用于将多余文件删除,不过需要手动调用。
调用方式:
make -clean。

然后再讲一点,给Makefile命名。
如果你不想你的Makefile文件都叫Makefile,可以自己起个名字。
然后运行的时候这样:make -f 名字
清楚的时候这样: make -f 名字 -clean

上一篇:【解决】Linux编译make时速度慢,一直在checking


下一篇:shell 替换,匹配