Movatterモバイル変換


[0]ホーム

URL:


Next: ,Previous: Assembler Options,Up: Invoking GCC


3.13 Options for Linking

These options come into play when the compiler links object files intoan executable output file. They are meaningless if the compiler isnot doing a link step.

object-file-name
A file name that does not end in a special recognized suffix isconsidered to name an object file or library. (Object files aredistinguished from libraries by the linker according to the filecontents.) If linking is done, these object files are used as inputto the linker.
-c
-S
-E
If any of these options is used, then the linker is not run, andobject file names should not be used as arguments. SeeOverall Options.


-llibrary
-llibrary
Search the library namedlibrary when linking. (The secondalternative with the library as a separate argument is only forPOSIX compliance and is not recommended.)

It makes a difference where in the command you write this option; thelinker searches and processes libraries and object files in the order theyare specified. Thus, `foo.o -lz bar.o' searches library `z'after filefoo.o but beforebar.o. Ifbar.o refersto functions in `z', those functions may not be loaded.

The linker searches a standard list of directories for the library,which is actually a file namedliblibrary.a. The linkerthen uses this file as if it had been specified precisely by name.

The directories searched include several standard system directoriesplus any that you specify with-L.

Normally the files found this way are library files—archive fileswhose members are object files. The linker handles an archive file byscanning through it for members which define symbols that have so farbeen referenced but not defined. But if the file that is found is anordinary object file, it is linked in the usual fashion. The onlydifference between using an-l option and specifying a file nameis that-l surroundslibrary with `lib' and `.a'and searches several directories.

-lobjc
You need this special case of the-l option in order tolink an Objective-C or Objective-C++ program.
-nostartfiles
Do not use the standard system startup files when linking. The standard system libraries are used normally, unless-nostdlibor-nodefaultlibs is used.
-nodefaultlibs
Do not use the standard system libraries when linking. Only the libraries you specify will be passed to the linker. The standard startup files are used normally, unless-nostartfilesis used. The compiler may generate calls tomemcmp,memset,memcpy andmemmove. These entries are usually resolved by entries inlibc. These entry points should be supplied through some othermechanism when this option is specified.
-nostdlib
Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be passed tothe linker. The compiler may generate calls tomemcmp,memset,memcpy andmemmove. These entries are usually resolved by entries inlibc. These entry points should be supplied through some othermechanism when this option is specified.

One of the standard libraries bypassed by-nostdlib and-nodefaultlibs islibgcc.a, a library of internal subroutinesthat GCC uses to overcome shortcomings of particular machines, or specialneeds for some languages. (SeeInterfacing to GCC Output,for more discussion oflibgcc.a.) In most cases, you needlibgcc.a even when you want to avoidother standard libraries. In other words, when you specify-nostdlibor-nodefaultlibs you should usually specify-lgcc as well. This ensures that you have no unresolved references to internal GCClibrary subroutines. (For example, `__main', used to ensure C++constructors will be called; seecollect2.)

-pie
Produce a position independent executable on targets which support it. For predictable results, you must also specify the same set of optionsthat were used to generate code (-fpie,-fPIE,or model suboptions) when you specify this option.
-rdynamic
Pass the flag-export-dynamic to the ELF linker, on targetsthat support it. This instructs the linker to add all symbols, notonly used ones, to the dynamic symbol table. This option is neededfor some uses ofdlopen or to allow obtaining backtracesfrom within a program.
-s
Remove all symbol table and relocation information from the executable.
-static
On systems that support dynamic linking, this prevents linking with the sharedlibraries. On other systems, this option has no effect.
-shared
Produce a shared object which can then be linked with other objects toform an executable. Not all systems support this option. For predictableresults, you must also specify the same set of options that were used togenerate code (-fpic,-fPIC, or model suboptions)when you specify this option.1
-shared-libgcc
-static-libgcc
On systems that providelibgcc as a shared library, these optionsforce the use of either the shared or static version respectively. If no shared version oflibgcc was built when the compiler wasconfigured, these options have no effect.

There are several situations in which an application should use thesharedlibgcc instead of the static version. The most commonof these is when the application wishes to throw and catch exceptionsacross different shared libraries. In that case, each of the librariesas well as the application itself should use the sharedlibgcc.

Therefore, the G++ and GCJ drivers automatically add-shared-libgcc whenever you build a shared library or a mainexecutable, because C++ and Java programs typically use exceptions, sothis is the right thing to do.

If, instead, you use the GCC driver to create shared libraries, you mayfind that they will not always be linked with the sharedlibgcc. If GCC finds, at its configuration time, that you have a non-GNU linkeror a GNU linker that does not support option--eh-frame-hdr,it will link the shared version oflibgcc into shared librariesby default. Otherwise, it will take advantage of the linker and optimizeaway the linking with the shared version oflibgcc, linking withthe static version of libgcc by default. This allows exceptions topropagate through such shared libraries, without incurring relocationcosts at library load time.

However, if a library or main executable is supposed to throw or catchexceptions, you must link it using the G++ or GCJ driver, as appropriatefor the languages used in the program, or using the option-shared-libgcc, such that it is linked with the sharedlibgcc.

-symbolic
Bind references to global symbols when building a shared object. Warnabout any unresolved references (unless overridden by the link editoroption `-Xlinker -z -Xlinker defs'). Only a few systems supportthis option.
-Xlinkeroption
Passoption as an option to the linker. You can use this tosupply system-specific linker options which GCC does not know how torecognize.

If you want to pass an option that takes an argument, you must use-Xlinker twice, once for the option and once for the argument. For example, to pass-assert definitions, you must write`-Xlinker -assert -Xlinker definitions'. It does not work to write-Xlinker "-assert definitions", because this passes the entirestring as a single argument, which is not what the linker expects.

-Wl,option
Passoption as an option to the linker. Ifoption containscommas, it is split into multiple options at the commas.
-usymbol
Pretend the symbolsymbol is undefined, to force linking oflibrary modules to define it. You can use-u multiple times withdifferent symbols to force loading of additional library modules.

Footnotes

[1] On some systems, `gcc -shared'needs to build supplementary stub code for constructors to work. Onmulti-libbed systems, `gcc -shared' must select the correct supportlibraries to link against. Failing to supply the correct flags may leadto subtle defects. Supplying them in cases where they are not necessaryis innocuous.



[8]ページ先頭

©2009-2025 Movatter.jp