【发布时间】:2019-07-26 07:20:39
【问题描述】:
我运行最新的 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 并且我有相关的库(a 等等)。
怎么了?
更新
即使下面的代码也行不通
#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;但这是一个问题,这与我的问题无关。
【问题讨论】:
-
你确定
bpf_create_map是助手的一部分吗?因为我在手册页中找不到它。 -
使用 GCC 和标准的 Linux 链接器,链接时在命令行上的目标文件和库的顺序很重要。如果目标文件
A.o依赖于库B,则A.o必须在 库B之前。因此,您显示的最后一个命令应该可以工作如果该函数实际上存在于该库中。 -
此外,阅读the BPF manual page 并没有提及要链接的库或包含的头文件。