天天看點

Makefile常用萬能模闆(包括靜态連結庫、動态連結庫、可執行檔案)

閱讀目錄

  • ​​1、生成可執行檔案的makefile​​
  • ​​2、生成靜态連結庫的makefile​​
  • ​​3、生成動态連結庫的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

######################################
#
#
######################################
  
#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)      

​​回到頂部​​

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)