Linux Makefile编写之可执行程序

1 概述

编译工具有很多(make/cmake/BJam)。如果不考虑跨平台的话,还是make比较方便。使用make编译需要编写Makefile。本文编写Makefile来生成C/C++可执行程序。

2 Makefile文件命名

Makefile文件首先是一个文本文件,Linux下默认有两种命名方式:

  • Makefile 这是最常用的命名方式
  • makefile 这是优先级高的命名方式

在工程目录下运行make命令,make程序先找makefile,如果没有makefile再找Makefile文件。也就是说如果makefile和Makefile两个文件都存在默认使用makefile。

其实Makefile的文件名可以是任意的,例如Buildfile,可以使用下面命令编译:

bash 复制代码
make -f BuildFile

本文使用make程序版本:

bash 复制代码
$make --version
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

3 MakeFile实例

这里以CppTest库测试代码为例,代码目录结构:

bash 复制代码
cpptest2.0.0$ tree
.
├── Makefile
├── cpptest
│   ├── Makefile
│   ├── inc
│   │   ├── cpptest-assert.h
│   │   ├── cpptest-collectoroutput.h
│   │   ├── cpptest-compileroutput.h
│   │   ├── cpptest-htmloutput.h
│   │   ├── cpptest-output.h
│   │   ├── cpptest-source.h
│   │   ├── cpptest-suite.h
│   │   ├── cpptest-textoutput.h
│   │   ├── cpptest-time.h
│   │   └── cpptest.h
│   └── src
│       ├── collectoroutput.cpp
│       ├── compileroutput.cpp
│       ├── config.h
│       ├── htmloutput.cpp
│       ├── missing.cpp
│       ├── missing.h
│       ├── source.cpp
│       ├── suite.cpp
│       ├── textoutput.cpp
│       ├── time.cpp
│       ├── utils.cpp
│       ├── utils.h
│       └── winconfig.h
└── test
    ├── Makefile
    └── mytest.cpp

4 directories, 27 files

3.1 Makefile

makefile 复制代码
all:
	@make -C cpptest
	@make -C test

clean:
	@make -C cpptest clean
	@make -C test clean

.PHNOY: all clean

说明:

  • 根目录Makefile调用cpptest和test目录下Makefile进行编译。

3.2 cpptest/Makefile

参考上一篇文章Linux Makefile编写之静态库

3.3 test/Makefile

makefile 复制代码
PROJECT_NAME ?= test
PWD := $(shell pwd)
TOP := $(PWD)/..
INCS :=  -I$(TOP)/cpptest/inc
SRCDIR := $(PWD)
BINDIR := $(TOP)/bin
LIBS := $(TOP)/cpptest/lib/libcpptest.a
APPNAME := $(BINDIR)/$(PROJECT_NAME)

CC ?= gcc
CXX ?= g++

CFLAGS := 
C++FLAGS := -std=c++11
LINKFLAGS := 

