History

selftests/bpf: Fix C++ compile error from missing _Bool type

While building, bpftool makes a skeleton from test_core_extern.c, which itself includes <stdbool.h> and uses the 'bool' type. However, the skeleton test_core_extern.skel.h generated *does not* include <stdbool.h> or use the 'bool' type, instead using the C-only '_Bool' type. Compiling test_cpp.cpp with g++ 12.3 for mips64el/musl-libc then fails with error: In file included from test_cpp.cpp:9: test_core_extern.skel.h:45:17: error: '_Bool' does not name a type 45 | _Bool CONFIG_BOOL; | ^~~~~ This was likely missed previously because glibc uses a GNU extension for <stdbool.h> with C++ (#define _Bool bool), not supported by musl libc. Normally, a C fragment would include <stdbool.h> and use the 'bool' type, and thus cleanly work after import by C++. The ideal fix would be for 'bpftool gen skeleton' to output the correct type/include supporting C++, but in the meantime add a conditional define as above.

Tony Ambardar committed 2 years ago

Commit aa950731 parent cacf2a51 file · +4 −0Browse files

Unified · Split

tools/testing/selftests/bpf/test_cpp.cpp+4−0
@@ -6,6 +6,10 @@ · 5 unmodified lines
66 #include <bpf/libbpf.h>
77 #include <bpf/bpf.h>
88 #include <bpf/btf.h>
9+
10+#ifndef _Bool
11+#define _Bool bool
12+#endif
913 #include "test_core_extern.skel.h"
1014 #include "struct_ops_module.skel.h"
1115