Link Options - Using the GNU Compiler Collection (GCC) (2024)

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. See Overall Options.
-llibrary
-l library
Search the library named library 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 file foo.o but before bar.o. If bar.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 named liblibrary.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 surrounds library 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 are passed to the linker, and optionsspecifying linkage of the system libraries, such as -static-libgccor -shared-libgcc, are ignored. The standard startup files are used normally, unless -nostartfilesis used.

The compiler may generate calls to memcmp,memset, memcpy and memmove. 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 are passed tothe linker, and options specifying linkage of the system libraries, such as-static-libgcc or -shared-libgcc, are ignored.

The compiler may generate calls to memcmp, memset,memcpy and memmove. 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 is libgcc.a, a library of internal subroutineswhich GCC uses to overcome shortcomings of particular machines, or specialneeds for some languages. (See Interfacing to GCC Output,for more discussion of libgcc.a.) In most cases, you need libgcc.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. (An example of such an internal subroutine is ‘__main’, used to ensure C++constructors are called; see collect2.)

-pie
Produce a position independent executable on targets that support it. For predictable results, you must also specify the same set of optionsused for compilation (-fpie, -fPIE,or model suboptions) when you specify this linker 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 of dlopen 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 used for compilation(-fpic, -fPIC, or model suboptions) whenyou specify this linker option.1
-shared-libgcc
-static-libgcc
On systems that provide libgcc as a shared library, these optionsforce the use of either the shared or static version, respectively. If no shared version of libgcc was built when the compiler wasconfigured, these options have no effect.

There are several situations in which an application should use theshared libgcc 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 shared libgcc.

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 are not always linked with the shared libgcc. 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 links the shared version of libgcc into shared librariesby default. Otherwise, it takes advantage of the linker and optimizesaway the linking with the shared version of libgcc, 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.

-static-libasan
When the -fsanitize=address option is used to link a program,the GCC driver automatically links against libasan. Iflibasan is available as a shared library, and the -staticoption is not used, then this links against the shared version oflibasan. The -static-libasan option directs the GCCdriver to link libasan statically, without necessarily linkingother libraries statically.
-static-libtsan
When the -fsanitize=thread option is used to link a program,the GCC driver automatically links against libtsan. Iflibtsan is available as a shared library, and the -staticoption is not used, then this links against the shared version oflibtsan. The -static-libtsan option directs the GCCdriver to link libtsan statically, without necessarily linkingother libraries statically.
-static-liblsan
When the -fsanitize=leak option is used to link a program,the GCC driver automatically links against liblsan. Ifliblsan is available as a shared library, and the -staticoption is not used, then this links against the shared version ofliblsan. The -static-liblsan option directs the GCCdriver to link liblsan statically, without necessarily linkingother libraries statically.
-static-libubsan
When the -fsanitize=undefined option is used to link a program,the GCC driver automatically links against libubsan. Iflibubsan is available as a shared library, and the -staticoption is not used, then this links against the shared version oflibubsan. The -static-libubsan option directs the GCCdriver to link libubsan statically, without necessarily linkingother libraries statically.
-static-libstdc++
When the g++ program is used to link a C++ program, itnormally automatically links against libstdc++. Iflibstdc++ is available as a shared library, and the-static option is not used, then this links against theshared version of libstdc++. That is normally fine. However, itis sometimes useful to freeze the version of libstdc++ used bythe program without going all the way to a fully static link. The-static-libstdc++ option directs the g++ driver tolink libstdc++ statically, without necessarily linking otherlibraries statically.
-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.
-T script
Use script as the linker script. This option is supported by mostsystems using the GNU linker. On some targets, such as bare-boardtargets without an operating system, the -T option may be requiredwhen linking to avoid references to undefined symbols.
-Xlinker option
Pass option as an option to the linker. You can use this tosupply system-specific linker options that GCC does not recognize.

If you want to pass an option that takes a separate 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.

When using the GNU linker, it is usually more convenient to passarguments to linker options using the option=valuesyntax than as separate arguments. For example, you can specify-Xlinker -Map=output.map rather than-Xlinker -Map -Xlinker output.map. Other linkers may not supportthis syntax for command-line options.

-Wl,option
Pass option as an option to the linker. If option containscommas, it is split into multiple options at the commas. You can use thissyntax to pass an argument to the option. For example, -Wl,-Map,output.map passes -Map output.map to thelinker. When using the GNU linker, you can also get the same effect with-Wl,-Map=output.map.
-u symbol
Pretend the symbol symbol 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.
Link Options - Using the GNU Compiler Collection (GCC) (2024)
Top Articles
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated:

Views: 6402

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.