跳转至

HelloWorld的makefile解析


更新于 2021-09-24

简介

dpdk现在的例子可以用makemeson编译
meson不是很熟悉,就不多说了
简单说下helloworldmakefile:

  • 编译出的文件分链接动态库,跟链接静态库两个版本
  • 通过libdpdk.pc查找头文件和动态库路径 关于libxxx.pcpkg-config请参见其他文章

makefile解析

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# SPDX-License-Identifier: BSD-3-Clause
# 开源协议类型
# Copyright(c) 2010-2014 Intel Corporation
# 版权信息

# binary name
APP = helloworld
# 二进制可执行文件名

# all source are stored in SRCS-y
SRCS-y := main.c
# 需要编译的源文件

PKGCONF ?= pkg-config 
# 配置pkg-config
# 很多SDK都提供libxxx.pc的文件
# 里面包含了,该库的头文件和库文件(动态/静态)路径
# pkg-config解析pc文件,就知道需要包含的路径
# 不需要使用编译器额外参数指定头文件/库路径,例如gcc的-I -L

# Build using pkg-config variables if possible
ifneq ($(shell $(PKGCONF) --exists libdpdk && echo 0),0)
$(error "no installation of DPDK found")
endif
#查找libdpdk.pc文件,检测dpdk是否安装

all: shared
# 默认链接动态库
.PHONY: shared static
# 定义链接静态库,跟动态库,两个版本
shared: build/$(APP)-shared
    ln -sf $(APP)-shared build/$(APP)
static: build/$(APP)-static
    ln -sf $(APP)-static build/$(APP)

PC_FILE := $(shell $(PKGCONF) --path libdpdk 2>/dev/null)
#查找libpddk.pc文件
CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)
# 编译参数
LDFLAGS_SHARED = $(shell $(PKGCONF) --libs libdpdk)
# 链接使用的动态库
LDFLAGS_STATIC = $(shell $(PKGCONF) --static --libs libdpdk)
# 链接使用的静态库
ifeq ($(MAKECMDGOALS),static)
# check for broken pkg-config
ifeq ($(shell echo $(LDFLAGS_STATIC) | grep 'whole-archive.*l:lib.*no-whole-archive'),)
$(warning "pkg-config output list does not contain drivers between 'whole-archive'/'no-whole-archive' flags.")
$(error "Cannot generate statically-linked binaries with this version of pkg-config")
endif
endif

CFLAGS += -DALLOW_EXPERIMENTAL_API

build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build
    $(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_SHARED)
# 编译并链接动态库

build/$(APP)-static: $(SRCS-y) Makefile $(PC_FILE) | build
    $(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_STATIC)
# 编译并链接静态库

build:
    @mkdir -p $@

# 清空相关
.PHONY: clean
clean:
    rm -f build/$(APP) build/$(APP)-static build/$(APP)-shared
    test -d build && rmdir -p build || true