[英]Can't compile sample bpf program, bpf/bpf.h is missing
I'm trying to compile the sample bpf program in Linux source code.我正在尝试在 Linux 源代码中编译示例 bpf 程序。 So I downloaded the current kernel source code and entered
samples/bpf
folder所以我下载了当前内核源代码并进入了
samples/bpf
文件夹
apt source linux
cd linux-*/samples/bpf
Then I tried to compile a sample program with gcc:然后我尝试用 gcc 编译一个示例程序:
# gcc sock_example.c
sock_example.c:29:10: fatal error: bpf/bpf.h: No such file or directory
29 | #include <bpf/bpf.h>
| ^~~~~~~~~~~
compilation terminated.
And I'm unable to find bpf/bpf.h
with apt-file
而且我无法使用
apt-file
找到bpf/bpf.h
# apt-file find bpf/bpf.h
(no output)
What was wrong?哪里错了?
The Linux eBPF samples come with a rather long Makefile. Linux eBPF 示例带有一个相当长的 Makefile。 It handles a lot of cases and integrates well with the kernel building workflow, but makes it more complicated to build the samples outside of the kernel tree.
它处理了很多情况并与内核构建工作流很好地集成,但是在内核树之外构建样本变得更加复杂。
For example, this Makefile adds a number of includes directories, such as TPROGS_CFLAGS += -I$(srctree)/tools/lib/
, meaning that bpf/bpf.h
will not be fetched from your OS' libraries but from tools/lib/
under your kernel repository.例如,此 Makefile 添加了许多包含目录,例如
TPROGS_CFLAGS += -I$(srctree)/tools/lib/
,这意味着bpf/bpf.h
不会从您的操作系统的库中提取,而是从tools/lib/
在您的内核存储库下。
Here are a few options that you can consider to compile your program:您可以考虑以下几个选项来编译您的程序:
tprogs-y
target.tprogs-y
目标。sudo make -C tools/lib/bpf install
) or from its GitHub mirror .sudo make -C tools/lib/bpf install
)或其GitHub 镜像安装 libbpf。 This should install the headers, and gcc should then be able to find them.gcc -I <path/to/linux>/tools/lib
).gcc -I <path/to/linux>/tools/lib
)。#include <bpf/bpf.h>
to make it point to your library.) #include <bpf/bpf.h>
的包含路径,使其指向您的库。)如果您安装libbpf-dev
软件包,它会提供<bpf/bpf.h>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.