mysql makefile例子_Makefile实例

A stupid man think himself is clever. A clever think himself is stupid.

Make基础

先上脚本:COMPILE = g++

FLAGS = -std=c++11 -g

target = epollserver

objs = client.o clientmanager.o main.o \

savetomysql.o threadable.o

libs = -L -lpthread \

-L/usr/lib/x86_64-linux-gnu  -lmysqlclient \

-L/home/ssx/Boost/boost_1_60_0/to/installation/prefix/lib -lboost_system \

-L/home/ssx/Boost/boost_1_60_0/to/installation/prefix/lib -lboost_thread \

minclude = -I.

$(target) : $(objs)

g++ -o  $(target) $(objs) $(libs)

client.o : client.cpp

g++ $(FLAGS) -c $

clientmanager.o : clientmanager.cpp

g++ $(FLAGS) -c $

savetomysql.o : savetomysql.cpp

g++ $(FLAGS) -c $

threadable.o : threadable.cpp

g++ $(FLAGS) -c $

main.o : main.cpp

g++ $(FLAGS) -c $

.PHONY : clean

clean :

rm $(target) $(objs)

Makefile的原理很简单,最终目标由几个阶段目标连接而成,而各个目标(.o文件) 又是由源文件编译出来的。

在shell中

编译的基本命令是 gcc -c  xx.c / g++ -c xx.cpp

链接的基本命令是 gcc -o xx.o x1.o x2.o

具体还要看你的编译选项,包含文件,包含库,总之就是个shell命令了。

Makefile就是目标+命令的组合,且看这2行:

client.o : client.cpp

g++ $(FLAGS) -c $

client.o 是目标 原料是client.cpp 命令是g++ $(FLAGS) -c $

就这么简单。

另外这里定义了一些宏(不是变量!),看起来比较方便,改起来不费劲。

最后的.PHONY : clean ... 是不需要原料,只有命令的目标---伪目标, make clean用到

2. Make进阶# which complier

CC = g++

#where are include file kept

INCLUDE = .

# Options for development

CFLAGS = -std=c++11  -shared -fPIC -Wall

#all the cpp files

SRCS := $(wildcard *.cpp)

#all the objs

OBJS := $(patsubst %cpp,%o,$(SRCS))

# All the Objects

OBJECTS = public_header.o threadable.o threadsmgr.o

#where are the libs kept

libs = -L -lpthread \

-L/home/ssx/Boost/boost_1_60_0/to/installation/prefix/lib -Wl,-rpath=/home/ssx/Boost/boost_1_60_0/to/installation/prefix/lib  -lboost_system \

-L/home/ssx/Boost/boost_1_60_0/to/installation/prefix/lib -Wl,-rpath=/home/ssx/Boost/boost_1_60_0/to/installation/prefix/lib  -lboost_thread \

-lglog

#target

target = libsuper_thread.so

#牛逼的递推

%.o:%.cpp

$(CC)    $(CFLAGS)  -c $

$(target):$(OBJECTS)

$(CC) $(CFLAGS) -o $@ $^  -I. $(libs)

clean :

rm -f $(OBJECTS) $(target)

这个和上一个版本相比更加复杂,但是更加智能化了。

#牛逼的递推

%.o:%.cpp

$(CC)    $(CFLAGS)  -c $

文件夹里面,每个cpp都会被编译成一个.o  根据这个思路,有了递推写法。

$(target):$(OBJECTS)

$(CC) $(CFLAGS) -o $@ $^  -I. $(libs)

当然objects也可以递推得出:

#all the cpp files

SRCS := $(wildcard *.cpp)

#all the objs

OBJS := $(patsubst %cpp,%o,$(SRCS))

但不推荐,因为有的.o未必需要被连接上

另外的要点

-L -l 与 -Wl -rpath= 这两组命令都跟依赖库有关,但是-L -l只告诉了编译时候去哪里读库(.so文件), 而-Wl -rpath= 告诉程序运行时去×××.so

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值