c++ - Ebpf:助手的未定义符号
问题描述
我运行最新的 debian 测试(使用内核 4.19)。
在我的系统上找不到助手(但它们存在于标题中,Qt 跳转到它们)
#include "bpf/bpf.h"
int main (){
int r = bpf_create_map(BPF_MAP_TYPE_ARRAY,1,1,1,0);
return 0;
}
编译结果为
undefined reference to `bpf_create_map(bpf_map_type, int, int, int, unsigned int)'
编译
g++ -c -pipe -g -std=gnu++1z -Wall -W -fPIC -DQT_QML_DEBUG -I. -I../../Qt/5.13.0/gcc_64/mkspecs/linux-g++ -o main.o main.cpp
g++ -lbpf -o server main.o
结果相同
g++ main.cpp -lbpf -o out
我也安装了 libbpf-dev 并且我有相关的库(一个等等)。
怎么了?
更新
即使下面的代码也行不通
#include <linux/bpf.h>
int main (){
//int r = bpf_create_map(BPF_MAP_TYPE_ARRAY,1,1,1,0);
bpf_attr attr = {};
attr.map_type = BPF_MAP_TYPE_ARRAY;
attr.key_size = 1;
attr.value_size = 1;
attr.max_entries = 1;
bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
return 0;
}
结果是
error: 'bpf' was not declared in this scope
更新2:
顺便说一句,密钥大小被规定为 4 而不是 1;但除此之外,这与我的问题无关。
解决方案
由于在 C++ 中编译导致的命名空间问题,您可能需要:
extern "C" {
#include "bpf/bpf.h"
}
int main()...
关于您的第二个错误(error: 'bpf' was not declared in this scope
),这与 libbpf 没有直接关系,这是因为没有简单地调用函数bpf()
来实际执行系统调用。相反,您必须使用系统调用号。例如,libbpf 定义如下:
static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
...并sys_bpf()
在此之后使用,与您尝试调用bpf()
样本的方式相同。
作为记录,“BPF 助手”通常指定您从 BPF 程序中调用的 BPF 函数,这里不是这种情况。因此,我相信评论中有些混乱。
推荐阅读
- excel - 使用具有重复列的powershell循环遍历excel数据
- python - JupyterLab:您的浏览器不支持 WebGL
- batch-file - 当远程主机关闭连接时,有没有办法在 Windows ssh 中跳过“按任意键继续”?
- spring - 使用 Spring Data Reactive Elasticsearch 排序
- c - 使用c中的函数从排序数组中查找中位数
- c++ - 如何在 cmake 中包含适用于 g++ 或反之亦然的库
- php - 无法连接到 Apache Web 服务器
- windows - 我需要知道如何批量回显嵌套变量
- android - Android 应用程序 ImageView.Gone 在设备上不一致
- python - 如何使用 Python 有效地从 XML 文件中创建表?