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进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE