起因
在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