其他分享
首页 > 其他分享> > 【makefile】01 经典应用示例

【makefile】01 经典应用示例

作者:互联网

经典应用示例

1. 文件结构

.
├── include
│   ├── module1
│   │   ├── mod1c1.hpp
│   │   └── mod1c2.hpp
│   ├── module2
│   │   ├── mod2c1.hpp
│   │   └── mod2c2.hpp
│   └── program.hpp
├── Makefile
└── src
    ├── module1
    │   ├── mod1c1.cpp
    │   └── mod1c2.cpp
    ├── module2
    │   ├── mod2c1.cpp
    │   └── mod2c2.cpp
    └── program.cpp

 

makefile文件:

#
# **************************************************************
# *                Simple C++ Makefile Template                *
# *                                                            *
# * Author: Arash Partow (2003)                                *
# * URL: http://www.partow.net/programming/makefile/index.html *
# *                                                            *
# * Copyright notice:                                          *
# * Free use of this C++ Makefile template is permitted under  *
# * the guidelines and in accordance with the the MIT License  *
# * http://www.opensource.org/licenses/MIT                     *
# *                                                            *
# **************************************************************
#

CXX      := -c++
CXXFLAGS := -pedantic-errors -Wall -Wextra -Werror
LDFLAGS  := -L/usr/lib -lstdc++ -lm
BUILD    := ./build
OBJ_DIR  := $(BUILD)/objects
APP_DIR  := $(BUILD)/apps
TARGET   := program
INCLUDE  := -Iinclude/
SRC      :=                      \
   $(wildcard src/module1/*.cpp) \
   $(wildcard src/module2/*.cpp) \
   $(wildcard src/*.cpp)         \

OBJECTS  := $(SRC:%.cpp=$(OBJ_DIR)/%.o)
DEPENDENCIES \
         := $(OBJECTS:.o=.d)

all: build $(APP_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %.cpp
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -MMD -o $@

$(APP_DIR)/$(TARGET): $(OBJECTS)
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)

-include $(DEPENDENCIES)

.PHONY: all build clean debug release info

build:
    @mkdir -p $(APP_DIR)
    @mkdir -p $(OBJ_DIR)

debug: CXXFLAGS += -DDEBUG -g
debug: all

release: CXXFLAGS += -O2
release: all

clean:
    -@rm -rvf $(OBJ_DIR)/*
    -@rm -rvf $(APP_DIR)/*

info:
    @echo "[*] Application dir: ${APP_DIR}     "
    @echo "[*] Object dir:      ${OBJ_DIR}     "
    @echo "[*] Sources:         ${SRC}         "
    @echo "[*] Objects:         ${OBJECTS}     "
    @echo "[*] Dependencies:    ${DEPENDENCIES}"

 

3. 运行结果:

 

 

 

参考资料:

1. Simple C++ Makefile Example

2. makefile教程【C语言中文网】

标签:01,OBJ,示例,APP,makefile,echo,CXXFLAGS,cpp,DIR
来源: https://www.cnblogs.com/sunbines/p/15435495.html