On 11/20, Y Song wrote: > On Tue, Nov 20, 2018 at 10:19 AM Stanislav Fomichev <s...@google.com> wrote: > > > > Wrap headers in extern "C", to turn off C++ mangling. > > This simplifies including libbpf in c++ and linking against it. > > > > Signed-off-by: Stanislav Fomichev <s...@google.com> > > --- > > tools/lib/bpf/bpf.h | 9 +++++++++ > > tools/lib/bpf/libbpf.h | 9 +++++++++ > > Do you want to add tools/lib/bpf/btf.h as well? it has some functions > which could be used outside libbpf as well. Sure, I can do that in v2.
> > 2 files changed, 18 insertions(+) > > > > diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h > > index 26a51538213c..9ea3aec82d8a 100644 > > --- a/tools/lib/bpf/bpf.h > > +++ b/tools/lib/bpf/bpf.h > > @@ -27,6 +27,10 @@ > > #include <stdbool.h> > > #include <stddef.h> > > > > +#ifdef __cplusplus > > +extern "C" { > > +#endif > > Some (but not all) __cplusplus extern wrappers wraps standard include as well > like "#include <stdbool.h>", "#include <stddef.h>". probably does not > matter here > since they may not have function prototype. But just want to point it out so > you > are aware of this and may double check. Standard headers should be safe, they are usually wrapped into __BEGIN_DECLS/__END_DECLS (which is extern "C" {}). For <linux/bpf.h> I don't think we need an extern, because it only defines enums/structs and not something we can link against (global vars/functions). > > + > > #ifndef LIBBPF_API > > #define LIBBPF_API __attribute__((visibility("default"))) > > #endif > > @@ -128,4 +132,9 @@ LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, > > char *log_buf, > > LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, > > __u32 *buf_len, __u32 *prog_id, __u32 > > *fd_type, > > __u64 *probe_offset, __u64 *probe_addr); > > + > > +#ifdef __cplusplus > > +} /* extern "C" */ > > +#endif > > + > > #endif /* __LIBBPF_BPF_H */ > > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h > > index b1686a787102..74e57e041705 100644 > > --- a/tools/lib/bpf/libbpf.h > > +++ b/tools/lib/bpf/libbpf.h > > @@ -16,6 +16,10 @@ > > #include <sys/types.h> // for size_t > > #include <linux/bpf.h> > > > > +#ifdef __cplusplus > > +extern "C" { > > +#endif > > + > > #ifndef LIBBPF_API > > #define LIBBPF_API __attribute__((visibility("default"))) > > #endif > > @@ -335,4 +339,9 @@ int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, > > int ifindex, > > libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); > > int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int > > handle, > > libbpf_dump_nlmsg_t dump_filter_nlmsg, void > > *cookie); > > + > > +#ifdef __cplusplus > > +} /* extern "C" */ > > +#endif > > + > > #endif /* __LIBBPF_LIBBPF_H */ > > -- > > 2.19.1.1215.g8438c0b245-goog > >