linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libtraceevent: Fixing linking to C++ code
@ 2022-08-15  3:15 Joel Fernandes (Google)
  2022-09-08 22:16 ` Steven Rostedt
  0 siblings, 1 reply; 2+ messages in thread
From: Joel Fernandes (Google) @ 2022-08-15  3:15 UTC (permalink / raw)
  To: linux-trace-devel; +Cc: rostedt, Joel Fernandes (Google)

Linking in C++ compilers (including g++) causes references to be created
with their arguments. Due to this, trace library headers included into
C++ code base will cause their objects to built with symbols with
arguments. Apparently this is to support operator overloading in C++.

This causes linker errors. For example, here's what I get when I try to
link libtraceevent with a main.o built from a C++ main.cc source file.

 main.cc:(.text+0x90):
	undefined reference to `trace_seq_init(trace_seq*)'
	undefined reference to `trace_seq_do_printf(trace_seq*)'
	undefined reference to `tep_event_fields(tep_event*)'

The standard fix for this is to wrap the C project's header in
extern "C".

With this patch, I am able to link libtraceevent into a C++
code base.

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 include/traceevent/event-parse.h | 8 ++++++++
 include/traceevent/trace-seq.h   | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/include/traceevent/event-parse.h b/include/traceevent/event-parse.h
index 0b911e1..9d7634e 100644
--- a/include/traceevent/event-parse.h
+++ b/include/traceevent/event-parse.h
@@ -14,6 +14,10 @@
 
 #include "trace-seq.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ifndef __maybe_unused
 #define __maybe_unused __attribute__((unused))
 #endif
@@ -778,4 +782,8 @@ void tep_set_loglevel(enum tep_loglevel level);
 void tep_print_field(struct trace_seq *s, void *data,
 		     struct tep_format_field *field);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* _PARSE_EVENTS_H */
diff --git a/include/traceevent/trace-seq.h b/include/traceevent/trace-seq.h
index d68ec69..217492f 100644
--- a/include/traceevent/trace-seq.h
+++ b/include/traceevent/trace-seq.h
@@ -10,6 +10,10 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* ----------------------- trace_seq ----------------------- */
 
 #ifndef TRACE_SEQ_BUF_SIZE
@@ -52,4 +56,8 @@ extern void trace_seq_terminate(struct trace_seq *s);
 extern int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp);
 extern int trace_seq_do_printf(struct trace_seq *s);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* _TRACE_SEQ_H */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] libtraceevent: Fixing linking to C++ code
  2022-08-15  3:15 [PATCH] libtraceevent: Fixing linking to C++ code Joel Fernandes (Google)
@ 2022-09-08 22:16 ` Steven Rostedt
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2022-09-08 22:16 UTC (permalink / raw)
  To: Joel Fernandes (Google); +Cc: linux-trace-devel

On Sun, 14 Aug 2022 23:15:51 -0400
"Joel Fernandes (Google)" <joel@joelfernandes.org> wrote:

> Linking in C++ compilers (including g++) causes references to be created
> with their arguments. Due to this, trace library headers included into
> C++ code base will cause their objects to built with symbols with
> arguments. Apparently this is to support operator overloading in C++.
> 
> This causes linker errors. For example, here's what I get when I try to
> link libtraceevent with a main.o built from a C++ main.cc source file.
> 
>  main.cc:(.text+0x90):
> 	undefined reference to `trace_seq_init(trace_seq*)'
> 	undefined reference to `trace_seq_do_printf(trace_seq*)'
> 	undefined reference to `tep_event_fields(tep_event*)'
> 
> The standard fix for this is to wrap the C project's header in
> extern "C".
> 
> With this patch, I am able to link libtraceevent into a C++
> code base.
> 
> Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> ---

Applied. Thanks Joel!

-- Steve

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-08 22:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-15  3:15 [PATCH] libtraceevent: Fixing linking to C++ code Joel Fernandes (Google)
2022-09-08 22:16 ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).