Makefile常用萬能模板(包括靜態鏈接庫、動態鏈接庫、可執行文件)

閱讀目錄

 


  本文把makefile 分成了三份:生成可執行文件的makefile,生成靜態鏈接庫的makefile,生成動態鏈接庫的makefile。

  這些makefile都很簡單,一般都是一看就會用,用法也很容易,只需要把它們拷貝到你的代碼的同一目錄下,然後就可以用 make 來生成目標文件了。

  下面是三個makefile的源代碼:

回到頂部

1、生成可執行文件的makefile

複製代碼

######################################
#
######################################
#source file
#源文件,自動找所有.c和.cpp文件,並將目標定義爲同名.o文件
SOURCE  := $(wildcard *.c) $(wildcard *.cpp)
OBJS    := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
  
#target you can change test to what you want
#目標文件名,輸入任意你想要的執行文件名
TARGET  := test
  
#compile and lib parameter
#編譯參數
CC      := gcc
LIBS    :=
LDFLAGS :=
DEFINES :=
INCLUDE := -I.
CFLAGS  := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H
  
  
#i think you should do anything here
#下面的基本上不需要做任何改動了
.PHONY : everything objs clean veryclean rebuild
  
everything : $(TARGET)
  
all : $(TARGET)
  
objs : $(OBJS)
  
rebuild: veryclean everything
                
clean :
    rm -fr *.so
    rm -fr *.o
    
veryclean : clean
    rm -fr $(TARGET)
  
$(TARGET) : $(OBJS)
    $(CC) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

複製代碼

 

回到頂部

2、生成靜態鏈接庫的makefile

按 Ctrl+C 複製代碼

###################################### # # ###################################### #target you can change test to what you want #共享庫文件名,lib*.a TARGET := libtest.a #compile and lib parameter #編譯參數 CC := gcc AR = ar RANLIB = ranlib LIBS := LDFLAGS := DEFINES := INCLUDE := -I. CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE) CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H #i think you should do anything here #下面的基本上不需要做任何改動了 #source file #源文件,自動找所有.c和.cpp文件,並將目標定義爲同名.o文件 SOURCE := $(wildcard *.c) $(wildcard *.cpp) OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) .PHONY : everything objs clean veryclean rebuild everything : $(TARGET) all : $(TARGET) objs : $(OBJS) rebuild: veryclean everything clean : rm -fr *.o veryclean : clean rm -fr $(TARGET) $(TARGET) : $(OBJS) $(AR) cru $(TARGET) $(OBJS) $(RANLIB) $(TARGET)

按 Ctrl+C 複製代碼

 

回到頂部

3、生成動態鏈接庫的makefile

複製代碼

######################################
#
#
######################################
  
#target you can change test to what you want
#共享庫文件名,lib*.so
TARGET  := libtest.so
  
#compile and lib parameter
#編譯參數
CC      := gcc
LIBS    :=
LDFLAGS :=
DEFINES :=
INCLUDE := -I.
CFLAGS  := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H
SHARE   := -fPIC -shared -o
  
#i think you should do anything here
#下面的基本上不需要做任何改動了
  
#source file
#源文件,自動找所有.c和.cpp文件,並將目標定義爲同名.o文件
SOURCE  := $(wildcard *.c) $(wildcard *.cpp)
OBJS    := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
  
.PHONY : everything objs clean veryclean rebuild
  
everything : $(TARGET)
  
all : $(TARGET)
  
objs : $(OBJS)
  
rebuild: veryclean everything
                
clean :
    rm -fr *.o
    
veryclean : clean
    rm -fr $(TARGET)
  
$(TARGET) : $(OBJS)
    $(CC) $(CXXFLAGS) $(SHARE) $@ $(OBJS) $(LDFLAGS) $(LIBS)

複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章