Linux Makefile编写之可执行程序

2024-04-26 19:04

本文主要是介绍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,可以使用下面命令编译:

make -f BuildFile

本文使用make程序版本:

$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库测试代码为例,代码目录结构:

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.cpp4 directories, 27 files

3.1 Makefile

all:@make -C cpptest@make -C testclean:@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

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

编译结果:

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 定义工程名及路径

PROJECT_NAME ?= testPWD := $(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 定义编译器及选项

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 编译依赖项

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和可执行文件。

这篇关于Linux Makefile编写之可执行程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/938398

相关文章

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

Linux环境变量&&进程地址空间详解

《Linux环境变量&&进程地址空间详解》本文介绍了Linux环境变量、命令行参数、进程地址空间以及Linux内核进程调度队列的相关知识,环境变量是系统运行环境的参数,命令行参数用于传递给程序的参数,... 目录一、初步认识环境变量1.1常见的环境变量1.2环境变量的基本概念二、命令行参数2.1通过命令编程

Linux之进程状态&&进程优先级详解

《Linux之进程状态&&进程优先级详解》文章介绍了操作系统中进程的状态,包括运行状态、阻塞状态和挂起状态,并详细解释了Linux下进程的具体状态及其管理,此外,文章还讨论了进程的优先级、查看和修改进... 目录一、操作系统的进程状态1.1运行状态1.2阻塞状态1.3挂起二、linux下具体的状态三、进程的

Linux编译器--gcc/g++使用方式

《Linux编译器--gcc/g++使用方式》文章主要介绍了C/C++程序的编译过程,包括预编译、编译、汇编和链接四个阶段,并详细解释了每个阶段的作用和具体操作,同时,还介绍了调试和发布版本的概念... 目录一、预编译指令1.1预处理功能1.2指令1.3问题扩展二、编译(生成汇编)三、汇编(生成二进制机器语

Rsnapshot怎么用? 基于Rsync的强大Linux备份工具使用指南

《Rsnapshot怎么用?基于Rsync的强大Linux备份工具使用指南》Rsnapshot不仅可以备份本地文件,还能通过SSH备份远程文件,接下来详细介绍如何安装、配置和使用Rsnaps... Rsnapshot 是一款开源的文件系统快照工具。它结合了 Rsync 和 SSH 的能力,可以帮助你在 li

Linux部署jar包过程

《Linux部署jar包过程》文章介绍了在Linux系统上部署Java(jar)包时需要注意的几个关键点,包括统一JDK版本、添加打包插件、修改数据库密码以及正确执行jar包的方法... 目录linux部署jar包1.统一jdk版本2.打包插件依赖3.修改密码4.执行jar包总结Linux部署jar包部署