⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 options.html

📁 gcc手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html lang="en">

<head>

<title>Untitled</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="Untitled">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

</head>

<body>

<div class="node">

<p>

Node:<a name="Options">Options</a>,

Next:<a rel="next" accesskey="n" href="Environment.html#Environment">Environment</a>,

Up:<a rel="up" accesskey="u" href="Invocation.html#Invocation">Invocation</a>

<hr><br>

</div>



<h3 class="section">Command Line Options</h3>



   <p>The linker supports a plethora of command-line options, but in actual

practice few of them are used in any particular context. 

For instance, a frequent use of <code>ld</code> is to link standard Unix

object files on a standard, supported Unix system.  On such a system, to

link a file <code>hello.o</code>:



<pre class="smallexample">     ld -o <var>output</var> /lib/crt0.o hello.o -lc

     </pre>



   <p>This tells <code>ld</code> to produce a file called <var>output</var> as the

result of linking the file <code>/lib/crt0.o</code> with <code>hello.o</code> and

the library <code>libc.a</code>, which will come from the standard search

directories.  (See the discussion of the <code>-l</code> option below.)



   <p>Some of the command-line options to <code>ld</code> may be specified at any

point in the command line.  However, options which refer to files, such

as <code>-l</code> or <code>-T</code>, cause the file to be read at the point at

which the option appears in the command line, relative to the object

files and other file options.  Repeating non-file options with a

different argument will either have no further effect, or override prior

occurrences (those further to the left on the command line) of that

option.  Options which may be meaningfully specified more than once are

noted in the descriptions below.



   <p>Non-option arguments are object files or archives which are to be linked

together.  They may follow, precede, or be mixed in with command-line

options, except that an object file argument may not be placed between

an option and its argument.



   <p>Usually the linker is invoked with at least one object file, but you can

specify other forms of binary input files using <code>-l</code>, <code>-R</code>,

and the script command language.  If <em>no</em> binary input files at all

are specified, the linker does not produce any output, and issues the

message <code>No input files</code>.



   <p>If the linker can not recognize the format of an object file, it will

assume that it is a linker script.  A script specified in this way

augments the main linker script used for the link (either the default

linker script or the one specified by using <code>-T</code>).  This feature

permits the linker to link against a file which appears to be an object

or an archive, but actually merely defines some symbol values, or uses

<code>INPUT</code> or <code>GROUP</code> to load other objects.  Note that

specifying a script in this way merely augments the main linker script;

use the <code>-T</code> option to replace the default linker script entirely. 

See <a href="Scripts.html#Scripts">Scripts</a>.



   <p>For options whose names are a single letter,

option arguments must either follow the option letter without intervening

whitespace, or be given as separate arguments immediately following the

option that requires them.



   <p>For options whose names are multiple letters, either one dash or two can

precede the option name; for example, <code>-trace-symbol</code> and

<code>--trace-symbol</code> are equivalent.  Note - there is one exception to

this rule.  Multiple letter options that start with a lower case 'o' can

only be preceeded by two dashes.  This is to reduce confusion with the

<code>-o</code> option.  So for example <code>-omagic</code> sets the output file

name to <code>magic</code> whereas <code>--omagic</code> sets the NMAGIC flag on the

output.



   <p>Arguments to multiple-letter options must either be separated from the

option name by an equals sign, or be given as separate arguments

immediately following the option that requires them.  For example,

<code>--trace-symbol foo</code> and <code>--trace-symbol=foo</code> are equivalent. 

Unique abbreviations of the names of multiple-letter options are

accepted.



   <p>Note - if the linker is being invoked indirectly, via a compiler driver

(eg <code>gcc</code>) then all the linker command line options should be

prefixed by <code>-Wl,</code> (or whatever is appropriate for the particular

compiler driver) like this:



<pre class="smallexample">       gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup

     </pre>



   <p>This is important, because otherwise the compiler driver program may

silently drop the linker options, resulting in a bad link.



   <p>Here is a table of the generic command line switches accepted by the GNU

linker:



     <dl>

<dt><code>-a</code><var>keyword</var><code></code>

     <dd>This option is supported for HP/UX compatibility.  The <var>keyword</var>

argument must be one of the strings <code>archive</code>, <code>shared</code>, or

<code>default</code>.  <code>-aarchive</code> is functionally equivalent to

<code>-Bstatic</code>, and the other two keywords are functionally equivalent

to <code>-Bdynamic</code>.  This option may be used any number of times.



     <br><dt><code>-A</code><var>architecture</var><code></code>

     <dd><dt><code>--architecture=</code><var>architecture</var><code></code>

     <dd>In the current release of <code>ld</code>, this option is useful only for the

Intel 960 family of architectures.  In that <code>ld</code> configuration, the

<var>architecture</var> argument identifies the particular architecture in

the 960 family, enabling some safeguards and modifying the

archive-library search path.  See <a href="i960.html#i960"><code>ld</code> and the Intel 960 family</a>, for details.



     <p>Future releases of <code>ld</code> may support similar functionality for

other architecture families.



     <br><dt><code>-b </code><var>input-format</var><code></code>

     <dd><dt><code>--format=</code><var>input-format</var><code></code>

     <dd><code>ld</code> may be configured to support more than one kind of object

file.  If your <code>ld</code> is configured this way, you can use the

<code>-b</code> option to specify the binary format for input object files

that follow this option on the command line.  Even when <code>ld</code> is

configured to support alternative object formats, you don't usually need

