title: Error compiling eBPF C code inside kernel source files
body: <p>I'm compiling the eBPf programs inside <a href="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/bpf" rel="nofollow noreferrer">Linux kernel tree</a>, after I run the command <code>sudo make headers_install &amp;&amp; sudo make  M=samples/bpf</code>, error shows agian.  In fact, I'm in stuck here for quite a long time. The first time the error is something wrong with my ubuntu. So I reinstalled it. But now the error is strange.</p>
  <pre class="lang-bash prettyprint-override"><code>skeleton/pid_iter.bpf.c:47:14: error: incomplete definition of type 'struct bpf_perf_link'
          perf_link = container_of(link, struct bpf_perf_link, link);
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:74:22: note: expanded from macro 'container_of'
                  ((type *)(__mptr - offsetof(type, member)));    \
                                     ^~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:68:60: note: expanded from macro 'offsetof'
  #define offsetof(TYPE, MEMBER)  ((unsigned long)&amp;((TYPE *)0)-&gt;MEMBER)
                                                   ~~~~~~~~~~~^
  skeleton/pid_iter.bpf.c:44:9: note: forward declaration of 'struct bpf_perf_link'
          struct bpf_perf_link *perf_link;
                 ^
  skeleton/pid_iter.bpf.c:48:10: error: incomplete definition of type 'struct bpf_perf_link'
          event = BPF_CORE_READ(perf_link, perf_file, private_data);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:403:2: note: expanded from macro 'BPF_CORE_READ'
          ___type((src), a, ##__VA_ARGS__) __r;                               \
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:274:29: note: expanded from macro '___type'
  #define ___type(...) typeof(___arrow(__VA_ARGS__))
                              ^~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:272:23: note: expanded from macro '___arrow'
  #define ___arrow(...) ___apply(___arrow, ___narg(__VA_ARGS__))(__VA_ARGS__)
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:223:25: note: expanded from macro '___concat'
  #define ___concat(a, b) a ## b
                          ^
  &lt;scratch space&gt;:19:1: note: expanded from here
  ___arrow3
  ^
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:264:29: note: expanded from macro '___arrow3'
  #define ___arrow3(a, b, c) a-&gt;b-&gt;c
                             ~^
  skeleton/pid_iter.bpf.c:44:9: note: forward declaration of 'struct bpf_perf_link'
          struct bpf_perf_link *perf_link;
                 ^
  skeleton/pid_iter.bpf.c:48:10: error: incomplete definition of type 'struct bpf_perf_link'
          event = BPF_CORE_READ(perf_link, perf_file, private_data);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:404:2: note: expanded from macro 'BPF_CORE_READ'
          BPF_CORE_READ_INTO(&amp;__r, (src), a, ##__VA_ARGS__);                  \
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:311:2: note: expanded from macro 'BPF_CORE_READ_INTO'
          ___core_read(bpf_core_read, bpf_core_read,                          \
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:302:2: note: expanded from macro '___core_read'
          ___apply(___core_read, ___empty(__VA_ARGS__))(fn, fn_ptr, dst,      \
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  note: (skipping 9 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:280:33: note: expanded from macro '___rd_first'
  #define ___rd_first(fn, src, a) ___read(fn, &amp;__t, ___type(src), src, a);
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:277:59: note: expanded from macro '___read'
          read_fn((void *)(dst), sizeof(*(dst)), &amp;((src_type)(src))-&gt;accessor)
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:206:79: note: expanded from macro 'bpf_core_read'
          bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src))
                                                                                       ^~~
  skeleton/pid_iter.bpf.c:44:9: note: forward declaration of 'struct bpf_perf_link'
          struct bpf_perf_link *perf_link;
                 ^
  skeleton/pid_iter.bpf.c:48:10: error: incomplete definition of type 'struct bpf_perf_link'
          event = BPF_CORE_READ(perf_link, perf_file, private_data);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:404:2: note: expanded from macro 'BPF_CORE_READ'
          BPF_CORE_READ_INTO(&amp;__r, (src), a, ##__VA_ARGS__);                  \
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:311:2: note: expanded from macro 'BPF_CORE_READ_INTO'
          ___core_read(bpf_core_read, bpf_core_read,                          \
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:302:2: note: expanded from macro '___core_read'
          ___apply(___core_read, ___empty(__VA_ARGS__))(fn, fn_ptr, dst,      \
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  note: (skipping 9 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:263:26: note: expanded from macro '___arrow2'
  #define ___arrow2(a, b) a-&gt;b
                           ^
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:277:44: note: expanded from macro '___read'
          read_fn((void *)(dst), sizeof(*(dst)), &amp;((src_type)(src))-&gt;accessor)
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:206:79: note: expanded from macro 'bpf_core_read'
          bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src))
                                                                                       ^~~
  skeleton/pid_iter.bpf.c:44:9: note: forward declaration of 'struct bpf_perf_link'
          struct bpf_perf_link *perf_link;
                 ^
  skeleton/pid_iter.bpf.c:48:8: warning: incompatible integer to pointer conversion assigning to 'struct perf_event *' from 'long' [-Wint-conversion]
          event = BPF_CORE_READ(perf_link, perf_file, private_data);
                ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  skeleton/pid_iter.bpf.c:49:30: error: no member named 'bpf_cookie' in 'struct perf_event'
          return BPF_CORE_READ(event, bpf_cookie);
                 ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:403:17: note: expanded from macro 'BPF_CORE_READ'
          ___type((src), a, ##__VA_ARGS__) __r;                               \
          ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:274:38: note: expanded from macro '___type'
  #define ___type(...) typeof(___arrow(__VA_ARGS__))
                              ~~~~~~~~~^~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:272:64: note: expanded from macro '___arrow'
  #define ___arrow(...) ___apply(___arrow, ___narg(__VA_ARGS__))(__VA_ARGS__)
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:263:28: note: expanded from macro '___arrow2'
  #define ___arrow2(a, b) a-&gt;b
                          ~  ^
  skeleton/pid_iter.bpf.c:49:30: error: no member named 'bpf_cookie' in 'struct perf_event'
          return BPF_CORE_READ(event, bpf_cookie);
                 ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:404:34: note: expanded from macro 'BPF_CORE_READ'
          BPF_CORE_READ_INTO(&amp;__r, (src), a, ##__VA_ARGS__);                  \
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:312:20: note: expanded from macro 'BPF_CORE_READ_INTO'
                       dst, (src), a, ##__VA_ARGS__)                          \
                       ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:303:18: note: expanded from macro '___core_read'
                                                        src, a, ##__VA_ARGS__)
                                                        ~~~~~^~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:296:38: note: expanded from macro '___core_read0'
          ___read(fn, dst, ___type(src), src, a);
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:277:61: note: expanded from macro '___read'
          read_fn((void *)(dst), sizeof(*(dst)), &amp;((src_type)(src))-&gt;accessor)
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:206:79: note: expanded from macro 'bpf_core_read'
          bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src))
                                                                                       ^~~
  skeleton/pid_iter.bpf.c:49:9: error: returning 'void' from a function with incompatible result type '__u64' (aka 'unsigned long long')
          return BPF_CORE_READ(event, bpf_cookie);
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:402:36: note: expanded from macro 'BPF_CORE_READ'
  #define BPF_CORE_READ(src, a, ...) ({                                       \
                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  skeleton/pid_iter.bpf.c:90:36: error: use of undeclared identifier 'BPF_LINK_TYPE_PERF_EVENT'; did you mean 'BPF_PROG_TYPE_PERF_EVENT'?
                  if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
                                                   ^~~~~~~~~~~~~~~~~~~~~~~~
                                                   BPF_PROG_TYPE_PERF_EVENT
  /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/vmlinux.h:9615:2: note: 'BPF_PROG_TYPE_PERF_EVENT' declared here
          BPF_PROG_TYPE_PERF_EVENT = 7,
          ^
  1 warning and 8 errors generated.
  make[2]: *** [Makefile:182: /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/pid_iter.bpf.o] Error 1
  make[1]: *** [samples/bpf/Makefile:296: /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bpftool] Error 2
  </code></pre>
  <p>I searched for solutions for quite a long time. I found this <a href="https://www.spinics.net/lists/kernel/msg4322624.html" rel="nofollow noreferrer">https://www.spinics.net/lists/kernel/msg4322624.html</a>. But I cannot get solution from it. Any help?</p>