makefile template

file structure

.
├── bin
│   └── test.out
├── include
│   └── reply.h
├── lib
│   ├── libreply.a
│   ├── libreply.so
│   ├── makefile_a
│   ├── makefile_so
│   └── reply.cpp
├── makefile

└── test.cpp

Note: makefile_a or makefile_so should be renamed to makefile as needed when compiling the library

   

1. Overall project makefile

SRCS    = $(wildcard *.cpp)
OBJS = $ (patsubst% .cpp,% .o, $ (SRCS))

CC      = g++
CFLAGS  = -g -Wall -O0

TARGET      = ./bin/test.out
INC_PATH    = -I$(TOP_DIR)./include/
LIB_PATH    = -L$(TOP_DIR)./lib/

EXT_LIB = #./lib/libreply.a #If you need to modify the parameters when using a static library
EXT_SO = -L. -lreply -Wl,--rpath=./lib/ #If you need to modify the parameters when using the dynamic library

all:$(TARGET)

$(TARGET):$(OBJS)
	$(CC) $(LIB_PATH) $(CFLAGS) -o $@ $^ $(EXT_LIB) $(EXT_SO)

% .o:% .cpp
	$(CC) $(INC_PATH) $(CFLAGS) -c -o $@ $<
%.o:%.c
	$(CC) $(INC_PATH) $(CFLAGS) -c -o $@ $<

run:$(TARGET)
	$(TARGET)

clear:
	rm -rf $(OBJS)

.PHONY:clean
clean:
	rm -rf $(TARGET) $(OBJS)

2. Static library makefile_a

SRCS    = $(wildcard *.cpp)
OBJS = $ (patsubst% .cpp,% .o, $ (SRCS))

CC      = g++
CFLAGS  = -g -Wall -O0

TARGET      = libreply.a
INC_PATH    = -I$(TOP_DIR)..
LIB_PATH    = -L$(TOP_DIR)..

EXT_LIB     = #../lib/libreply.a
EXT_SO		= #-L. -lreply -Wl,--rpath=./lib/
EXT_SO_CC	= #-fPIC

all:$(TARGET)

$(TARGET):$(OBJS)
	ar crv $@ $^
#$(CC) $(LIB_PATH) $(CFLAGS) -o $@ $^ $(EXT_LIB) $(EXT_SO)

% .o:% .cpp
	$(CC) $(INC_PATH) $(CFLAGS) -c -o $@ $<
%.o:%.c
	$(CC) $(INC_PATH) $(CFLAGS) -c -o $@ $<

run:$(TARGET)
	$(TARGET)

clear: #Command to delete only intermediate files
	rm -rf $(OBJS)
	
.PHONY:clean
clean:
	rm -rf $(TARGET) $(OBJS)

3. Dynamic library makefile_so

SRCS    = $(wildcard *.cpp)
OBJS = $ (patsubst% .cpp,% .o, $ (SRCS))

CC      = g++
CFLAGS  = -g -Wall -O0 $(EXT_SO_CC)

TARGET      = libreply.so
INC_PATH    = -I$(TOP_DIR)..
LIB_PATH    = -L$(TOP_DIR)..

EXT_LIB     = #../lib/libreply.a
EXT_SO		= -L. -lreply -Wl,--rpath=./lib/
EXT_SO_CC = -fPIC #Additional parameters are required when compiling dynamic libraries

all:$(TARGET)

$(TARGET):$(OBJS)
	$(CC) -shared -fPIC -o $(TARGET) $(OBJS)  

% .o:% .cpp
	$(CC) $(INC_PATH) $(CFLAGS) -c -o $@ $<
%.o:%.c
	$(CC) $(INC_PATH) $(CFLAGS) -c -o $@ $<

run:$(TARGET)
	$(TARGET)

clear: #Command to delete only intermediate files
	rm -rf $(OBJS)
	
.PHONY:clean
clean:
	rm -rf $(TARGET) $(OBJS)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325603001&siteId=291194637