docs/versions/7.6.0/reference/be/c-cpp.mdx
{% dynamic setvar source_file "src/main/java/com/google/devtools/build/docgen/templates/be/rules.vm" %} {% dynamic setvar version "7.6.0" %} {% dynamic setvar original_path "/reference/be/c-cpp" %} {% include "_buttons.html" %}
<h2>Rules</h2> <ul> <li> <a href="#cc_binary"> cc_binary </a> </li> <li> <a href="#cc_import"> cc_import </a> </li> <li> <a href="#cc_library"> cc_library </a> </li> <li> <a href="#cc_proto_library"> cc_proto_library </a> </li> <li> <a href="#cc_shared_library"> cc_shared_library </a> </li> <li> <a href="#cc_static_library"> cc_static_library </a> </li> <li> <a href="#fdo_prefetch_hints"> fdo_prefetch_hints </a> </li> <li> <a href="#fdo_profile"> fdo_profile </a> </li> <li> <a href="#memprof_profile"> memprof_profile </a> </li> <li> <a href="#propeller_optimize"> propeller_optimize </a> </li> <li> <a href="#cc_test"> cc_test </a> </li> <li> <a href="#cc_toolchain"> cc_toolchain </a> </li> <li> <a href="#cc_toolchain_suite"> cc_toolchain_suite </a> </li> </ul> <h2 id="cc_binary"> cc_binary </h2> <a class="button button-with-icon" href="https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcBinaryRule.java" target="_blank"> View rule source<span class="material-icons icon-after" aria-hidden="true">open_in_new</span> </a> <pre class="rule-signature">cc_binary(<a href="#cc_binary.name">name</a>, <a href="#cc_binary.deps">deps</a>, <a href="#cc_binary.srcs">srcs</a>, <a href="common-definitions.html#typical.data">data</a>, <a href="#cc_binary.additional_linker_inputs">additional_linker_inputs</a>, <a href="common-definitions.html#binary.args">args</a>, <a href="common-definitions.html#common.compatible_with">compatible_with</a>, <a href="#cc_binary.copts">copts</a>, <a href="#cc_binary.defines">defines</a>, <a href="common-definitions.html#common.deprecation">deprecation</a>, <a href="common-definitions.html#common.distribs">distribs</a>, <a href="common-definitions.html#binary.env">env</a>, <a href="common-definitions.html#common.exec_compatible_with">exec_compatible_with</a>, <a href="common-definitions.html#common.exec_properties">exec_properties</a>, <a href="common-definitions.html#common.features">features</a>, <a href="#cc_binary.includes">includes</a>, <a href="common-definitions.html#typical.licenses">licenses</a>, <a href="#cc_binary.link_extra_lib">link_extra_lib</a>, <a href="#cc_binary.linkopts">linkopts</a>, <a href="#cc_binary.linkshared">linkshared</a>, <a href="#cc_binary.linkstatic">linkstatic</a>, <a href="#cc_binary.local_defines">local_defines</a>, <a href="#cc_binary.malloc">malloc</a>, <a href="#cc_binary.nocopts">nocopts</a>, <a href="common-definitions.html#binary.output_licenses">output_licenses</a>, <a href="common-definitions.html#common.restricted_to">restricted_to</a>, <a href="#cc_binary.stamp">stamp</a>, <a href="common-definitions.html#common.tags">tags</a>, <a href="common-definitions.html#common.target_compatible_with">target_compatible_with</a>, <a href="common-definitions.html#common.testonly">testonly</a>, <a href="common-definitions.html#common.toolchains">toolchains</a>, <a href="common-definitions.html#common.visibility">visibility</a>, <a href="#cc_binary.win_def_file">win_def_file</a>)</pre> <h4 id="cc_binary_implicit_outputs">Implicit output targets</h4> <ul> <li><code><var>name</var>.stripped</code> (only built if explicitly requested): A stripped version of the binary. <code>strip -g</code> is run on the binary to remove debug symbols. Additional strip options can be provided on the command line using <code>--stripopt=-foo</code>. This output is only built if explicitly requested.</li> <li><code><var>name</var>.dwp</code> (only built if explicitly requested): If <a href="https://gcc.gnu.org/wiki/DebugFission">Fission</a> is enabled: a debug information package file suitable for debugging remotely deployed binaries. Else: an empty file.</li> </ul> <h3 id="cc_binary_args">Arguments</h3> <table class="table table-condensed table-bordered table-params"> <colgroup> <col class="col-param" /> <col class="param-description" /> </colgroup> <thead> <tr> <th colspan="2">Attributes</th> </tr> </thead> <tbody> <tr> <td id="cc_binary.name"><code>name</code></td> <td> <p><a href="/versions/7.6.0/concepts/labels#target-names">Name</a>; required</p> <p>A unique name for this target.</p> </td>
</tr>
<tr>
<td id="cc_binary.deps">
<code>deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of other libraries to be linked in to the binary target.
<p>These can be <code>cc_library</code> or <code>objc_library</code>
targets.</p>
</td>
</tr>
<tr>
<td id="cc_binary.srcs">
<code>srcs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of C and C++ files that are processed to create the target.
These are C/C++ source and header files, either non-generated (normal source
code) or generated.
<p>All <code>.cc</code>, <code>.c</code>, and <code>.cpp</code> files will
be compiled. These might be generated files: if a named file is in
the <code>outs</code> of some other rule, this rule
will automatically depend on that other rule.
</p>
<p>A <code>.h</code> file will not be compiled, but will be available for
inclusion by sources in this rule. Both <code>.cc</code> and
<code>.h</code> files can directly include headers listed in
these <code>srcs</code> or in the <code>hdrs</code> of any rule listed in
the <code>deps</code> argument.
</p>
<p>All <code>#include</code>d files must be mentioned in the
<code>srcs</code> attribute of this rule, or in the
<code>hdrs</code> attribute of referenced <code>cc_library()</code>s.
The recommended style is for headers associated with a library to be
listed in that library's <code>hdrs</code> attribute, and any remaining
headers associated with this rule's sources to be listed in
<code>srcs</code>. See <a href="#hdrs">"Header inclusion checking"</a>
for a more detailed description.
</p>
<p>If a rule's name is in the <code>srcs</code>,
then this rule automatically depends on that one.
If the named rule's <code>outs</code> are C or C++
source files, they are compiled into this rule;
if they are library files, they are linked in.
</p>
<p>
Permitted <code>srcs</code> file types:
</p>
<ul>
<li>C and C++ source files: <code>.c</code>, <code>.cc</code>, <code>.cpp</code>,
<code>.cxx</code>, <code>.c++</code>, <code>.C</code></li>
<li>C and C++ header files: <code>.h</code>, <code>.hh</code>, <code>.hpp</code>,
<code>.hxx</code>, <code>.inc</code>, <code>.inl</code>, <code>.H</code></li>
<li>Assembler with C preprocessor: <code>.S</code></li>
<li>Archive: <code>.a</code>, <code>.pic.a</code></li>
<li>"Always link" library: <code>.lo</code>, <code>.pic.lo</code></li>
<li>Shared library, versioned or unversioned: <code>.so</code>,
<code>.so.<i>version</i></code></li>
<li>Object file: <code>.o</code>, <code>.pic.o</code></li>
</ul>
<p>
...and any rules that produce those files.
Different extensions denote different programming languages in
accordance with gcc convention.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.additional_linker_inputs">
<code>additional_linker_inputs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
Pass these files to the C++ linker command.
<p>
For example, compiled Windows .res files can be provided here to be embedded in
the binary target.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.copts">
<code>copts</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
Add these options to the C++ compilation command.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make variable"</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
<p>
Each string in this attribute is added in the given order to <code>COPTS</code> before
compiling the binary target. The flags take effect only for compiling this target, not
its dependencies, so be careful about header files included elsewhere. All paths should
be relative to the workspace, not to the current package.
</p>
<p>
If the package declares the <a href="/versions/7.6.0/reference/be/functions.html#package.features">feature</a>
<code>no_copts_tokenization</code>, Bourne shell tokenization applies only to strings
that consist of a single "Make" variable.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.defines">
<code>defines</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of defines to add to the compile line.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
Each string, which must consist of a single Bourne shell token,
is prepended with <code>-D</code> and added to the compile command line to this target,
as well as to every rule that depends on it. Be very careful, since this may have
far-reaching effects. When in doubt, add define values to
<a href="#cc_binary.local_defines"><code>local_defines</code></a> instead.
</td>
</tr>
<tr>
<td id="cc_binary.includes">
<code>includes</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of include dirs to be added to the compile line.
<p>
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make variable"</a> substitution.
Each string is prepended with <code>-isystem</code> and added to <code>COPTS</code>.
Unlike <a href="#cc_binary.copts">COPTS</a>, these flags are added for this rule
and every rule that depends on it. (Note: not the rules it depends upon!) Be
very careful, since this may have far-reaching effects. When in doubt, add
"-I" flags to <a href="#cc_binary.copts">COPTS</a> instead.
</p>
<p>
Headers must be added to srcs or hdrs, otherwise they will not be available to dependent
rules when compilation is sandboxed (the default).
</p>
</td>
</tr>
<tr>
<td id="cc_binary.link_extra_lib">
<code>link_extra_lib</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>"@bazel_tools//tools/cpp:link_extra_lib"</code></p>
Control linking of extra libraries.
<p>
By default, C++ binaries are linked against <code>//tools/cpp:link_extra_lib</code>,
which by default depends on the label flag <code>//tools/cpp:link_extra_libs</code>.
Without setting the flag, this library is empty by default. Setting the label flag
allows linking optional dependencies, such as overrides for weak symbols, interceptors
for shared library functions, or special runtime libraries (for malloc replacements,
prefer <code>malloc</code> or <code>--custom_malloc</code>). Setting this attribute to
<code>None</code> disables this behaviour.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.linkopts">
<code>linkopts</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
Add these flags to the C++ linker command.
Subject to <a href="make-variables.html">"Make" variable</a> substitution,
<a href="common-definitions.html#sh-tokenization">
Bourne shell tokenization</a> and
<a href="common-definitions.html#label-expansion">label expansion</a>.
Each string in this attribute is added to <code>LINKOPTS</code> before
linking the binary target.
<p>
Each element of this list that does not start with <code>$</code> or <code>-</code> is
assumed to be the label of a target in <code>deps</code>. The
list of files generated by that target is appended to the linker
options. An error is reported if the label is invalid, or is
not declared in <code>deps</code>.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.linkshared">
<code>linkshared</code>
</td>
<td>
<p>Boolean; <a href="common-definitions.html#configurable-attributes">nonconfigurable</a>; default is <code>False</code></p>
Create a shared library.
To enable this attribute, include <code>linkshared=True</code> in your rule. By default
this option is off.
<p>
The presence of this flag means that linking occurs with the <code>-shared</code> flag
to <code>gcc</code>, and the resulting shared library is suitable for loading into for
example a Java program. However, for build purposes it will never be linked into the
dependent binary, as it is assumed that shared libraries built with a
<a href="#cc_binary">cc_binary</a> rule are only loaded manually by other programs, so
it should not be considered a substitute for the <a href="#cc_library">cc_library</a>
rule. For sake of scalability we recommend avoiding this approach altogether and
simply letting <code>java_library</code> depend on <code>cc_library</code> rules
instead.
</p>
<p>
If you specify both <code>linkopts=['-static']</code> and <code>linkshared=True</code>,
you get a single completely self-contained unit. If you specify both
<code>linkstatic=True</code> and <code>linkshared=True</code>, you get a single, mostly
self-contained unit.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.linkstatic">
<code>linkstatic</code>
</td>
<td>
<p>Boolean; default is <code>True</code></p>
For <a href="/versions/7.6.0/reference/be/c-cpp.html#cc_binary"><code>cc_binary</code></a> and
<a href="/versions/7.6.0/reference/be/c-cpp.html#cc_test"><code>cc_test</code></a>: link the binary in static
mode. For <code>cc_library.linkstatic</code>: see below.
<p>
By default this option is on for <code>cc_binary</code> and off for the rest.
</p>
<p>
If enabled and this is a binary or test, this option tells the build tool to link in
<code>.a</code>'s instead of <code>.so</code>'s for user libraries whenever possible.
Some system libraries may still be linked dynamically, as are libraries for which
there is no static library. So the resulting executable will still be dynamically
linked, hence only <i>mostly</i> static.
</p>
<p>There are really three different ways to link an executable:</p>
<ul>
<li> STATIC with fully_static_link feature, in which everything is linked statically;
e.g. "<code>gcc -static foo.o libbar.a libbaz.a -lm</code>".
This mode is enabled by specifying <code>fully_static_link</code> in the
<a href="/versions/7.6.0/reference/be/common-definitions#features"><code>features</code></a> attribute.</li>
<li> STATIC, in which all user libraries are linked statically (if a static
version is available), but where system libraries (excluding C/C++ runtime libraries)
are linked dynamically, e.g. "<code>gcc foo.o libfoo.a libbaz.a -lm</code>".
This mode is enabled by specifying <code>linkstatic=True</code>.</li>
<li> DYNAMIC, in which all libraries are linked dynamically (if a dynamic version is
available), e.g. "<code>gcc foo.o libfoo.so libbaz.so -lm</code>".
This mode is enabled by specifying <code>linkstatic=False</code>.</li>
</ul>
<p>
The <code>linkstatic</code> attribute has a different meaning if used on a
<a href="/versions/7.6.0/reference/be/c-cpp.html#cc_library"><code>cc_library()</code></a> rule.
For a C++ library, <code>linkstatic=True</code> indicates that only
static linking is allowed, so no <code>.so</code> will be produced. linkstatic=False does
not prevent static libraries from being created. The attribute is meant to control the
creation of dynamic libraries.
</p>
<p>
If <code>linkstatic=False</code>, then the build tool will create symlinks to
depended-upon shared libraries in the <code>*.runfiles</code> area.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.local_defines">
<code>local_defines</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of defines to add to the compile line.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
Each string, which must consist of a single Bourne shell token,
is prepended with <code>-D</code> and added to the compile command line for this target,
but not to its dependents.
</td>
</tr>
<tr>
<td id="cc_binary.malloc">
<code>malloc</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>"@bazel_tools//tools/cpp:malloc"</code></p>
Override the default dependency on malloc.
<p>
By default, C++ binaries are linked against <code>//tools/cpp:malloc</code>,
which is an empty library so the binary ends up using libc malloc.
This label must refer to a <code>cc_library</code>. If compilation is for a non-C++
rule, this option has no effect. The value of this attribute is ignored if
<code>linkshared=True</code> is specified.
</p>
</td>
</tr>
<tr>
<td id="cc_binary.nocopts">
<code>nocopts</code>
</td>
<td>
<p>String; default is <code>""</code></p>
Remove matching options from the C++ compilation command.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution.
The value of this attribute is interpreted as a regular expression.
Any preexisting <code>COPTS</code> that match this regular expression
(including values explicitly specified in the rule's <a
href="#cc_binary.copts">copts</a> attribute) will be removed from
<code>COPTS</code> for purposes of compiling this rule.
This attribute should rarely be needed.
</td>
</tr>
<tr>
<td id="cc_binary.stamp">
<code>stamp</code>
</td>
<td>
<p>Integer; default is <code>-1</code></p>
Whether to encode build information into the binary. Possible values:
<ul>
<li>
<code>stamp = 1</code>: Always stamp the build information into the binary, even in
<a href="/versions/7.6.0/docs/user-manual#flag--stamp"><code>--nostamp</code></a> builds. <b>This
setting should be avoided</b>, since it potentially kills remote caching for the
binary and any downstream actions that depend on it.
</li>
<li>
<code>stamp = 0</code>: Always replace build information by constant values. This
gives good build result caching.
</li>
<li>
<code>stamp = -1</code>: Embedding of build information is controlled by the
<a href="/versions/7.6.0/docs/user-manual#flag--stamp"><code>--[no]stamp</code></a> flag.
</li>
</ul>
<p>Stamped binaries are <em>not</em> rebuilt unless their dependencies change.</p>
</td>
</tr>
<tr>
<td id="cc_binary.win_def_file">
<code>win_def_file</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
The Windows DEF file to be passed to linker.
<p>This attribute should only be used when Windows is the target platform.
It can be used to <a href="https://msdn.microsoft.com/en-us/library/d91k01sh.aspx">
export symbols</a> during linking a shared library.</p>
</td>
</tr>
</tbody>
On Unix:
<pre class="code"> cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.a", shared_library = "libmylib.so", ) # first will link to libmylib.a cc_binary( name = "first", srcs = ["first.cc"], deps = [":mylib"], linkstatic = 1, # default value ) # second will link to libmylib.so cc_binary( name = "second", srcs = ["second.cc"], deps = [":mylib"], linkstatic = 0, ) </pre>On Windows:
<pre class="code"> cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.lib", # A normal static library interface_library = "mylib.lib", # An import library for mylib.dll shared_library = "mylib.dll", ) # first will link to libmylib.lib cc_binary( name = "first", srcs = ["first.cc"], deps = [":mylib"], linkstatic = 1, # default value ) # second will link to mylib.dll through mylib.lib cc_binary( name = "second", srcs = ["second.cc"], deps = [":mylib"], linkstatic = 0, ) </pre><code>cc_import</code> supports an include attribute. For example:
<pre class="code"> cc_import( name = "curl_lib", hdrs = glob(["vendor/curl/include/curl/*.h"]), includes = [ "vendor/curl/include" ], shared_library = "vendor/curl/lib/.libs/libcurl.dylib", ) </pre> </p> <h3 id="cc_import_args">Arguments</h3> <table class="table table-condensed table-bordered table-params"> <colgroup> <col class="col-param" /> <col class="param-description" /> </colgroup> <thead> <tr> <th colspan="2">Attributes</th> </tr> </thead> <tbody> <tr> <td id="cc_import.name"><code>name</code></td> <td> <p><a href="/versions/7.6.0/concepts/labels#target-names">Name</a>; required</p> <p>A unique name for this target.</p> </td>
</tr>
<tr>
<td id="cc_import.deps">
<code>deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of other libraries that the target depends upon.
See general comments about <code>deps</code>
at <a href="/versions/7.6.0/reference/be/common-definitions#typical-attributes">Typical attributes defined by
most build rules</a>.
</td>
</tr>
<tr>
<td id="cc_import.hdrs">
<code>hdrs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of header files published by
this precompiled library to be directly included by sources in dependent rules.
</td>
</tr>
<tr>
<td id="cc_import.alwayslink">
<code>alwayslink</code>
</td>
<td>
<p>Boolean; default is <code>False</code></p>
If 1, any binary that depends (directly or indirectly) on this C++
precompiled library will link in all the object files archived in the static library,
even if some contain no symbols referenced by the binary.
This is useful if your code isn't explicitly called by code in
the binary, e.g., if your code registers to receive some callback
provided by some service.
<p>If alwayslink doesn't work with VS 2017 on Windows, that is due to a
<a href="https://github.com/bazelbuild/bazel/issues/3949">known issue</a>,
please upgrade your VS 2017 to the latest version.</p>
</td>
</tr>
<tr>
<td id="cc_import.interface_library">
<code>interface_library</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
A single interface library for linking the shared library.
<p> Permitted file types:
<code>.ifso</code>,
<code>.tbd</code>,
<code>.lib</code>,
<code>.so</code>
or <code>.dylib</code>
</p>
</td>
</tr>
<tr>
<td id="cc_import.shared_library">
<code>shared_library</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
A single precompiled shared library. Bazel ensures it is available to the
binary that depends on it during runtime.
<p> Permitted file types:
<code>.so</code>,
<code>.dll</code>
or <code>.dylib</code>
</p>
</td>
</tr>
<tr>
<td id="cc_import.static_library">
<code>static_library</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
A single precompiled static library.
<p> Permitted file types:
<code>.a</code>,
<code>.pic.a</code>
or <code>.lib</code>
</p>
</td>
</tr>
<tr>
<td id="cc_import.system_provided">
<code>system_provided</code>
</td>
<td>
<p>Boolean; default is <code>False</code></p>
If 1, it indicates the shared library required at runtime is provided by the system. In
this case, <code>interface_library</code> should be specified and
<code>shared_library</code> should be empty.
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="cc_library.deps">
<code>deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of other libraries to be linked in to the binary target.
<p>These can be <code>cc_library</code> or <code>objc_library</code>
targets.</p>
</td>
</tr>
<tr>
<td id="cc_library.srcs">
<code>srcs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of C and C++ files that are processed to create the target.
These are C/C++ source and header files, either non-generated (normal source
code) or generated.
<p>All <code>.cc</code>, <code>.c</code>, and <code>.cpp</code> files will
be compiled. These might be generated files: if a named file is in
the <code>outs</code> of some other rule, this rule
will automatically depend on that other rule.
</p>
<p>A <code>.h</code> file will not be compiled, but will be available for
inclusion by sources in this rule. Both <code>.cc</code> and
<code>.h</code> files can directly include headers listed in
these <code>srcs</code> or in the <code>hdrs</code> of any rule listed in
the <code>deps</code> argument.
</p>
<p>All <code>#include</code>d files must be mentioned in the
<code>srcs</code> attribute of this rule, or in the
<code>hdrs</code> attribute of referenced <code>cc_library()</code>s.
The recommended style is for headers associated with a library to be
listed in that library's <code>hdrs</code> attribute, and any remaining
headers associated with this rule's sources to be listed in
<code>srcs</code>. See <a href="#hdrs">"Header inclusion checking"</a>
for a more detailed description.
</p>
<p>If a rule's name is in the <code>srcs</code>,
then this rule automatically depends on that one.
If the named rule's <code>outs</code> are C or C++
source files, they are compiled into this rule;
if they are library files, they are linked in.
</p>
<p>
Permitted <code>srcs</code> file types:
</p>
<ul>
<li>C and C++ source files: <code>.c</code>, <code>.cc</code>, <code>.cpp</code>,
<code>.cxx</code>, <code>.c++</code>, <code>.C</code></li>
<li>C and C++ header files: <code>.h</code>, <code>.hh</code>, <code>.hpp</code>,
<code>.hxx</code>, <code>.inc</code>, <code>.inl</code>, <code>.H</code></li>
<li>Assembler with C preprocessor: <code>.S</code></li>
<li>Archive: <code>.a</code>, <code>.pic.a</code></li>
<li>"Always link" library: <code>.lo</code>, <code>.pic.lo</code></li>
<li>Shared library, versioned or unversioned: <code>.so</code>,
<code>.so.<i>version</i></code></li>
<li>Object file: <code>.o</code>, <code>.pic.o</code></li>
</ul>
<p>
...and any rules that produce those files.
Different extensions denote different programming languages in
accordance with gcc convention.
</p>
</td>
</tr>
<tr>
<td id="cc_library.hdrs">
<code>hdrs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of header files published by
this library to be directly included by sources in dependent rules.
<p>This is the strongly preferred location for declaring header files that
describe the interface for the library. These headers will be made
available for inclusion by sources in this rule or in dependent rules.
Headers not meant to be included by a client of this library should be
listed in the <code>srcs</code> attribute instead, even if they are
included by a published header. See <a href="#hdrs">"Header inclusion
checking"</a> for a more detailed description. </p>
</td>
</tr>
<tr>
<td id="cc_library.additional_compiler_inputs">
<code>additional_compiler_inputs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
Any additional files you might want to pass to the compiler command line, such as sanitizer
ignorelists, for example. Files specified here can then be used in copts with the
$(location) function.
</td>
</tr>
<tr>
<td id="cc_library.additional_linker_inputs">
<code>additional_linker_inputs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
Pass these files to the C++ linker command.
<p>
For example, compiled Windows .res files can be provided here to be embedded in
the binary target.
</p>
</td>
</tr>
<tr>
<td id="cc_library.alwayslink">
<code>alwayslink</code>
</td>
<td>
<p>Boolean; default is <code>False</code></p>
If 1, any binary that depends (directly or indirectly) on this C++
library will link in all the object files for the files listed in
<code>srcs</code>, even if some contain no symbols referenced by the binary.
This is useful if your code isn't explicitly called by code in
the binary, e.g., if your code registers to receive some callback
provided by some service.
<p>If alwayslink doesn't work with VS 2017 on Windows, that is due to a
<a href="https://github.com/bazelbuild/bazel/issues/3949">known issue</a>,
please upgrade your VS 2017 to the latest version.</p>
</td>
</tr>
<tr>
<td id="cc_library.copts">
<code>copts</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
Add these options to the C++ compilation command.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make variable"</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
<p>
Each string in this attribute is added in the given order to <code>COPTS</code> before
compiling the binary target. The flags take effect only for compiling this target, not
its dependencies, so be careful about header files included elsewhere. All paths should
be relative to the workspace, not to the current package.
</p>
<p>
If the package declares the <a href="/versions/7.6.0/reference/be/functions.html#package.features">feature</a>
<code>no_copts_tokenization</code>, Bourne shell tokenization applies only to strings
that consist of a single "Make" variable.
</p>
</td>
</tr>
<tr>
<td id="cc_library.defines">
<code>defines</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of defines to add to the compile line.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
Each string, which must consist of a single Bourne shell token,
is prepended with <code>-D</code> and added to the compile command line to this target,
as well as to every rule that depends on it. Be very careful, since this may have
far-reaching effects. When in doubt, add define values to
<a href="#cc_binary.local_defines"><code>local_defines</code></a> instead.
</td>
</tr>
<tr>
<td id="cc_library.implementation_deps">
<code>implementation_deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of other libraries that the library target depends on. Unlike with
<code>deps</code>, the headers and include paths of these libraries (and all their
transitive deps) are only used for compilation of this library, and not libraries that
depend on it. Libraries specified with <code>implementation_deps</code> are still linked in
binary targets that depend on this library.
<p>For now usage is limited to cc_libraries and guarded by the flag
<code>--experimental_cc_implementation_deps</code>.</p>
</td>
</tr>
<tr>
<td id="cc_library.include_prefix">
<code>include_prefix</code>
</td>
<td>
<p>String; default is <code>""</code></p>
The prefix to add to the paths of the headers of this rule.
<p>When set, the headers in the <code>hdrs</code> attribute of this rule are accessible
at is the value of this attribute prepended to their repository-relative path.
<p>The prefix in the <code>strip_include_prefix</code> attribute is removed before this
prefix is added.
</td>
</tr>
<tr>
<td id="cc_library.includes">
<code>includes</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of include dirs to be added to the compile line.
<p>
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make variable"</a> substitution.
Each string is prepended with <code>-isystem</code> and added to <code>COPTS</code>.
Unlike <a href="#cc_binary.copts">COPTS</a>, these flags are added for this rule
and every rule that depends on it. (Note: not the rules it depends upon!) Be
very careful, since this may have far-reaching effects. When in doubt, add
"-I" flags to <a href="#cc_binary.copts">COPTS</a> instead.
</p>
<p>
Headers must be added to srcs or hdrs, otherwise they will not be available to dependent
rules when compilation is sandboxed (the default).
</p>
</td>
</tr>
<tr>
<td id="cc_library.linkopts">
<code>linkopts</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
Add these flags to the C++ linker command.
Subject to <a href="make-variables.html">"Make" variable</a> substitution,
<a href="common-definitions.html#sh-tokenization">
Bourne shell tokenization</a> and
<a href="common-definitions.html#label-expansion">label expansion</a>.
Each string in this attribute is added to <code>LINKOPTS</code> before
linking the binary target.
<p>
Each element of this list that does not start with <code>$</code> or <code>-</code> is
assumed to be the label of a target in <code>deps</code>. The
list of files generated by that target is appended to the linker
options. An error is reported if the label is invalid, or is
not declared in <code>deps</code>.
</p>
</td>
</tr>
<tr>
<td id="cc_library.linkstamp">
<code>linkstamp</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Simultaneously compiles and links the specified C++ source file into the final
binary. This trickery is required to introduce timestamp
information into binaries; if we compiled the source file to an
object file in the usual way, the timestamp would be incorrect.
A linkstamp compilation may not include any particular set of
compiler flags and so should not depend on any particular
header, compiler option, or other build variable.
<em class='harmful'>This option should only be needed in the
<code>base</code> package.</em>
</td>
</tr>
<tr>
<td id="cc_library.linkstatic">
<code>linkstatic</code>
</td>
<td>
<p>Boolean; default is <code>False</code></p>
For <a href="/versions/7.6.0/reference/be/c-cpp.html#cc_binary"><code>cc_binary</code></a> and
<a href="/versions/7.6.0/reference/be/c-cpp.html#cc_test"><code>cc_test</code></a>: link the binary in static
mode. For <code>cc_library.linkstatic</code>: see below.
<p>
By default this option is on for <code>cc_binary</code> and off for the rest.
</p>
<p>
If enabled and this is a binary or test, this option tells the build tool to link in
<code>.a</code>'s instead of <code>.so</code>'s for user libraries whenever possible.
Some system libraries may still be linked dynamically, as are libraries for which
there is no static library. So the resulting executable will still be dynamically
linked, hence only <i>mostly</i> static.
</p>
<p>There are really three different ways to link an executable:</p>
<ul>
<li> STATIC with fully_static_link feature, in which everything is linked statically;
e.g. "<code>gcc -static foo.o libbar.a libbaz.a -lm</code>".
This mode is enabled by specifying <code>fully_static_link</code> in the
<a href="/versions/7.6.0/reference/be/common-definitions#features"><code>features</code></a> attribute.</li>
<li> STATIC, in which all user libraries are linked statically (if a static
version is available), but where system libraries (excluding C/C++ runtime libraries)
are linked dynamically, e.g. "<code>gcc foo.o libfoo.a libbaz.a -lm</code>".
This mode is enabled by specifying <code>linkstatic=True</code>.</li>
<li> DYNAMIC, in which all libraries are linked dynamically (if a dynamic version is
available), e.g. "<code>gcc foo.o libfoo.so libbaz.so -lm</code>".
This mode is enabled by specifying <code>linkstatic=False</code>.</li>
</ul>
<p>
The <code>linkstatic</code> attribute has a different meaning if used on a
<a href="/versions/7.6.0/reference/be/c-cpp.html#cc_library"><code>cc_library()</code></a> rule.
For a C++ library, <code>linkstatic=True</code> indicates that only
static linking is allowed, so no <code>.so</code> will be produced. linkstatic=False does
not prevent static libraries from being created. The attribute is meant to control the
creation of dynamic libraries.
</p>
<p>
If <code>linkstatic=False</code>, then the build tool will create symlinks to
depended-upon shared libraries in the <code>*.runfiles</code> area.
</p>
</td>
</tr>
<tr>
<td id="cc_library.local_defines">
<code>local_defines</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of defines to add to the compile line.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
Each string, which must consist of a single Bourne shell token,
is prepended with <code>-D</code> and added to the compile command line for this target,
but not to its dependents.
</td>
</tr>
<tr>
<td id="cc_library.nocopts">
<code>nocopts</code>
</td>
<td>
<p>String; default is <code>""</code></p>
Remove matching options from the C++ compilation command.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution.
The value of this attribute is interpreted as a regular expression.
Any preexisting <code>COPTS</code> that match this regular expression
(including values explicitly specified in the rule's <a
href="#cc_binary.copts">copts</a> attribute) will be removed from
<code>COPTS</code> for purposes of compiling this rule.
This attribute should rarely be needed.
</td>
</tr>
<tr>
<td id="cc_library.strip_include_prefix">
<code>strip_include_prefix</code>
</td>
<td>
<p>String; default is <code>""</code></p>
The prefix to strip from the paths of the headers of this rule.
<p>When set, the headers in the <code>hdrs</code> attribute of this rule are accessible
at their path with this prefix cut off.
<p>If it's a relative path, it's taken as a package-relative one. If it's an absolute one,
it's understood as a repository-relative path.
<p>The prefix in the <code>include_prefix</code> attribute is added after this prefix is
stripped.
</td>
</tr>
<tr>
<td id="cc_library.textual_hdrs">
<code>textual_hdrs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of header files published by
this library to be textually included by sources in dependent rules.
<p>This is the location for declaring header files that cannot be compiled on their own;
that is, they always need to be textually included by other source files to build valid
code.</p>
</td>
</tr>
<tr>
<td id="cc_library.win_def_file">
<code>win_def_file</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
The Windows DEF file to be passed to linker.
<p>This attribute should only be used when Windows is the target platform.
It can be used to <a href="https://msdn.microsoft.com/en-us/library/d91k01sh.aspx">
export symbols</a> during linking a shared library.</p>
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="cc_proto_library.deps">
<code>deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of <a href="protocol-buffer.html#proto_library"><code>proto_library</code></a>
rules to generate C++ code for.
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="cc_shared_library.deps">
<code>deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
Top level libraries that will unconditionally be statically linked into the shared library
after being whole-archived.
<p>
Any transitive library dependency of these direct deps will be linked into this shared
library as long as they have not already been linked by a <code>cc_shared_library</code>
in <code>dynamic_deps</code>.</p>
<p>
During analysis, the rule implementation will consider any target listed in
<code>deps</code> as being exported by the shared library in order to give errors when
multiple <code>cc_shared_libraries</code> export the same targets. The rule implementation
does not take care of informing the linker about which symbols should be exported by the
shared object. The user should take care of this via linker scripts or visibility
declarations in the source code.</p>
<p>
The implementation will also trigger errors whenever the same library is linked statically
into more than one <code>cc_shared_library</code>. This can be avoided by adding
<code>"LINKABLE_MORE_THAN_ONCE"</code> to the <code>cc_library.tags</code> or by listing
the `cc_library` as an export of one of the shared libraries so that one can be made a
<code>dynamic_dep</code> of the other.
</p>
</td>
</tr>
<tr>
<td id="cc_shared_library.additional_linker_inputs">
<code>additional_linker_inputs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
Any additional files that you may want to pass to the linker, for example, linker scripts.
You have to separately pass any linker flags that the linker needs in order to be aware
of this file. You can do so via the <code>user_link_flags</code> attribute.
</td>
</tr>
<tr>
<td id="cc_shared_library.dynamic_deps">
<code>dynamic_deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
These are other <code>cc_shared_library</code> dependencies the current target depends on.
<p>
The <code>cc_shared_library</code> implementation will use the list of
<code>dynamic_deps</code> (transitively, i.e. also the <code>dynamic_deps</code> of the
current target's <code>dynamic_deps</code>) to decide which <code>cc_libraries</code> in
the transitive <code>deps</code> should not be linked in because they are already provided
by a different <code>cc_shared_library</code>.
</p>
</td>
</tr>
<tr>
<td id="cc_shared_library.exports_filter">
<code>exports_filter</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
This attribute contains a list of targets that are claimed to be exported by the current
shared library.
<p>
Any target <code>deps</code> is already understood to be exported by the shared library.
This attribute should be used to list any targets that are exported by the shared library
but are transitive dependencies of <code>deps</code>.
</p>
<p>
Note that this attribute is not actually adding a dependency edge to those targets, the
dependency edge should instead be created by <code>deps</code>.The entries in this
attribute are just strings. Keep in mind that when placing a target in this attribute,
this is considered a claim that the shared library exports the symbols from that target.
The <code>cc_shared_library</code> logic doesn't actually handle telling the linker which
symbols should be exported.
</p>
<p>The following syntax is allowed:</p>
<p><code>//foo:__package__</code> to account for any target in foo/BUILD</p>
<p><code>//foo:__subpackages__</code> to account for any target in foo/BUILD or any other
package below foo/ like foo/bar/BUILD</p>
</td>
</tr>
<tr>
<td id="cc_shared_library.shared_lib_name">
<code>shared_lib_name</code>
</td>
<td>
<p>String; default is <code>""</code></p>
By default cc_shared_library will use a name for the shared library output file based on
the target's name and the platform. This includes an extension and sometimes a prefix.
Sometimes you may not want the default name, for example, when loading C++ shared libraries
for Python the default lib* prefix is often not desired, in which case you can use this
attribute to choose a custom name.
</td>
</tr>
<tr>
<td id="cc_shared_library.user_link_flags">
<code>user_link_flags</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
Any additional flags that you may want to pass to the linker. For example, to make the
linker aware of a linker script passed via additional_linker_inputs you can use the
following:
<pre class="code">
cc_shared_library(
name = "foo_shared",
additional_linker_inputs = select({
"//src/conditions:linux": [
":foo.lds",
":additional_script.txt",
],
"//conditions:default": []}),
user_link_flags = select({
"//src/conditions:linux": [
"-Wl,-rpath,kittens",
"-Wl,--version-script=$(location :foo.lds)",
"-Wl,--script=$(location :additional_script.txt)",
],
"//conditions:default": []}),
...
)
</pre>
</td>
</tr>
<tr>
<td id="cc_shared_library.win_def_file">
<code>win_def_file</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
The Windows DEF file to be passed to linker.
<p>This attribute should only be used when Windows is the target platform.
It can be used to <a href="https://msdn.microsoft.com/en-us/library/d91k01sh.aspx">
export symbols</a> during linking a shared library.</p>
</td>
</tr>
</tbody>
Produces a static library from a list of targets and their transitive dependencies.
<p>The resulting static library contains the object files of the targets listed in <code>deps</code> as well as their transitive dependencies, with preference given to <code>PIC</code> objects.</p> <h4 id="cc_static_library_output_groups">Output groups</h4> <h5><code>linkdeps</code></h5> <p>A text file containing the labels of those transitive dependencies of targets listed in <code>deps</code> that did not contribute any object files to the static library, but do provide at least one static, dynamic or interface library. The resulting static library may require these libraries to be available at link time.</p> <h5><code>linkopts</code></h5> <p>A text file containing the user-provided <code>linkopts</code> of all transitive dependencies of targets listed in <code>deps</code>. <h4 id="cc_static_library_symbol_check">Duplicate symbols</h4> <p>By default, the <code>cc_static_library</code> rule checks that the resulting static library does not contain any duplicate symbols. If it does, the build fails with an error message that lists the duplicate symbols and the object files containing them.</p> <p>This check can be disabled per target or per package by setting <code>features = ["-symbol_check"]</code> or globally via <code>--features=-symbol_check</code>.</p> <h5 id="cc_static_library_symbol_check_toolchain">Toolchain support for <code>symbol_check</code></h5> <p>The auto-configured C++ toolchains shipped with Bazel support the <code>symbol_check</code> feature on all platforms. Custom toolchains can add support for it in one of two ways:</p> <ul> <li>Implementing the <code>ACTION_NAMES.validate_static_library</code> action and enabling it with the <code>symbol_check</code> feature. The tool set in the action is invoked with two arguments, the static library to check for duplicate symbols and the path of a file that must be created if the check passes.</li> <li>Having the <code>symbol_check</code> feature add archiver flags that cause the action creating the static library to fail on duplicate symbols.</li> </ul> <h3 id="cc_static_library_args">Arguments</h3> <table class="table table-condensed table-bordered table-params"> <colgroup> <col class="col-param" /> <col class="param-description" /> </colgroup> <thead> <tr> <th colspan="2">Attributes</th> </tr> </thead> <tbody> <tr> <td id="cc_static_library.name"><code>name</code></td> <td> <p><a href="/versions/7.6.0/concepts/labels#target-names">Name</a>; required</p> <p>A unique name for this target.</p> </td>
</tr>
<tr>
<td id="cc_static_library.deps">
<code>deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of targets to combine into a static library, including all their transitive
dependencies.
<p>Dependencies that do not provide any object files are not included in the static
library, but their labels are collected in the file provided by the
<code>linkdeps</code> output group.</p>
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="fdo_prefetch_hints.profile">
<code>profile</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Label of the hints profile. The hints file has the .afdo extension
The label can also point to an fdo_absolute_path_profile rule.
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="fdo_profile.absolute_path_profile">
<code>absolute_path_profile</code>
</td>
<td>
<p>String; default is <code>""</code></p>
Absolute path to the FDO profile. The FDO file may only have the .afdo extension.
</td>
</tr>
<tr>
<td id="fdo_profile.profile">
<code>profile</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Label of the FDO profile or a rule which generates it. The FDO file can have one of the
following extensions: .profraw for unindexed LLVM profile, .profdata for indexed LLVM
profile, .zip that holds an LLVM profraw profile, .afdo for AutoFDO profile, .xfdo for
XBinary profile. The label can also point to an fdo_absolute_path_profile rule.
</td>
</tr>
<tr>
<td id="fdo_profile.proto_profile">
<code>proto_profile</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Label of the protobuf profile.
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="memprof_profile.absolute_path_profile">
<code>absolute_path_profile</code>
</td>
<td>
<p>String; default is <code>""</code></p>
Absolute path to the MEMPROF profile. The file may only have a .profdata or
.zip extension (where the zipfile must contain a memprof.profdata file).
</td>
</tr>
<tr>
<td id="memprof_profile.profile">
<code>profile</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Label of the MEMPROF profile. The profile is expected to have
either a .profdata extension (for an indexed/symbolized memprof
profile), or a .zip extension for a zipfile containing a memprof.profdata
file.
The label can also point to an fdo_absolute_path_profile rule.
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="propeller_optimize.ld_profile">
<code>ld_profile</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Label of the profile passed to the link action. This file has
the .txt extension.
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="cc_test.deps">
<code>deps</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of other libraries to be linked in to the binary target.
<p>These can be <code>cc_library</code> or <code>objc_library</code>
targets.</p>
</td>
</tr>
<tr>
<td id="cc_test.srcs">
<code>srcs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
The list of C and C++ files that are processed to create the target.
These are C/C++ source and header files, either non-generated (normal source
code) or generated.
<p>All <code>.cc</code>, <code>.c</code>, and <code>.cpp</code> files will
be compiled. These might be generated files: if a named file is in
the <code>outs</code> of some other rule, this rule
will automatically depend on that other rule.
</p>
<p>A <code>.h</code> file will not be compiled, but will be available for
inclusion by sources in this rule. Both <code>.cc</code> and
<code>.h</code> files can directly include headers listed in
these <code>srcs</code> or in the <code>hdrs</code> of any rule listed in
the <code>deps</code> argument.
</p>
<p>All <code>#include</code>d files must be mentioned in the
<code>srcs</code> attribute of this rule, or in the
<code>hdrs</code> attribute of referenced <code>cc_library()</code>s.
The recommended style is for headers associated with a library to be
listed in that library's <code>hdrs</code> attribute, and any remaining
headers associated with this rule's sources to be listed in
<code>srcs</code>. See <a href="#hdrs">"Header inclusion checking"</a>
for a more detailed description.
</p>
<p>If a rule's name is in the <code>srcs</code>,
then this rule automatically depends on that one.
If the named rule's <code>outs</code> are C or C++
source files, they are compiled into this rule;
if they are library files, they are linked in.
</p>
<p>
Permitted <code>srcs</code> file types:
</p>
<ul>
<li>C and C++ source files: <code>.c</code>, <code>.cc</code>, <code>.cpp</code>,
<code>.cxx</code>, <code>.c++</code>, <code>.C</code></li>
<li>C and C++ header files: <code>.h</code>, <code>.hh</code>, <code>.hpp</code>,
<code>.hxx</code>, <code>.inc</code>, <code>.inl</code>, <code>.H</code></li>
<li>Assembler with C preprocessor: <code>.S</code></li>
<li>Archive: <code>.a</code>, <code>.pic.a</code></li>
<li>"Always link" library: <code>.lo</code>, <code>.pic.lo</code></li>
<li>Shared library, versioned or unversioned: <code>.so</code>,
<code>.so.<i>version</i></code></li>
<li>Object file: <code>.o</code>, <code>.pic.o</code></li>
</ul>
<p>
...and any rules that produce those files.
Different extensions denote different programming languages in
accordance with gcc convention.
</p>
</td>
</tr>
<tr>
<td id="cc_test.additional_linker_inputs">
<code>additional_linker_inputs</code>
</td>
<td>
<p>List of <a href="/versions/7.6.0/concepts/labels">labels</a>; default is <code>[]</code></p>
Pass these files to the C++ linker command.
<p>
For example, compiled Windows .res files can be provided here to be embedded in
the binary target.
</p>
</td>
</tr>
<tr>
<td id="cc_test.copts">
<code>copts</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
Add these options to the C++ compilation command.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make variable"</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
<p>
Each string in this attribute is added in the given order to <code>COPTS</code> before
compiling the binary target. The flags take effect only for compiling this target, not
its dependencies, so be careful about header files included elsewhere. All paths should
be relative to the workspace, not to the current package.
</p>
<p>
If the package declares the <a href="/versions/7.6.0/reference/be/functions.html#package.features">feature</a>
<code>no_copts_tokenization</code>, Bourne shell tokenization applies only to strings
that consist of a single "Make" variable.
</p>
</td>
</tr>
<tr>
<td id="cc_test.defines">
<code>defines</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of defines to add to the compile line.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
Each string, which must consist of a single Bourne shell token,
is prepended with <code>-D</code> and added to the compile command line to this target,
as well as to every rule that depends on it. Be very careful, since this may have
far-reaching effects. When in doubt, add define values to
<a href="#cc_binary.local_defines"><code>local_defines</code></a> instead.
</td>
</tr>
<tr>
<td id="cc_test.includes">
<code>includes</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of include dirs to be added to the compile line.
<p>
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make variable"</a> substitution.
Each string is prepended with <code>-isystem</code> and added to <code>COPTS</code>.
Unlike <a href="#cc_binary.copts">COPTS</a>, these flags are added for this rule
and every rule that depends on it. (Note: not the rules it depends upon!) Be
very careful, since this may have far-reaching effects. When in doubt, add
"-I" flags to <a href="#cc_binary.copts">COPTS</a> instead.
</p>
<p>
Headers must be added to srcs or hdrs, otherwise they will not be available to dependent
rules when compilation is sandboxed (the default).
</p>
</td>
</tr>
<tr>
<td id="cc_test.link_extra_lib">
<code>link_extra_lib</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>"@bazel_tools//tools/cpp:link_extra_lib"</code></p>
Control linking of extra libraries.
<p>
By default, C++ binaries are linked against <code>//tools/cpp:link_extra_lib</code>,
which by default depends on the label flag <code>//tools/cpp:link_extra_libs</code>.
Without setting the flag, this library is empty by default. Setting the label flag
allows linking optional dependencies, such as overrides for weak symbols, interceptors
for shared library functions, or special runtime libraries (for malloc replacements,
prefer <code>malloc</code> or <code>--custom_malloc</code>). Setting this attribute to
<code>None</code> disables this behaviour.
</p>
</td>
</tr>
<tr>
<td id="cc_test.linkopts">
<code>linkopts</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
Add these flags to the C++ linker command.
Subject to <a href="make-variables.html">"Make" variable</a> substitution,
<a href="common-definitions.html#sh-tokenization">
Bourne shell tokenization</a> and
<a href="common-definitions.html#label-expansion">label expansion</a>.
Each string in this attribute is added to <code>LINKOPTS</code> before
linking the binary target.
<p>
Each element of this list that does not start with <code>$</code> or <code>-</code> is
assumed to be the label of a target in <code>deps</code>. The
list of files generated by that target is appended to the linker
options. An error is reported if the label is invalid, or is
not declared in <code>deps</code>.
</p>
</td>
</tr>
<tr>
<td id="cc_test.linkstatic">
<code>linkstatic</code>
</td>
<td>
<p>Boolean; default is <code>False</code></p>
For <a href="/versions/7.6.0/reference/be/c-cpp.html#cc_binary"><code>cc_binary</code></a> and
<a href="/versions/7.6.0/reference/be/c-cpp.html#cc_test"><code>cc_test</code></a>: link the binary in static
mode. For <code>cc_library.linkstatic</code>: see below.
<p>
By default this option is on for <code>cc_binary</code> and off for the rest.
</p>
<p>
If enabled and this is a binary or test, this option tells the build tool to link in
<code>.a</code>'s instead of <code>.so</code>'s for user libraries whenever possible.
Some system libraries may still be linked dynamically, as are libraries for which
there is no static library. So the resulting executable will still be dynamically
linked, hence only <i>mostly</i> static.
</p>
<p>There are really three different ways to link an executable:</p>
<ul>
<li> STATIC with fully_static_link feature, in which everything is linked statically;
e.g. "<code>gcc -static foo.o libbar.a libbaz.a -lm</code>".
This mode is enabled by specifying <code>fully_static_link</code> in the
<a href="/versions/7.6.0/reference/be/common-definitions#features"><code>features</code></a> attribute.</li>
<li> STATIC, in which all user libraries are linked statically (if a static
version is available), but where system libraries (excluding C/C++ runtime libraries)
are linked dynamically, e.g. "<code>gcc foo.o libfoo.a libbaz.a -lm</code>".
This mode is enabled by specifying <code>linkstatic=True</code>.</li>
<li> DYNAMIC, in which all libraries are linked dynamically (if a dynamic version is
available), e.g. "<code>gcc foo.o libfoo.so libbaz.so -lm</code>".
This mode is enabled by specifying <code>linkstatic=False</code>.</li>
</ul>
<p>
The <code>linkstatic</code> attribute has a different meaning if used on a
<a href="/versions/7.6.0/reference/be/c-cpp.html#cc_library"><code>cc_library()</code></a> rule.
For a C++ library, <code>linkstatic=True</code> indicates that only
static linking is allowed, so no <code>.so</code> will be produced. linkstatic=False does
not prevent static libraries from being created. The attribute is meant to control the
creation of dynamic libraries.
</p>
<p>
If <code>linkstatic=False</code>, then the build tool will create symlinks to
depended-upon shared libraries in the <code>*.runfiles</code> area.
</p>
</td>
</tr>
<tr>
<td id="cc_test.local_defines">
<code>local_defines</code>
</td>
<td>
<p>List of strings; default is <code>[]</code></p>
List of defines to add to the compile line.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution and
<a href="/versions/7.6.0/reference/be/common-definitions#sh-tokenization">Bourne shell tokenization</a>.
Each string, which must consist of a single Bourne shell token,
is prepended with <code>-D</code> and added to the compile command line for this target,
but not to its dependents.
</td>
</tr>
<tr>
<td id="cc_test.malloc">
<code>malloc</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>"@bazel_tools//tools/cpp:malloc"</code></p>
Override the default dependency on malloc.
<p>
By default, C++ binaries are linked against <code>//tools/cpp:malloc</code>,
which is an empty library so the binary ends up using libc malloc.
This label must refer to a <code>cc_library</code>. If compilation is for a non-C++
rule, this option has no effect. The value of this attribute is ignored if
<code>linkshared=True</code> is specified.
</p>
</td>
</tr>
<tr>
<td id="cc_test.nocopts">
<code>nocopts</code>
</td>
<td>
<p>String; default is <code>""</code></p>
Remove matching options from the C++ compilation command.
Subject to <a href="/versions/7.6.0/reference/be/make-variables">"Make" variable</a> substitution.
The value of this attribute is interpreted as a regular expression.
Any preexisting <code>COPTS</code> that match this regular expression
(including values explicitly specified in the rule's <a
href="#cc_binary.copts">copts</a> attribute) will be removed from
<code>COPTS</code> for purposes of compiling this rule.
This attribute should rarely be needed.
</td>
</tr>
<tr>
<td id="cc_test.stamp">
<code>stamp</code>
</td>
<td>
<p>Integer; default is <code>0</code></p>
Whether to encode build information into the binary. Possible values:
<ul>
<li>
<code>stamp = 1</code>: Always stamp the build information into the binary, even in
<a href="/versions/7.6.0/docs/user-manual#flag--stamp"><code>--nostamp</code></a> builds. <b>This
setting should be avoided</b>, since it potentially kills remote caching for the
binary and any downstream actions that depend on it.
</li>
<li>
<code>stamp = 0</code>: Always replace build information by constant values. This
gives good build result caching.
</li>
<li>
<code>stamp = -1</code>: Embedding of build information is controlled by the
<a href="/versions/7.6.0/docs/user-manual#flag--stamp"><code>--[no]stamp</code></a> flag.
</li>
</ul>
<p>Stamped binaries are <em>not</em> rebuilt unless their dependencies change.</p>
</td>
</tr>
<tr>
<td id="cc_test.win_def_file">
<code>win_def_file</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
The Windows DEF file to be passed to linker.
<p>This attribute should only be used when Windows is the target platform.
It can be used to <a href="https://msdn.microsoft.com/en-us/library/d91k01sh.aspx">
export symbols</a> during linking a shared library.</p>
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="cc_toolchain.all_files">
<code>all_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; required</p>
Collection of all cc_toolchain artifacts. These artifacts will be added as inputs to all
rules_cc related actions (with the exception of actions that are using more precise sets of
artifacts from attributes below). Bazel assumes that <code>all_files</code> is a superset
of all other artifact-providing attributes (e.g. linkstamp compilation needs both compile
and link files, so it takes <code>all_files</code>).
<p>
This is what <code>cc_toolchain.files</code> contains, and this is used by all Starlark
rules using C++ toolchain.</p>
</td>
</tr>
<tr>
<td id="cc_toolchain.ar_files">
<code>ar_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
<p>Collection of all cc_toolchain artifacts required for archiving actions.</p>
</td>
</tr>
<tr>
<td id="cc_toolchain.as_files">
<code>as_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
<p>Collection of all cc_toolchain artifacts required for assembly actions.</p>
</td>
</tr>
<tr>
<td id="cc_toolchain.compiler_files">
<code>compiler_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; required</p>
Collection of all cc_toolchain artifacts required for compile actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.compiler_files_without_includes">
<code>compiler_files_without_includes</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Collection of all cc_toolchain artifacts required for compile actions in case when
input discovery is supported (currently Google-only).
</td>
</tr>
<tr>
<td id="cc_toolchain.coverage_files">
<code>coverage_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Collection of all cc_toolchain artifacts required for coverage actions. If not specified,
all_files are used.
</td>
</tr>
<tr>
<td id="cc_toolchain.dwp_files">
<code>dwp_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; required</p>
Collection of all cc_toolchain artifacts required for dwp actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.dynamic_runtime_lib">
<code>dynamic_runtime_lib</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Dynamic library artifact for the C++ runtime library (e.g. libstdc++.so).
<p>This will be used when 'static_link_cpp_runtimes' feature is enabled, and we're linking
dependencies dynamically.</p>
</td>
</tr>
<tr>
<td id="cc_toolchain.exec_transition_for_inputs">
<code>exec_transition_for_inputs</code>
</td>
<td>
<p>Boolean; default is <code>True</code></p>
Set to True to build all file inputs to cc_toolchain for the exec platform,
instead of having no transition (i.e. target platform by default).
</td>
</tr>
<tr>
<td id="cc_toolchain.libc_top">
<code>libc_top</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
A collection of artifacts for libc passed as inputs to compile/linking actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.linker_files">
<code>linker_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; required</p>
Collection of all cc_toolchain artifacts required for linking actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.module_map">
<code>module_map</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Module map artifact to be used for modular builds.
</td>
</tr>
<tr>
<td id="cc_toolchain.objcopy_files">
<code>objcopy_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; required</p>
Collection of all cc_toolchain artifacts required for objcopy actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.static_runtime_lib">
<code>static_runtime_lib</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; default is <code>None</code></p>
Static library artifact for the C++ runtime library (e.g. libstdc++.a).
<p>This will be used when 'static_link_cpp_runtimes' feature is enabled, and we're linking
dependencies statically.</p>
</td>
</tr>
<tr>
<td id="cc_toolchain.strip_files">
<code>strip_files</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; required</p>
Collection of all cc_toolchain artifacts required for strip actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.supports_header_parsing">
<code>supports_header_parsing</code>
</td>
<td>
<p>Boolean; default is <code>False</code></p>
Set to True when cc_toolchain supports header parsing actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.supports_param_files">
<code>supports_param_files</code>
</td>
<td>
<p>Boolean; default is <code>True</code></p>
Set to True when cc_toolchain supports using param files for linking actions.
</td>
</tr>
<tr>
<td id="cc_toolchain.toolchain_config">
<code>toolchain_config</code>
</td>
<td>
<p><a href="/versions/7.6.0/concepts/labels">Label</a>; required</p>
The label of the rule providing <code>cc_toolchain_config_info</code>.
</td>
</tr>
<tr>
<td id="cc_toolchain.toolchain_identifier">
<code>toolchain_identifier</code>
</td>
<td>
<p>String; <a href="common-definitions.html#configurable-attributes">nonconfigurable</a>; default is <code>""</code></p>
The identifier used to match this cc_toolchain with the corresponding
crosstool_config.toolchain.
<p>
Until issue <a href="https://github.com/bazelbuild/bazel/issues/5380">#5380</a> is fixed
this is the recommended way of associating <code>cc_toolchain</code> with
<code>CROSSTOOL.toolchain</code>. It will be replaced by the <code>toolchain_config</code>
attribute (<a href="https://github.com/bazelbuild/bazel/issues/5380">#5380</a>).</p>
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td id="cc_toolchain_suite.toolchains">
<code>toolchains</code>
</td>
<td>
<p>Dictionary mapping strings to <a href="/versions/7.6.0/concepts/labels">labels</a>; <a href="common-definitions.html#configurable-attributes">nonconfigurable</a>; required</p>
A map from "<cpu>" or "<cpu>|<compiler>" strings to
a <code>cc_toolchain</code> label. "<cpu>" will be used when only <code>--cpu</code>
is passed to Bazel, and "<cpu>|<compiler>" will be used when both
<code>--cpu</code> and <code>--compiler</code> are passed to Bazel. Example:
<p>
<pre>
cc_toolchain_suite(
name = "toolchain",
toolchains = {
"piii|gcc": ":my_cc_toolchain_for_piii_using_gcc",
"piii": ":my_cc_toolchain_for_piii_using_default_compiler",
},
)
</pre>
</p>
</td>
</tr>
</tbody>