Initial import
[lttng-docs.git] / contents / using-lttng / instrumenting / c-application / building-linking / static-linking.md
CommitLineData
5e0cbfb0
PP
1---
2id: static-linking
3---
4
5With the static linking method, compiled tracepoint providers are copied
6into the target application. There are three ways to do this:
7
8 1. Use one of your **existing C source files** to create probes.
9 2. Create probes in a separate C source file and build it as an
10 **object file** to be linked with the application (more decoupled).
11 3. Create probes in a separate C source file, build it as an
12 object file and archive it to create a **static library**
13 (more decoupled, more portable).
14
15The first approach is to define `TRACEPOINT_CREATE_PROBES` and include
16your tracepoint provider(s) header file(s) directly into an existing C
17source file. Here's an example:
18
19~~~ c
20#include <stdlib.h>
21#include <stdio.h>
22/* ... */
23
24#define TRACEPOINT_CREATE_PROBES
25#define TRACEPOINT_DEFINE
26#include "tp.h"
27
28/* ... */
29
30int my_func(int a, const char* b)
31{
32 /* ... */
33
34 tracepoint(my_provider, my_tracepoint, buf, sz, limit, &tt)
35
36 /* ... */
37}
38
39/* ... */
40~~~
41
42Again, `TRACEPOINT_CREATE_PROBES` and `TRACEPOINT_DEFINE` must be
43defined in one, **and only one**, translation unit. Other C source
44files of the same application may include `tp.h` to use tracepoints
45with `tracepoint()`, but must not define
46`TRACEPOINT_CREATE_PROBES`/`TRACEPOINT_DEFINE` again.
47
48This translation unit may be built as an object file by making sure to
49add `.` to the include path:
50
51<pre class="term">
52gcc -c <strong>-I.</strong> file.c
53</pre>
54
55The second approach is to isolate the tracepoint provider code into a
56separate object file by using a dedicated C source file to create probes:
57
58~~~ c
59#define TRACEPOINT_CREATE_PROBES
60
61#include "tp.h"
62~~~
63
64`TRACEPOINT_DEFINE` must be defined by a translation unit of the
65application. Since we're talking about static linking here, it could as
66well be defined in the file above, before `#include "tp.h"`. This is
67actually what [`lttng-gen-tp`](#doc-lttng-gen-tp) does.
68
69Build the tracepoint provider:
70
71<pre class="term">
72gcc -c -I. tp.c
73</pre>
74
75Finally, the resulting object file may be archived to create a
76more portable tracepoint provider static library:
77
78<pre class="term">
79ar rc tp.a tp.o
80</pre>
81
82Using a static library does have the advantage of centralising the
83tracepoint providers objects so they can be shared between multiple
84applications. This way, when the tracepoint provider is modified, the
85source code changes don't have to be patched into each application's source
86code tree. The applications need to be relinked after each change, but need
87not to be otherwise recompiled (unless the tracepoint provider's API
88changes).
89
90Regardless of which method you choose, you end up with an object file
91(potentially archived) containing the trace providers assembled code.
92To link this code with the rest of your application, you must also link
93with `liblttng-ust` and `libdl`:
94
95<pre class="term">
96gcc -o app <strong>tp.o</strong> other.o files.o of.o your.o app.o <strong>-llttng-ust -ldl</strong>
97</pre>
98
99or
100
101<pre class="term">
102gcc -o app <strong>tp.a</strong> other.o files.o of.o your.o app.o -llttng-ust -ldl
103</pre>
104
105If you're using a <abbr title="Berkeley Software Distribution">BSD</abbr>
106system, replace `-ldl` with `-lc`:
107
108<pre class="term">
109gcc -o app tp.a other.o files.o of.o your.o app.o -llttng-ust <strong>-lc</strong>
110</pre>
111
112The application can be started as usual, e.g.:
113
114<pre class="term">
115./app
116</pre>
117
118The `lttng` command line tool can be used to
119[control tracing](#doc-controlling-tracing).
This page took 0.026071 seconds and 4 git commands to generate.