当前位置:网站首页>makefile中进行宏定义并传递给源代码
makefile中进行宏定义并传递给源代码
2022-07-20 05:33:00 【倾我一生来读】
makefile是liunx 平台下c、c++开发必不可的工具,掌握一些makefile的技巧对于项目编译构建有很大的帮助,本篇文章主要记录一下makefile中进行宏定义并传递给源代码的使用技巧,主要传递了一个code_version变量,变量是通过git命令获取commit id,通过宏定义传递给源码,在源码中可以获取commit id,可以作为code的版本信息,这对于频繁release的项目很重要,可以确认一些so的版本是否匹配。
1 工程目录结构
建立的demo工程目录结构如下所示:
├── include
│ └── common.h
├── main_dir
│ ├── main.c
│ └── makefile
├── makefile
├── obj
│ ├── function.o
│ └── main.o
├── readme.txt
└── sub_dir
├── function.c
└── makefile
2 顶层目录makefile解读
顶层目录makefile:
CC = gcc
CFLAGS = -g -Wall -O
export TARGET = demo
export OBJ = function.o main.o
export BUILD_PATH = $(PWD)
export SUBDIR = main_dir sub_dir
export OBJDIR = $(BUILD_PATH)
export code_version = $(shell (git rev-parse --short HEAD))
export code_branch = $(shell (git branch))
export code_date= $(shell (git log -1 --format=%cd ))
$(info $(code_version))
$(info $(code_branch))
$(info $(code_date))
all:sub_target $(TARGET)
$(TARGET):$(OBJ)
$(CC) $^ -o $(OBJDIR)/[email protected]
@echo "-------build target success-------"
sub_target:
@echo "-------start build sub dir-------"
make -C main_dir
make -C sub_dir
@echo "-------build sub dir end-------"
clean:
rm -rf $(OBJDIR)/*.o $(TARGET)
顶层makefile目标是编译demo可以执行程序,编译demo需要依赖main_dir和sub_dir编译出的function.o 和main.o,并且编译function.o 和main.o时将其存放至obj文件夹,这样方便项目管理。顶层目录中还通过shell 命令定义了code_version变量,这样项目中其他makefile 就可以使用该变量了,这也是makefile之间传递变量的一种方式。
2 main_dir目录源码解读
main_dir 目录有一个main.c 和一个makefile文件,main.c 中内容入下:
#include <stdio.h>
#include "../include/common.h"
int main()
{
printf("code version: %s\n", CODE_VERSION);
printf("main IN\n");
int sum = add(5, 6);
printf("sum = %d \n",sum);
printf("main OUT\n");
return 0;
}
里面主要调用了一个add函数,add 函数的声明在include/common.h,实现在sub_dir/function.c,common.h中内容很简单,只做了add函数声明:
int add (int a, int b);
main函数中还打印了CODE_VERSION这个宏,这个宏在common.h和main.c中都没有定义,而是在编译的时候通过makefile定义的,main_dir 中makefile内容入下:
CFLAGS += -DCODE_VERSION=\"$(code_version)\"
$(OBJDIR)/main.o:main.c
$(CC) $(CFLAGS) -c $^ -o [email protected]
其中:
CFLAGS += -DCODE_VERSION=\"$(code_version)\"
就是定义了CODE_VERSION这个宏,并且宏的内容是顶层makefile中传递的变量,此外,makefile中定义字符串宏时需要使用转义字符。
3 sub_dir目录源码解读
sub_dir 目录包含了一个function.c文件和一个makefile,function.c中就是add函数实现:
#include <stdio.h>
#include "../include/common.h"
int add (int a, int b)
{
return a+b;
}
sub_dir 中makefile内容:
$(OBJDIR)/function.o:function.c
$(CC) $(CFLAGS) -c $^ -o [email protected]
4 效果验证
在顶层目录执行make命令进行编译:
make
编译结果如下:
运行程序:
./demo
结果如下:
可以看到CODE_VERSION有被正确印出来。
实验代码git 地址:https://github.com/zhenghaiyang123/makefile_demo.git
边栏推荐
- (6) Pytorch deep learning: logistic regression (multi-layer and multi-dimensional feature input)
- 热能动力工程毕业论文题目
- FPGA学习准备
- Understand the seperable revolution
- 辨析Iteration、epoch及batchsize之间的关系
- 2D目标检测综述之提议生成和特征表示篇(三)
- 交通安全管理毕业论文范文
- 从购买服务器到搭建个人博客网站 | 图文详细过程(腾讯云|宝塔面板|wordpress|Argon)
- Package (.Py) files into (.Exe) files in pychart
- 石油工程毕业论文范文
猜你喜欢
(7) Pytorch deep learning: full connection layer network
Chapter003-FPGA学习之PWM的LED呼吸灯
Paper study -- resource allocation and beamforming desing in the short blocklength region for urllc
(2) Pytorch deep learning: gradient descent
Understand wgan and spectral normalization
Li Hongyi machine learning 2020---p12 brief introduction of DL & p15 why DL
Understand the original Gan loss and hinge Gan loss
给定一个正整数N,将其表示为数字1,3,7,15相加的形式输出。请编码找出使上述数字出现的总次数最少(每个数字可以重复使用)的组合。
Tensorflow V1 introductory tutorial
【ROS機器人系統】自主導航+YOLO目標檢測+語音播報
随机推荐
STM32CubeMX的正交编码器encoder
Chapter006-FPGA学习之LCD显示
【CANN訓練營】基於昇思的GAN實現隨筆
基于STM32CubeMX的片外SPIFLASH嵌入FATFS
理解原始Gan Loss 和 Hinge Gan Loss
Hyperledger fabric super ledger CA construction and use
Neural networks: a review of 2D target detection
Data generator - supports multiple types
Affairs (segregated sector)
qlineargradient中坐标的含义
城市轨道交通工程毕业论文题目
PyQt5 使用自定义ToolTip解决QTableWidget数据显示不全问题
uni-app配置
(4) Pytorch deep learning: pyttorch realizes linear regression
Paper learning -- resource allocation in urllc with online learning for mobileusers
Play with the one-stop plan of cann target detection and recognition [introduction]
SQL subquery
[cann training camp] AI CPU operator development based on shengteng cann platform
HPDC Huawei partners and Developers Conference 2022 essay
vsCode配置Eslint+Prettier结合使用详细配置步骤,规范化开发