首页 > 其他分享 >Makefile knowledge summarization

Makefile knowledge summarization

时间:2023-10-20 23:35:17浏览次数:46  
标签:summarization SRC %. knowledge Makefile BUILD cpp main DIR

Wildcard

The wildcard in makefile is similar with macro in C/C++, it isn't similar with wildcard in linux shell, so it doesn't expend automatically.

object1 = *.c  // *.c

object2 = $(wildcard *.cpp) // main.cpp t1.cpp t2.cpp

Automatically generate dependencies

Utilizing the -M and -MM options to get the dependencies of source code from gcc/g++.

These options' document can be found at Preprocessor-Options. (About why these option in the preprocessor options, it is easy to understand. In the state of preprocessor, the preprocessor will judge the header file, so it knows which files are dependented by current file.)

Hide commands themself

Place @ front of the command, e.g. If you use echo "compiling..." in makefile, you will get the output

echo "compiling..."
compiling...

If you use @echo "compiling...", you will get the output

compiling...

Function

Automation variables

  • $@ : 表示规则中的目标文件集
  • $% : 仅当目标是函数库文件中,表示规则中的目标成员名
  • $< : 依赖目标中的第一个目标名字
  • $? : 所有比目标新的依赖目标的集合
  • $^ : 所有的依赖目标的集合
  • $+ : 这个变量很像 $^ ,也是所有依赖目标的集合。只是它不去除重复的依赖目标

Example

<details> <summary>Advanced makefile usage example code</summary>

# 编译工具信息
CXX = g++
CFLAGS = -std=c++11 -Wall

# 目录信息
SRC_DIR = src
BUILD_DIR = build

