Makefile

最简单的Makefile模板

all: Hello
Hello:
    gcc -o Hello hello.c
clean:
    rm -rf Hello

 通用模板一

CC = gcc
LD = gcc
SUBDIRS = . ./src ./src/lua
SRCS = $(wildcard $(foreach S,$(SUBDIRS), $S/*.c))
OBJS = $(SRCS:.c = .o)

INCLUDES = $(foreach S,$(SUBDIRS),-I$S)
CCFAGS = -g -Wall -O0 $(INCLUDES)
LIBS = -lm -lasound -lpthread -lcurl -lmpg123
TAEGET = MasterController

.PHONY:all clean
all:$(TAEGET)
$(TARGET):$(OBJS) $(M_OBJS)
    $(LD) $(CCFLAGS) -o $@ $^ $(LIBS)

/%o:/%c
    $(CC) $(CCFLAGS) -c $^

clean:
    rm -rf *.o $(foreach D,$(SUBDIRS),$D*.o) $(TARGET)

 通用模板二

ifeq ($(VERBOSE),1)
ECHO :=
else
ECHO := @
endif

# Compilation flags
ifeq ($(DEBUG),1)
CXXFLAGS += -g
else
CXXFLAGS += -O2
endif

# Compiler
CXX := g++

# Target
TARGET := app
TARGET_DIR := .

# Directories
INC_DIRS := ../include
LIB_DIRS := ../lib64

# Files
INCS := $(wildcard )
SRCS := $(wildcard *.cpp)
LIBS := rt pthread

# Make it all!
all : $(TARGET_DIR)/$(TARGET)

# Host executable target.
$(TARGET_DIR)/$(TARGET) : Makefile $(SRCS) $(INCS) $(TARGET_DIR)
        $(ECHO)$(CXX) $(CPPFLAGS) $(CXXFLAGS) -fPIC $(foreach D,$(INC_DIRS),-I$D) \
                        $(foreach D,$(LIB_DIRS),-L$D) \
                        $(foreach L,$(LIBS),-l$L) \
                        -o $(TARGET_DIR)/$(TARGET)
$(TARGET_DIR) :
        $(ECHO)mkdir $(TARGET_DIR)
        
# Standard make targets
clean :
        $(ECHO)rm -f $(TARGET_DIR)/$(TARGET)
.PHONY : all clean

Makefile中用来调试和打印的方法:

1、使用$(warning )

$(warning "CCFLGAS: $(CCFLAGS)")

2、在最前面加@

ifeq ($(VERBOSE),1)
ECHO :=
else
ECHO := @
endif
$(TARGET):Makefile $(SRCS) $(INCS) $(TARGET_DIR)
    $(ECHO)$(CXX) $(CPPFLAGS)

 

你可能感兴趣的:(Linux)