zoukankan      html  css  js  c++  java
  • 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)
    复制代码
  • 相关阅读:
    .net core webapi 前后端开发分离后的配置和部署
    403
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    一件能让人没脸见人的事
    过程 sp_addextendedproperty, 对象无效。不允许有扩展属性,或对象不存在。
    处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    阿里ECS配置MSSQL远程连接的坑
    .Net Webapi SignalR与微信小程序的交互
    Entity Framework 异常: 'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT 的用法无效。
    postgresql 表继承
  • 原文地址:https://www.cnblogs.com/any91/p/10412267.html
Copyright © 2011-2022 走看看