eBPF 开发中 ‘asm/types.h‘ file not found 的原因分析及其解决方法

起因

在eBPF程序开发中,当我们包含了 <asm/types.h> 头文件,在编译时,可能会出现问题:

$ clang -target bpf -g -O2 -c xdp.c -o xdp.o
In file included from xdp.c:2:
/usr/include/linux/types.h:5:10: fatal error: 'asm/types.h' file not found
    5 | #include <asm/types.h>
      |          ^~~~~~~~~~~~~
1 error generated.

原因是,<asm/types.h> 头文件保存在了 /usr/include/$(uname -m)-linux-gnu/ 目录

$ ls /usr/include/$(uname -m)-linux-gnu/
a.out.h  asm  bits  c++  ffi.h  ffitarget.h  fpu_control.h  gnu  ieee754.h  sys

在以 bpf 为目标时,不会自动搜索这个目录

$ clang -target bpf -v -E -x c /dev/null
Ubuntu clang version 18.1.3 (1ubuntu1)
Target: bpf
Thread model: posix
InstalledDir: /usr/bin
 (in-process)
 "/usr/lib/llvm-18/bin/clang" -cc1 -triple bpf -E -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name null -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -debugger-tuning=gdb -fdebug-compilation-dir=/home/hit/http-pcap -v -fcoverage-compilation-dir=/home/hit/http-pcap -resource-dir /usr/lib/llvm-18/lib/clang/18 -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -faddrsig -o - -x c /dev/null
clang -cc1 version 18.1.3 based upon LLVM 18.1.3 default target x86_64-pc-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/llvm-18/lib/clang/18/include
 /usr/include
End of search list.
# 1 "/dev/null"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 339 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/dev/null" 2

解决方法

方法1:建立软连接

sudo ln -sf /usr/include/$(uname -m)-linux-gnu/asm /usr/include/asm

方法2:指定目录

$ clang -target bpf -g -O2 -c xdp.c -o xdp.o -I/usr/include/$(uname -m)-linux-gnu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值