简体   繁体   English

无法编译示例 bpf 程序,缺少 bpf/bpf.h

[英]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?哪里错了?

You need to install libbpf :您需要安装libbpf

git clone --depth 1 https://github.com/libbpf/libbpf
cd src
sudo make install

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:您可以考虑以下几个选项来编译您的程序:

  • Adjust the existing Makefile to use it to build your own programs, for example by adding your kernel code to the tprogs-y target.调整现有的 Makefile 以使用它来构建您自己的程序,例如通过将您的内核代码添加到tprogs-y目标。
  • Install libbpf from the kernel repo ( 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 应该能够找到它们。
  • Adjust your compile option to tell gcc where to look for the header ( gcc -I <path/to/linux>/tools/lib ).调整编译选项以告诉 gcc 在哪里查找头文件 ( gcc -I <path/to/linux>/tools/lib )。
  • (Adjust the include path in #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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM