]>
Commit | Line | Data |
---|---|---|
c63539ff ML |
1 | .. |
2 | Copyright 1988-2022 Free Software Foundation, Inc. | |
3 | This is part of the GCC manual. | |
4 | For copying conditions, see the copyright.rst file. | |
5 | ||
6 | .. index:: link options, options, linking | |
7 | ||
8 | .. _link-options: | |
9 | ||
10 | Options for Linking | |
11 | ******************* | |
12 | ||
13 | These options come into play when the compiler links object files into | |
14 | an executable output file. They are meaningless if the compiler is | |
15 | not doing a link step. | |
16 | ||
17 | .. index:: file names | |
18 | ||
19 | ``object-file-name`` | |
20 | A file name that does not end in a special recognized suffix is | |
21 | considered to name an object file or library. (Object files are | |
22 | distinguished from libraries by the linker according to the file | |
23 | contents.) If linking is done, these object files are used as input | |
24 | to the linker. | |
25 | ||
26 | .. option:: -c, -S, -E | |
27 | ||
28 | If any of these options is used, then the linker is not run, and | |
29 | object file names should not be used as arguments. See :ref:`overall-options`. | |
30 | ||
31 | .. option:: -flinker-output={type} | |
32 | ||
33 | This option controls code generation of the link-time optimizer. By | |
34 | default the linker output is automatically determined by the linker | |
35 | plugin. For debugging the compiler and if incremental linking with a | |
36 | non-LTO object file is desired, it may be useful to control the type | |
37 | manually. | |
38 | ||
39 | If :samp:`{type}` is :samp:`exec`, code generation produces a static | |
40 | binary. In this case :option:`-fpic` and :option:`-fpie` are both | |
41 | disabled. | |
42 | ||
43 | If :samp:`{type}` is :samp:`dyn`, code generation produces a shared | |
44 | library. In this case :option:`-fpic` or :option:`-fPIC` is preserved, | |
45 | but not enabled automatically. This allows to build shared libraries | |
46 | without position-independent code on architectures where this is | |
47 | possible, i.e. on x86. | |
48 | ||
49 | If :samp:`{type}` is :samp:`pie`, code generation produces an :option:`-fpie` | |
50 | executable. This results in similar optimizations as :samp:`exec` | |
51 | except that :option:`-fpie` is not disabled if specified at compilation | |
52 | time. | |
53 | ||
54 | If :samp:`{type}` is :samp:`rel`, the compiler assumes that incremental linking is | |
55 | done. The sections containing intermediate code for link-time optimization are | |
56 | merged, pre-optimized, and output to the resulting object file. In addition, if | |
57 | :option:`-ffat-lto-objects` is specified, binary code is produced for future | |
58 | non-LTO linking. The object file produced by incremental linking is smaller | |
59 | than a static library produced from the same object files. At link time the | |
60 | result of incremental linking also loads faster than a static | |
61 | library assuming that the majority of objects in the library are used. | |
62 | ||
63 | Finally :samp:`nolto-rel` configures the compiler for incremental linking where | |
64 | code generation is forced, a final binary is produced, and the intermediate | |
65 | code for later link-time optimization is stripped. When multiple object files | |
66 | are linked together the resulting code is better optimized than with | |
67 | link-time optimizations disabled (for example, cross-module inlining | |
68 | happens), but most of benefits of whole program optimizations are lost. | |
69 | ||
70 | During the incremental link (by :option:`-r`) the linker plugin defaults to | |
71 | rel. With current interfaces to GNU Binutils it is however not | |
72 | possible to incrementally link LTO objects and non-LTO objects into a single | |
73 | mixed object file. If any of object files in incremental link cannot | |
74 | be used for link-time optimization, the linker plugin issues a warning and | |
75 | uses :samp:`nolto-rel`. To maintain whole program optimization, it is | |
76 | recommended to link such objects into static library instead. Alternatively it | |
77 | is possible to use H.J. Lu's binutils with support for mixed objects. | |
78 | ||
79 | .. option:: -fuse-ld=bfd | |
80 | ||
81 | Use the :command:`bfd` linker instead of the default linker. | |
82 | ||
83 | .. option:: -fuse-ld=gold | |
84 | ||
85 | Use the :command:`gold` linker instead of the default linker. | |
86 | ||
87 | .. option:: -fuse-ld=lld | |
88 | ||
89 | Use the LLVM :command:`lld` linker instead of the default linker. | |
90 | ||
91 | .. option:: -fuse-ld=mold | |
92 | ||
93 | Use the Modern Linker (:command:`mold`) instead of the default linker. | |
94 | ||
95 | .. index:: Libraries | |
96 | ||
97 | .. option:: -llibrary, -l {library} | |
98 | ||
99 | Search the library named :samp:`{library}` when linking. (The second | |
100 | alternative with the library as a separate argument is only for | |
101 | POSIX compliance and is not recommended.) | |
102 | ||
103 | The :option:`-l` option is passed directly to the linker by GCC. Refer | |
104 | to your linker documentation for exact details. The general | |
105 | description below applies to the GNU linker. | |
106 | ||
107 | The linker searches a standard list of directories for the library. | |
108 | The directories searched include several standard system directories | |
109 | plus any that you specify with :option:`-L`. | |
110 | ||
111 | Static libraries are archives of object files, and have file names | |
112 | like :samp:`lib{library}.a`. Some targets also support shared | |
113 | libraries, which typically have names like :samp:`lib{library}.so`. | |
114 | If both static and shared libraries are found, the linker gives | |
115 | preference to linking with the shared library unless the | |
116 | :option:`-static` option is used. | |
117 | ||
118 | It makes a difference where in the command you write this option; the | |
119 | linker searches and processes libraries and object files in the order they | |
120 | are specified. Thus, :samp:`foo.o -lz bar.o` searches library :samp:`z` | |
121 | after file :samp:`foo.o` but before :samp:`bar.o`. If :samp:`bar.o` refers | |
122 | to functions in :samp:`z`, those functions may not be loaded. | |
123 | ||
124 | .. option:: -lobjc | |
125 | ||
126 | You need this special case of the :option:`-l` option in order to | |
127 | link an Objective-C or Objective-C++ program. | |
128 | ||
129 | .. option:: -nostartfiles | |
130 | ||
131 | Do not use the standard system startup files when linking. | |
132 | The standard system libraries are used normally, unless :option:`-nostdlib`, | |
133 | :option:`-nolibc`, or :option:`-nodefaultlibs` is used. | |
134 | ||
135 | .. option:: -nodefaultlibs | |
136 | ||
137 | Do not use the standard system libraries when linking. | |
138 | Only the libraries you specify are passed to the linker, and options | |
139 | specifying linkage of the system libraries, such as :option:`-static-libgcc` | |
140 | or :option:`-shared-libgcc`, are ignored. | |
141 | The standard startup files are used normally, unless :option:`-nostartfiles` | |
142 | is used. | |
143 | ||
144 | The compiler may generate calls to ``memcmp``, | |
145 | ``memset``, ``memcpy`` and ``memmove``. | |
146 | These entries are usually resolved by entries in | |
147 | libc. These entry points should be supplied through some other | |
148 | mechanism when this option is specified. | |
149 | ||
150 | .. option:: -nolibc | |
151 | ||
152 | Do not use the C library or system libraries tightly coupled with it when | |
153 | linking. Still link with the startup files, :samp:`libgcc` or toolchain | |
154 | provided language support libraries such as :samp:`libgnat`, :samp:`libgfortran` | |
155 | or :samp:`libstdc++` unless options preventing their inclusion are used as | |
156 | well. This typically removes :option:`-lc` from the link command line, as well | |
157 | as system libraries that normally go with it and become meaningless when | |
158 | absence of a C library is assumed, for example :option:`-lpthread` or | |
159 | :option:`-lm` in some configurations. This is intended for bare-board | |
160 | targets when there is indeed no C library available. | |
161 | ||
162 | .. option:: -nostdlib | |
163 | ||
164 | Do not use the standard system startup files or libraries when linking. | |
165 | No startup files and only the libraries you specify are passed to | |
166 | the linker, and options specifying linkage of the system libraries, such as | |
167 | :option:`-static-libgcc` or :option:`-shared-libgcc`, are ignored. | |
168 | ||
169 | The compiler may generate calls to ``memcmp``, ``memset``, | |
170 | ``memcpy`` and ``memmove``. | |
171 | These entries are usually resolved by entries in | |
172 | libc. These entry points should be supplied through some other | |
173 | mechanism when this option is specified. | |
174 | ||
175 | .. index:: -lgcc, use with -nostdlib, -nostdlib and unresolved references, unresolved references and -nostdlib, -lgcc, use with -nodefaultlibs, -nodefaultlibs and unresolved references, unresolved references and -nodefaultlibs | |
176 | ||
177 | One of the standard libraries bypassed by :option:`-nostdlib` and | |
178 | :option:`-nodefaultlibs` is :samp:`libgcc.a`, a library of internal subroutines | |
179 | which GCC uses to overcome shortcomings of particular machines, or special | |
180 | needs for some languages. | |
181 | (See :ref:`gccint:interface`, | |
182 | for more discussion of :samp:`libgcc.a`.) | |
183 | In most cases, you need :samp:`libgcc.a` even when you want to avoid | |
184 | other standard libraries. In other words, when you specify :option:`-nostdlib` | |
185 | or :option:`-nodefaultlibs` you should usually specify :option:`-lgcc` as well. | |
186 | This ensures that you have no unresolved references to internal GCC | |
187 | library subroutines. | |
188 | (An example of such an internal subroutine is ``__main``, used to ensure C++ | |
189 | constructors are called; see :ref:`gccint:collect2`.) | |
190 | ||
191 | .. option:: -nostdlib++ | |
192 | ||
193 | Do not implicitly link with standard C++ libraries. | |
194 | ||
195 | .. option:: -e {entry}, --entry={entry} | |
196 | ||
197 | Specify that the program entry point is :samp:`{entry}`. The argument is | |
198 | interpreted by the linker; the GNU linker accepts either a symbol name | |
199 | or an address. | |
200 | ||
201 | .. option:: -pie | |
202 | ||
203 | Produce a dynamically linked position independent executable on targets | |
204 | that support it. For predictable results, you must also specify the same | |
205 | set of options used for compilation (:option:`-fpie`, :option:`-fPIE`, | |
206 | or model suboptions) when you specify this linker option. | |
207 | ||
208 | .. option:: -no-pie | |
209 | ||
210 | Don't produce a dynamically linked position independent executable. | |
211 | ||
212 | .. option:: -static-pie | |
213 | ||
214 | Produce a static position independent executable on targets that support | |
215 | it. A static position independent executable is similar to a static | |
216 | executable, but can be loaded at any address without a dynamic linker. | |
217 | For predictable results, you must also specify the same set of options | |
218 | used for compilation (:option:`-fpie`, :option:`-fPIE`, or model | |
219 | suboptions) when you specify this linker option. | |
220 | ||
221 | .. option:: -pthread | |
222 | ||
223 | Link with the POSIX threads library. This option is supported on | |
224 | GNU/Linux targets, most other Unix derivatives, and also on | |
225 | x86 Cygwin and MinGW targets. On some targets this option also sets | |
226 | flags for the preprocessor, so it should be used consistently for both | |
227 | compilation and linking. | |
228 | ||
229 | .. option:: -r | |
230 | ||
231 | Produce a relocatable object as output. This is also known as partial | |
232 | linking. | |
233 | ||
234 | .. option:: -rdynamic | |
235 | ||
236 | Pass the flag :option:`-export-dynamic` to the ELF linker, on targets | |
237 | that support it. This instructs the linker to add all symbols, not | |
238 | only used ones, to the dynamic symbol table. This option is needed | |
239 | for some uses of ``dlopen`` or to allow obtaining backtraces | |
240 | from within a program. | |
241 | ||
242 | .. option:: -s | |
243 | ||
244 | Remove all symbol table and relocation information from the executable. | |
245 | ||
246 | .. option:: -static | |
247 | ||
248 | On systems that support dynamic linking, this overrides :option:`-pie` | |
249 | and prevents linking with the shared libraries. On other systems, this | |
250 | option has no effect. | |
251 | ||
252 | .. option:: -shared | |
253 | ||
254 | Produce a shared object which can then be linked with other objects to | |
255 | form an executable. Not all systems support this option. For predictable | |
256 | results, you must also specify the same set of options used for compilation | |
257 | (:option:`-fpic`, :option:`-fPIC`, or model suboptions) when | |
258 | you specify this linker option. | |
259 | ||
260 | On some systems, :samp:`gcc -shared` | |
261 | needs to build supplementary stub code for constructors to work. On | |
262 | multi-libbed systems, :samp:`gcc -shared` must select the correct support | |
263 | libraries to link against. Failing to supply the correct flags may lead | |
264 | to subtle defects. Supplying them in cases where they are not necessary | |
265 | is innocuous. | |
266 | ||
267 | .. option:: -shared-libgcc, -static-libgcc | |
268 | ||
269 | On systems that provide :samp:`libgcc` as a shared library, these options | |
270 | force the use of either the shared or static version, respectively. | |
271 | If no shared version of :samp:`libgcc` was built when the compiler was | |
272 | configured, these options have no effect. | |
273 | ||
274 | There are several situations in which an application should use the | |
275 | shared :samp:`libgcc` instead of the static version. The most common | |
276 | of these is when the application wishes to throw and catch exceptions | |
277 | across different shared libraries. In that case, each of the libraries | |
278 | as well as the application itself should use the shared :samp:`libgcc`. | |
279 | ||
280 | Therefore, the G++ driver automatically adds :option:`-shared-libgcc` | |
281 | whenever you build a shared library or a main executable, because C++ | |
282 | programs typically use exceptions, so this is the right thing to do. | |
283 | ||
284 | If, instead, you use the GCC driver to create shared libraries, you may | |
285 | find that they are not always linked with the shared :samp:`libgcc`. | |
286 | If GCC finds, at its configuration time, that you have a non-GNU linker | |
287 | or a GNU linker that does not support option :option:`--eh-frame-hdr`, | |
288 | it links the shared version of :samp:`libgcc` into shared libraries | |
289 | by default. Otherwise, it takes advantage of the linker and optimizes | |
290 | away the linking with the shared version of :samp:`libgcc`, linking with | |
291 | the static version of libgcc by default. This allows exceptions to | |
292 | propagate through such shared libraries, without incurring relocation | |
293 | costs at library load time. | |
294 | ||
295 | However, if a library or main executable is supposed to throw or catch | |
296 | exceptions, you must link it using the G++ driver, or using the option | |
297 | :option:`-shared-libgcc`, such that it is linked with the shared | |
298 | :samp:`libgcc`. | |
299 | ||
300 | .. option:: -static-libasan | |
301 | ||
302 | When the :option:`-fsanitize=address` option is used to link a program, | |
303 | the GCC driver automatically links against libasan. If | |
304 | :samp:`libasan` is available as a shared library, and the :option:`-static` | |
305 | option is not used, then this links against the shared version of | |
306 | :samp:`libasan`. The :option:`-static-libasan` option directs the GCC | |
307 | driver to link :samp:`libasan` statically, without necessarily linking | |
308 | other libraries statically. | |
309 | ||
310 | .. option:: -static-libtsan | |
311 | ||
312 | When the :option:`-fsanitize=thread` option is used to link a program, | |
313 | the GCC driver automatically links against libtsan. If | |
314 | :samp:`libtsan` is available as a shared library, and the :option:`-static` | |
315 | option is not used, then this links against the shared version of | |
316 | :samp:`libtsan`. The :option:`-static-libtsan` option directs the GCC | |
317 | driver to link :samp:`libtsan` statically, without necessarily linking | |
318 | other libraries statically. | |
319 | ||
320 | .. option:: -static-liblsan | |
321 | ||
322 | When the :option:`-fsanitize=leak` option is used to link a program, | |
323 | the GCC driver automatically links against liblsan. If | |
324 | :samp:`liblsan` is available as a shared library, and the :option:`-static` | |
325 | option is not used, then this links against the shared version of | |
326 | :samp:`liblsan`. The :option:`-static-liblsan` option directs the GCC | |
327 | driver to link :samp:`liblsan` statically, without necessarily linking | |
328 | other libraries statically. | |
329 | ||
330 | .. option:: -static-libubsan | |
331 | ||
332 | When the :option:`-fsanitize=undefined` option is used to link a program, | |
333 | the GCC driver automatically links against libubsan. If | |
334 | :samp:`libubsan` is available as a shared library, and the :option:`-static` | |
335 | option is not used, then this links against the shared version of | |
336 | :samp:`libubsan`. The :option:`-static-libubsan` option directs the GCC | |
337 | driver to link :samp:`libubsan` statically, without necessarily linking | |
338 | other libraries statically. | |
339 | ||
340 | .. option:: -static-libstdc++ | |
341 | ||
342 | When the :command:`g++` program is used to link a C++ program, it | |
343 | normally automatically links against libstdc++. If | |
344 | :samp:`libstdc++` is available as a shared library, and the | |
345 | :option:`-static` option is not used, then this links against the | |
346 | shared version of :samp:`libstdc++`. That is normally fine. However, it | |
347 | is sometimes useful to freeze the version of :samp:`libstdc++` used by | |
348 | the program without going all the way to a fully static link. The | |
349 | :option:`-static-libstdc++` option directs the :command:`g++` driver to | |
350 | link :samp:`libstdc++` statically, without necessarily linking other | |
351 | libraries statically. | |
352 | ||
353 | .. option:: -symbolic | |
354 | ||
355 | Bind references to global symbols when building a shared object. Warn | |
356 | about any unresolved references (unless overridden by the link editor | |
357 | option :option:`-Xlinker -z -Xlinker defs`). Only a few systems support | |
358 | this option. | |
359 | ||
360 | .. index:: linker script | |
361 | ||
362 | .. option:: -T {script} | |
363 | ||
364 | Use :samp:`{script}` as the linker script. This option is supported by most | |
365 | systems using the GNU linker. On some targets, such as bare-board | |
366 | targets without an operating system, the :option:`-T` option may be required | |
367 | when linking to avoid references to undefined symbols. | |
368 | ||
369 | .. option:: -Xlinker {option} | |
370 | ||
371 | Pass :samp:`{option}` as an option to the linker. You can use this to | |
372 | supply system-specific linker options that GCC does not recognize. | |
373 | ||
374 | If you want to pass an option that takes a separate argument, you must use | |
375 | :option:`-Xlinker` twice, once for the option and once for the argument. | |
376 | For example, to pass :option:`-assert definitions`, you must write | |
377 | :option:`-Xlinker -assert -Xlinker definitions`. It does not work to write | |
378 | :option:`-Xlinker "-assert definitions"`, because this passes the entire | |
379 | string as a single argument, which is not what the linker expects. | |
380 | ||
381 | When using the GNU linker, it is usually more convenient to pass | |
382 | arguments to linker options using the :samp:`{option}={value}` | |
383 | syntax than as separate arguments. For example, you can specify | |
384 | :option:`-Xlinker -Map=output.map` rather than | |
385 | :option:`-Xlinker -Map -Xlinker output.map`. Other linkers may not support | |
386 | this syntax for command-line options. | |
387 | ||
388 | .. option:: -Wl,option | |
389 | ||
390 | Pass :samp:`{option}` as an option to the linker. If :samp:`{option}` contains | |
391 | commas, it is split into multiple options at the commas. You can use this | |
392 | syntax to pass an argument to the option. | |
393 | For example, :option:`-Wl,-Map,output.map` passes :option:`-Map output.map` to the | |
394 | linker. When using the GNU linker, you can also get the same effect with | |
395 | :option:`-Wl,-Map=output.map`. | |
396 | ||
397 | .. option:: -u {symbol} | |
398 | ||
399 | Pretend the symbol :samp:`{symbol}` is undefined, to force linking of | |
400 | library modules to define it. You can use :option:`-u` multiple times with | |
401 | different symbols to force loading of additional library modules. | |
402 | ||
403 | .. option:: -z {keyword} | |
404 | ||
405 | :option:`-z` is passed directly on to the linker along with the keyword | |
406 | :samp:`{keyword}`. See the section in the documentation of your linker for | |
3ed1b4ce | 407 | permitted values and their meanings. |