CSRC := $(wildcard $(SRCDIR)/*.c)
OBJS := $(patsubst %.c,%.o,$(CSRC))

CPPS := $(wildcard $(SRCDIR)/*.cpp)
CPPOBJS := $(patsubst %.cpp,%.o,$(CPPS))

all: $(OBJS) $(CPPOBJS) $(BINDIR)
	$(CXX) $(OBJS) $(CPPOBJS) $(LIBS) $(LINKFLAGS) -o $(APPNAME)

$(OBJS): %.o:%.c
	$(CC) -c $(CFLAGS) $(INCS) $< -o $@

$(CPPOBJS): %.o:%.cpp
	$(CXX) -c $(C++FLAGS) $(INCS) $< -o $@

$(BINDIR):
	@mkdir -p $(BINDIR)

.PHNOY:clean
clean:
	@rm -f $(OBJS) $(CPPOBJS)
	@rm -f $(APPNAME)

3.4

编译结果:

bash 复制代码
make[1]: Entering directory '/home/james/git/cpptest2.0.0/cpptest'
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/utils.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/utils.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/source.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/source.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/time.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/time.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/collectoroutput.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/collectoroutput.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/textoutput.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/textoutput.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/compileroutput.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/compileroutput.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/htmloutput.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/htmloutput.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/suite.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/suite.o
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/cpptest/inc /home/james/git/cpptest2.0.0/cpptest/src/missing.cpp -o /home/james/git/cpptest2.0.0/cpptest/src/missing.o
ar -rcD /home/james/git/cpptest2.0.0/cpptest/lib/libcpptest.a  /home/james/git/cpptest2.0.0/cpptest/src/utils.o /home/james/git/cpptest2.0.0/cpptest/src/source.o /home/james/git/cpptest2.0.0/cpptest/src/time.o /home/james/git/cpptest2.0.0/cpptest/src/collectoroutput.o /home/james/git/cpptest2.0.0/cpptest/src/textoutput.o /home/james/git/cpptest2.0.0/cpptest/src/compileroutput.o /home/james/git/cpptest2.0.0/cpptest/src/htmloutput.o /home/james/git/cpptest2.0.0/cpptest/src/suite.o /home/james/git/cpptest2.0.0/cpptest/src/missing.o
make[1]: Leaving directory '/home/james/git/cpptest2.0.0/cpptest'
make[1]: Entering directory '/home/james/git/cpptest2.0.0/test'
g++ -c -std=c++11 -I/home/james/git/cpptest2.0.0/test/../cpptest/inc /home/james/git/cpptest2.0.0/test/mytest.cpp -o /home/james/git/cpptest2.0.0/test/mytest.o
g++  /home/james/git/cpptest2.0.0/test/mytest.o /home/james/git/cpptest2.0.0/test/../cpptest/lib/libcpptest.a  -o /home/james/git/cpptest2.0.0/test/../bin/test
make[1]: Leaving directory '/home/james/git/cpptest2.0.0/test'

说明:

  • 编译生成静态库libcpptes.a文件放在cpptest/lib目录下
  • 编译生成可执行程序放bin目录下
  • 编译生成.o与源码在同一目录

4 代码分析

4.1 定义工程名及路径

makefile 复制代码
PROJECT_NAME ?= test

PWD := $(shell pwd)
TOP := $(PWD)/..
INCS :=  -I$(TOP)/cpptest/inc
SRCDIR := $(PWD)
BINDIR := $(TOP)/bin
LIBS := $(TOP)/cpptest/lib/libcpptest.a
APPNAME := $(BINDIR)/$(PROJECT_NAME)

说明:

  • 定义工程名
  • 调用shell命令pwd获取当前路径PWD
  • 利用PWD定义include/src/bin路径
  • 定义依赖库名称
  • 定义可执行程序名称

4.2 定义编译器及选项

bash 复制代码
CC ?= gcc
CXX ?= g++

CFLAGS := 
C++FLAGS := -std=c++11
LINKFLAGS := 

说明:

  • 定义C/C++编译器名称
  • 定义C/C++编译选项,C++使用C++11标准。
  • 定义链接选项LINKFLAGS

4.3 自动选择译源文件

复制代码
CSRC := $(wildcard $(SRCDIR)/*.c)
OBJS := $(patsubst %.c,%.o,$(CSRC))

CPPS := $(wildcard $(SRCDIR)/*.cpp)
CPPOBJS := $(patsubst %.cpp,%.o,$(CPPS))

说明:

  • 调用函数wildcard扫描src下所有.c/.cpp文件
  • 调用函数patsubst通过源文件生成.o目标文件

4.4 编译依赖项

makefile 复制代码
all: $(OBJS) $(CPPOBJS) $(BINDIR)
	$(CXX) $(OBJS) $(CPPOBJS) $(LIBS) $(LINKFLAGS) -o $(APPNAME)

$(OBJS): %.o:%.c
	$(CC) -c $(CFLAGS) $(INCS) $< -o $@

$(CPPOBJS): %.o:%.cpp
	$(CXX) -c $(C++FLAGS) $(INCS) $< -o $@

$(BINDIR):
	@mkdir -p $(BINDIR)

.PHNOY:clean
clean:
	@rm -f $(OBJS) $(CPPOBJS)
	@rm -f $(APPNAME)

说明:

  • $(OBJS)依赖项编译.c文件为.o文件
  • $(CPPOBJS)依赖项编译.cpp文件为.o文件
  • $(BINDIR)依赖项创建目录bin
  • all依赖项将.o文件和.a文件链接成可执行文件。
  • clean依赖项删除编译生成.o和可执行文件。
相关推荐
爱凤的小光40 分钟前
Linux清理磁盘技巧---个人笔记
linux·运维
耗同学一米八1 小时前
2026年河北省职业院校技能大赛中职组“网络建设与运维”赛项答案解析 1.系统安装
linux·服务器·centos
知星小度S2 小时前
系统核心解析:深入文件系统底层机制——Ext系列探秘:从磁盘结构到挂载链接的全链路解析
linux
2401_890443022 小时前
Linux 基础IO
linux·c语言
智慧地球(AI·Earth)3 小时前
在Linux上使用Claude Code 并使用本地VS Code SSH远程访问的完整指南
linux·ssh·ai编程
老王熬夜敲代码4 小时前
解决IP不够用的问题
linux·网络·笔记
zly35004 小时前
linux查看正在运行的nginx的当前工作目录(webroot)
linux·运维·nginx
QT 小鲜肉5 小时前
【Linux命令大全】001.文件管理之file命令(实操篇)
linux·运维·前端·网络·chrome·笔记
问道飞鱼5 小时前
【Linux知识】Linux 虚拟机磁盘扩缩容操作指南(按文件系统分类)
linux·运维·服务器·磁盘扩缩容
egoist20236 小时前
【Linux仓库】超越命令行用户:手写C语言Shell解释器,解密Bash背后的进程创建(附源码)
linux·c语言·bash·xshell·环境变量·命令行参数·内建命令