makefile的学习

一个源文件:

#因为有多线程,所以后面添加了-lpthread
Test:Test.c
    gcc -g Test.c -o Test -lpthread

多个文件:

all:Server Client
Server:Server.cpp
    g++ -g Server.cpp -o Server
Client:Client.cpp
    g++ -g Client.cpp -o Client

一个最基本的makefile,包括3个头文件,4个源文件

PosServer:main.o PosServer.o DataBase.o public_function.o
    g++ -o PosServer  main.o PosServer.o DataBase.o public_function.o -lpthread -L/usr/lib/x86_64-linux-gnu -lmysqlclient
main.o:main.cpp PosServer.h DataBase.cpp DataBase.h public_function.h
    g++ -c main.cpp
PosServer.o:PosServer.cpp PosServer.h
    g++ -c PosServer.cpp
DataBase.o:DataBase.cpp DataBase.h
    g++ -c DataBase.cpp
public_function.o:public_function.cpp public_function.h
    g++ -c public_function.cpp
clean:
    rm PosServer main.o PosServer.o DataBase.o public_function.o

优化后的makefile

#声明一个变量objects
objects = main.o PosServer.o DataBase.o public_function.o
PosServer : $(objects)
    g++ -o PosServer  $(objects) -lpthread -L/usr/lib/x86_64-linux-gnu -lmysqlclient
main.o:PosServer.h DataBase.cpp DataBase.h public_function.h
PosServer.o : PosServer.h
DataBase.o : DataBase.h
public_function.o : public_function.h

#.PHONY表示clean是个伪目标文件
.PHONY : clean
clean : 
    -rm PosServer $(objects)

再简单点:

EXE = PosServer         #可执行文件名

all : $(EXE)

$(EXE) : main.cpp PosServer.h DataBase.h public_function.h
    g++ -g -o $(EXE) main.cpp PosServer.cpp DataBase.cpp public_function.cpp -lpthread -L/usr/lib/x86_64-linux-gnu -lmysqlclient

#.PHONY表示clean是个伪目标文件
.PHONY : clean
clean : 
    -rm -f $(EXE)

你可能感兴趣的:(gcc)