# 源文件列表
SRCS = $(wildcard $(SRC_DIR)/*.cpp)

# 生成目标文件列表(目标文件.o和源文件.cpp一一对应)
OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))

# 最终目标文件
TARGET = $(BUILD_DIR)/main

.PHONY: all clean

all: $(TARGET)

$(TARGET): $(OBJS)
	@mkdir -p $(dir $@) # $(dir $@) = $(BUILD_DIR)
	@$(CXX) $(CFLAGS) $^ -o $@

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
	@mkdir -p $(dir $@)
	@$(CXX) $(CFLAGS) -c $< -o $@

clean:
	rm -rf $(BUILD_DIR)

</details>

The above makefile can judge the source code which locates in src directory, and output the object file into the build directory. The following image shows the project file structure.

% tree
.
├── Makefile
└── src
    ├── funcs.cpp
    └── main.cpp

2 directories, 3 files

After compiling, we will get the following file structure.

% tree
.
├── Makefile
├── build
│   ├── funcs.o
│   ├── main
│   └── main.o
└── src
    ├── funcs.cpp
    └── main.cpp

3 directories, 6 files

There are some noteworthy points.

  1. $^ and $< Using the same file structure and editing the makefile, we can understand the difference between these two automation variables.
# 编译工具信息
CXX = g++
CFLAGS = -std=c++11 -Wall

# 目录信息
SRC_DIR = src
BUILD_DIR = build

# 源文件列表
SRCS = $(wildcard $(SRC_DIR)/*.cpp)

# 生成目标文件列表(目标文件.o和源文件.cpp一一对应)
OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))

# 最终目标文件
TARGET = $(BUILD_DIR)/main

.PHONY: all clean

all: $(TARGET)

$(TARGET): $(OBJS)
	@echo "\n"
	@echo $^

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
	@ echo $<

clean:
	rm -rf $(BUILD_DIR)

After we excute the make command, we will get the result.

src/funcs.cpp
src/main.cpp


build/funcs.o build/main.o

Based on the project file structure, we can understand the difference between them.

Pattern-specific Variable Values

The most obvious and easy example is distinguishing the difference between $< and $^ in Automation variables. i.e. the difference between $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp and $(OBJS): $(SRCS)

Although we know the $< is the name of the first prerequisite and $^ is the names of all the prerequisites, if we don't know the difference of the above expression of target: prerequisites, we can't understand the result of the g++ $(CFLAGS) -c $< -o $@.

In short, if we still use the above file structure, when using $(OBJS): $(SRCS), the $(OBJS) is the 'build/funcs.o build/main.o, they are one expression, but using$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp, the$(BUILD_DIR)/%.oisbuild/funcs.oandbuild/main.o`, they are two expressions, it looks like a enumation, so we can use this to generate the corresponding .o object file and .c source file.

Reference

标签:summarization,SRC,%.,knowledge,Makefile,BUILD,cpp,main,DIR
From: https://blog.51cto.com/u_14882565/7960169

相关文章

  • makefile中.PHNOY的用法
    makefile中PHONY的重要性     伪目标是这样一个目标:它不代表一个真正的文件名,在执行make时可以指定这个目标来执行所在规则定义的命令,有时也可以将一个伪目标称为标签。伪目标通过  PHONY来指明。   PHONY定义伪目标的命令一定会被执行,下面尝试分析这种优点的......
  • Makefile深入
    题目要求建立项目目录myutilxxxx(xxxx为学号后四位),子目录有:srcincludelibbin等源代码放入src,头文件放入include,生成的静态库,共享库放入lib,生成的中间文件,可执行文件放入bin编辑makefile放入mymath目录写出编译代码的makefile,编译出来的目标文件为testmyuti......
  • 论文阅读:Knowledge Distillation via the Target-aware Transformer
    摘要Knowledgedistillationbecomesadefactostandardtoimprovetheperformanceofsmallneuralnetworks.知识蒸馏成为提高小型神经网络性能的事实上的标准。Mostofthepreviousworksproposetoregresstherepresentationalfeaturesfromtheteachertothes......
  • MQTT控制报文格式 -- PUBACK(Publish Acknowledgement) Publish消息应答
    该消息是接收方收到QoS1的PUBLISH消息后,返回给发送方的应答消息。该消息由于没有Payload,固定包头的剩余长度值为21.固定包头FixedHeaderBit76543210byte1MQTTControlPackettype(4)Reserved 01000......
  • MQTT控制报文格式 -- CONNACK (Acknowledge connection request)连接请求应答
    该报文由服务端收到CONNECT数据包后发出,客户端可以根据在合理的时间内是否收到该报文而决定是否断开网络连接。该数据包不包含Payload部分,仅有FixedHeader和VariableHeader,现对其详述如下:1.固定包头FixedHeader固定包头共2个字节byte1=0x20byte2=0x02剩余长度共有2......
  • 用户态app Makefile 简易示例模板
    #Makefileforuser-spaceprogramexportPATH=/opt/toolchain/aarch64/bin/:$PATHCC:=aarch64-none-linux-gnu-gccDIR_PATH:=/home/user/sdk-v22.04/test_makefileOTHER_DUND_DIR:=$(DIR_PATH)/test_file_cOTHER_DUND_H:=$(DIR_PATH)/test_file_hCFLAGS:=-......
  • Makefile 入门教程
    Makefile是一个非常强大的构建自动化工具,用于管理项目的编译、链接和其他构建任务。以下是一个详细的Makefile使用文档,包括基本概念、语法、示例和常见任务。1.基本概念目标(Targets):在Makefile中,目标是要生成的文件或执行的操作的名称。目标可以是文件名,也可以是伪目标......
  • shell脚本执行make不一定调用Makefile
    今天在研究安卓编译流程时发现,shell脚本内执行make并不会调用当前路径下的Makefile,这有点让我大吃一惊。一番定位过后,发现执行make时调用的是另外一个shell脚本内定义的make函数,所以猜想可能GNUMake工具发现make有定义,所以直接调用该函数去了。于是我做了如下实验进行猜想验......
  • 论文阅读:A Lightweight Knowledge Graph Embedding Framework for Efficient Inferenc
    ABSTRACT现存的KGE方法无法适用于大规模的图(由于存储和推理效率的限制)作者提出了一种LightKG框架:自动的推断出码本codebooks和码字codewords,为每个实体生成合适的embedding。同时,框架中包含残差模块来实现码本的多样性,并且包含连续函数来近似的实现码字的选择。为更好的提升K......
  • 论文阅读:iterator zero-shot llm prompting for knowledge graph construction
    Abstract知识图谱,一种相互连接和可解释的结构。生成需要更多的人力、领域知识、并需要适用于不同的应用领域。本论文提出借助LLM,通过0-shot和外部知识不可知的情况下生成知识图谱。主要贡献:迭代的prompting提取最终图的相关部分0-shot,不需要examples一个可扩展的解决方案,......