to specify this, as <code>ld</code> should be configured to expect as a

default input format the most usual format on each machine. 

<var>input-format</var> is a text string, the name of a particular format

supported by the BFD libraries.  (You can list the available binary

formats with <code>objdump -i</code>.) 

See <a href="BFD.html#BFD">BFD</a>.



     <p>You may want to use this option if you are linking files with an unusual

binary format.  You can also use <code>-b</code> to switch formats explicitly (when

linking object files of different formats), by including

<code>-b </code><var>input-format</var><code></code> before each group of object files in a

particular format.



     <p>The default format is taken from the environment variable

<code>GNUTARGET</code>. 

See <a href="Environment.html#Environment">Environment</a>. 

You can also define the input format from a script, using the command

<code>TARGET</code>;

see <a href="Format-Commands.html#Format%20Commands">Format Commands</a>.



     <br><dt><code>-c </code><var>MRI-commandfile</var><code></code>

     <dd><dt><code>--mri-script=</code><var>MRI-commandfile</var><code></code>

     <dd>For compatibility with linkers produced by MRI, <code>ld</code> accepts script

files written in an alternate, restricted command language, described in

<a href="MRI.html#MRI">MRI Compatible Script Files</a>. 

Introduce MRI script files with

the option <code>-c</code>; use the <code>-T</code> option to run linker

scripts written in the general-purpose <code>ld</code> scripting language. 

If <var>MRI-cmdfile</var> does not exist, <code>ld</code> looks for it in the directories

specified by any <code>-L</code> options.



     <br><dt><code>-d</code>

     <dd><dt><code>-dc</code>

     <dd><dt><code>-dp</code>

     <dd>These three options are equivalent; multiple forms are supported for

compatibility with other linkers.  They assign space to common symbols

even if a relocatable output file is specified (with <code>-r</code>).  The

script command <code>FORCE_COMMON_ALLOCATION</code> has the same effect. 

See <a href="Miscellaneous-Commands.html#Miscellaneous%20Commands">Miscellaneous Commands</a>.



     <br><dt><code>-e </code><var>entry</var><code></code>

     <dd><dt><code>--entry=</code><var>entry</var><code></code>

     <dd>Use <var>entry</var> as the explicit symbol for beginning execution of your

program, rather than the default entry point.  If there is no symbol

named <var>entry</var>, the linker will try to parse <var>entry</var> as a number,

and use that as the entry address (the number will be interpreted in

base 10; you may use a leading <code>0x</code> for base 16, or a leading

<code>0</code> for base 8).  See <a href="Entry-Point.html#Entry%20Point">Entry Point</a>, for a discussion of defaults

and other ways of specifying the entry point.



     <br><dt><code>-E</code>

     <dd><dt><code>--export-dynamic</code>

     <dd>When creating a dynamically linked executable, add all symbols to the

dynamic symbol table.  The dynamic symbol table is the set of symbols

which are visible from dynamic objects at run time.



     <p>If you do not use this option, the dynamic symbol table will normally

contain only those symbols which are referenced by some dynamic object

mentioned in the link.



     <p>If you use <code>dlopen</code> to load a dynamic object which needs to refer

back to the symbols defined by the program, rather than some other

dynamic object, then you will probably need to use this option when

linking the program itself.



     <p>You can also use the version script to control what symbols should

be added to the dynamic symbol table if the output format supports it. 

See the description of <code>--version-script</code> in <a href="VERSION.html#VERSION">VERSION</a>.



     <br><dt><code>-EB</code>

     <dd>Link big-endian objects.  This affects the default output format.



     <br><dt><code>-EL</code>

     <dd>Link little-endian objects.  This affects the default output format.



     <br><dt><code>-f</code>

     <dd><dt><code>--auxiliary </code><var>name</var><code></code>

     <dd>When creating an ELF shared object, set the internal DT_AUXILIARY field

to the specified name.  This tells the dynamic linker that the symbol

table of the shared object should be used as an auxiliary filter on the

symbol table of the shared object <var>name</var>.



     <p>If you later link a program against this filter object, then, when you

run the program, the dynamic linker will see the DT_AUXILIARY field.  If

the dynamic linker resolves any symbols from the filter object, it will

first check whether there is a definition in the shared object

<var>name</var>.  If there is one, it will be used instead of the definition

in the filter object.  The shared object <var>name</var> need not exist. 

Thus the shared object <var>name</var> may be used to provide an alternative

implementation of certain functions, perhaps for debugging or for

machine specific performance.



     <p>This option may be specified more than once.  The DT_AUXILIARY entries

will be created in the order in which they appear on the command line.



     <br><dt><code>-F </code><var>name</var><code></code>

     <dd><dt><code>--filter </code><var>name</var><code></code>

     <dd>When creating an ELF shared object, set the internal DT_FILTER field to

the specified name.  This tells the dynamic linker that the symbol table

of the shared object which is being created should be used as a filter

on the symbol table of the shared object <var>name</var>.



     <p>If you later link a program against this filter object, then, when you

run the program, the dynamic linker will see the DT_FILTER field.  The

dynamic linker will resolve symbols according to the symbol table of the

filter object as usual, but it will actually link to the definitions

found in the shared object <var>name</var>.  Thus the filter object can be

used to select a subset of the symbols provided by the object

<var>name</var>.



     <p>Some older linkers used the <code>-F</code> option throughout a compilation

toolchain for specifying object-file format for both input and output

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -