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学习之Linux常用的几个文件夹的作用

bin(binaries)和sbin(super user binaries)文件夹为可执行文件夹,存放的都是二进制可执行文件,sbin文件夹只有root才能访问 opt平时使用的文件夹,和D盘的意思差不多  etc(etcetera)存放系统配置文件(环境变量) usr(unix shared resources)存放共享的系统资源 home存放用户文件的根目录 root超级用户目录

Linux 阿里云用apt 命令重新卸载安装mysql

正在设置 mysql-server-5.5 (5.5.27-0ubuntu2) ...start: Job failed to startinvoke-rc.d: initscript mysql, action "start" failed.dpkg: error processing mysql-server-5.5 (--configure): 子进程 已安装

linux container_of宏

1 结构体 假如我们有一个结构体Student,其定义如下: typedef struct student{int id;char name[30];int math;}Student; 通过Student stu定义stu变量后,我们可以通过stu.id或stu.name来获取stu的成员。但如果想反过来,通过stu.id或者stu.name来获取stu变量的起始地址好像就没那么简单了

Ansible——playbook编写

一、简介 1.什么是playbook  Ansible Playbook 是设定自动化任务的一种蓝图,可在无需人工干预或有限干预的前提下执行复杂的 IT 操作。Ansible Playbook 对一组或一类共同构成 Ansible 清单的主机执行。         Ansible Playbook 本质上是一些框架,是一些预先编写的代码,开发人员可以用作临时模板或起始模板。Ansible P

Alibaba Cloud Linux 安装mysql及注意事项

1.安装mysql #1.运行以下命令,更新YUM源。sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm#2.(可选)当操作系统为Alibaba Cloud Linux 3时,执行如下命令,安装MySQL所需的库文件。sudo rpm -Uvh https://mirrors.a

linux C-kermit 安装使用

linux C-kermit 安装使用 系统环境 Ubuntu 14.04.5 LTS 安装 sudo apt-get install ckermit 修改配置文件 ~/.kermrc: set line /dev/ttyUSB0 set speed 115200 set carrier-watch off set handshake none set flow-cont

linux环境下同时使用静态库、动态库编译程序

1. 应用场景 因某些原因,需要同时使用静态库及动态库编译代码。 在这里我需要静态链接的是zbarlib,动态链接的是opencv库。 经过一个下午的艰苦奋斗,分享一下网上的解决方法以及最终导致不成功的原因所在。 2.Makfile实例 CXX=g++CFLAGS += -I${PWD}/../zbar/includeCFLAGS += -I${PWD}/../opencv/inc

如何让linux内核make uImage时Entry Point(入口地址)自动偏移64个字节

转自:http://blog.csdn.net/hunhunzi/article/details/6262244 这两天在移植u-boot-1.3.4到s3c2440,期间还遇到不少事,首先是u-boot的mach_type和kernel的mach_type不一致导致内核正常解压完后,终端就不动了,停在了那里,改了mach_type之后内核可以正常拷贝和解压了。 现在又遇到ma

从零开始认识Linux只需要这些笔记就够了(10)

1.FTP: file transfor protocal     文件传输协议,提供上传下载服务 2.FTP连接     控制连接:TCP 21,用于发送FTP命令信息     数据连接:TCP 20,用于上传、下载数据 3.数据连接的建立类型  主动模式:服务端从 20 端口主动向客户端发起连接  被动模式:服务端在指定范围内某个端口被动等待客户端连接 4.FTP传输模式 文本模式:AS

Linux进程——Linux进程间切换与命令行参数

前言:在上一篇了解完进程状态后,我们简单了解了进程优先级,然后遗留了一点内容,本篇我们就来研究进程间的切换,来理解上篇提到的并发。如果对进程优先级还有没理解的地方可以先阅读: Linux进程优先级 本篇主要内容: 进程间的切换Linux进程调度队列命令行参数了解环境变量 目录 1. 进程间的切换2. Linux进程调度队列2.1 活跃队列2.2 过期队列 3.