两个简单的makefile的实现,编译当前目录下所有的.c文件
2016-11-18 22:02
435 查看
在网上找到的一个非常好的makefile文件,可以编译本文件夹下的所有.c文件,之需要定义输出文件名,不需要指定main文件的文件名,非常的好,非常的方便,注释也很详细,收藏并分享给需要的人。
#####################################################################
## file : test makefile for build current dir .c ##
## author : jernymy ##
## date-time : 05/06/2010 ##
#####################################################################
CC = gcc
CPP = g++
RM = rm -rf
## debug flag
DBG_ENABLE = 1
## source file path
SRC_PATH := .
## target exec file name
TARGET := test
## get all source files
SRCS += $(wildcard $(SRC_PATH)/*.c)
## all .o based on all .c
OBJS := $(SRCS:.c=.o)
## need libs, add at here
LIBS := pthread
## used headers file path
INCLUDE_PATH := .
## used include librarys file path
LIBRARY_PATH := /lib
## debug for debug info, when use gdb to debug
ifeq (1, ${DBG_ENABLE})
CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
endif
## get all include path
CFLAGS += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
## get all library path
LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))
## get all librarys
LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))
all: clean build
build:
$(CC) -c $(CFLAGS) $(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)
$(RM) $(OBJS)
clean:
$(RM) $(OBJS) $(TARGET)
注意有一些空行的开头是tab键,但是csdn自动改成了4个空格。需要注意。
这个也是我经常用的makefile,有汉语注释更容易理解。
#####################################################################
## file : test makefile for build current dir .c ##
## author : jernymy ##
## date-time : 05/06/2010 ##
#####################################################################
CC = gcc
CPP = g++
RM = rm -rf
## debug flag
DBG_ENABLE = 1
## source file path
SRC_PATH := .
## target exec file name
TARGET := test
## get all source files
SRCS += $(wildcard $(SRC_PATH)/*.c)
## all .o based on all .c
OBJS := $(SRCS:.c=.o)
## need libs, add at here
LIBS := pthread
## used headers file path
INCLUDE_PATH := .
## used include librarys file path
LIBRARY_PATH := /lib
## debug for debug info, when use gdb to debug
ifeq (1, ${DBG_ENABLE})
CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
endif
## get all include path
CFLAGS += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
## get all library path
LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))
## get all librarys
LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))
all: clean build
build:
$(CC) -c $(CFLAGS) $(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)
$(RM) $(OBJS)
clean:
$(RM) $(OBJS) $(TARGET)
注意有一些空行的开头是tab键,但是csdn自动改成了4个空格。需要注意。
# $Id: Makefile,v 2.0 2008/10/08 18:55:40 Update $ prog_name = testtcp #目标文件名 ###################################### # # Generic makefile # # by Jackie Xie # email: jackie.CPlusPlus@gmail.com # # Copyright (c) 2008 Jackie Xie # All rights reserved. # # No warranty, no liability; # you use this at your own risk. # # You are free to modify and # distribute this without giving # credit to the original author. # ###################################### ### Customising # # Adjust the following if necessary; EXECUTABLE is the target # executable"s filename, and LIBS is a list of libraries to link in # (e.g. alleg, stdcx, iostr, etc). You can override these on make"s # command line of course, if you prefer to do it that way. # EXECUTABLE := $(prog_name) LIBS := -lpthread #库文件 没有可不写 # Now alter any implicit rules" variables if you like, e.g.: # CFLAGS := -g -Wall -O CXXFLAGS := $(CFLAGS) CC := arm-linux-gcc #编译器 # The next bit checks to see whether rm is in your djgpp bin # directory; if not it uses del instead, but this can cause (harmless) # `File not found" error messages. If you are not using DOS at all, # set the variable to something which will unquestioningly remove # files. # ifneq ($(wildcard $(DJDIR)/bin/rm),) RM-F := rm -f else RM-F := del endif # You shouldn"t need to change anything below this point. # # ................... SOURCE := $(wildcard *.cpp) $(wildcard *.c) OBJS := $(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(SOURCE))) DEPS := $(patsubst %.o,%.d,$(OBJS)) MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS)) MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.cpp,$(MISSING_DEPS)) $(patsubst %.d,%.cpp,$(MISSING_DEPS))) CPPFLAGS += -MD .PHONY : everything deps objs clean veryclean rebuild everything : $(EXECUTABLE) deps : $(DEPS) objs : $(OBJS) clean : @$(RM-F) *.o @$(RM-F) *.d veryclean: clean @$(RM-F) $(EXECUTABLE) @$(RM-F) *.*~ *~ distclean: veryclean rebuild: veryclean everything ifneq ($(MISSING_DEPS),) $(MISSING_DEPS) : @$(RM-F) $(patsubst %.d,%.o,$@) endif -include $(DEPS) $(EXECUTABLE) : $(OBJS) $(CC) -o $(EXECUTABLE) $(OBJS) $(addprefix ,$(LIBS))
这个也是我经常用的makefile,有汉语注释更容易理解。
相关文章推荐
- 一个简单的makefile的实现,编译当前目录下所有的.c文件
- Makefile编译当前目录下所有c文件到共享库
- 多文件目录下makefile文件递归执行编译所有c文件
- 多文件目录下makefile文件递归执行编译所有c文件
- gcc/g++最简单编译当前目录下所有文件
- 多文件目录下makefile文件递归执行编译所有c文件
- Python实现的两个文件操作:当前目录下所有文件和转码
- 多文件目录下makefile文件递归执行编译所有c文件
- 【转】多文件目录下makefile文件递归执行编译所有c文件
- gcc/g++最简单编译当前目录下所有文件
- 多文件目录下makefile文件递归执行编译所有c文件(小例子1)
- makefile 自动编译同一个目录下的所有文件
- 仅在当前目录编译的通用Makefile
- makefile 自动编译同一个目录下的所有文件
- ubuntu 用shell脚本实现将当前目录下所有目录中的某一类文件拷贝到同一目录下
- 【Linux学习】 写一个简单的Makefile编译源码获取当前系统时间
- Makefile编译目录下多个文件(简单例子0)
- 一个将目录内所有c代码分别编译成独立的out文件的Makefile
- 【Linux学习】 写一个简单的Makefile编译源码获取当前系统时间
- makefile 自动编译同一个目录下的所有文件