OXIESEC PANEL
- Current Dir:
/
/
usr
/
local
/
doc
/
cmake
/
html
/
command
Server IP: 139.59.38.164
Upload:
Create Dir:
Name
Size
Modified
Perms
π
..
-
04/04/2023 07:10:48 PM
rwxr-xr-x
π
add_compile_definitions.html
8.97 KB
04/04/2023 07:04:57 PM
rw-r--r--
π
add_compile_options.html
13.11 KB
04/04/2023 07:04:57 PM
rw-r--r--
π
add_custom_command.html
67.2 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_custom_target.html
26.7 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_definitions.html
9.72 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_dependencies.html
8.79 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_executable.html
20.23 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_library.html
43.25 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_link_options.html
17.79 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_subdirectory.html
8.85 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
add_test.html
17.09 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
aux_source_directory.html
6.45 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
block.html
13.15 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
break.html
5.97 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
build_command.html
10.66 KB
04/04/2023 07:04:58 PM
rw-r--r--
π
build_name.html
6.43 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
cmake_host_system_information.html
38.46 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
cmake_language.html
59.74 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
cmake_minimum_required.html
13.56 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
cmake_parse_arguments.html
18.62 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
cmake_path.html
80.45 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
cmake_policy.html
23.14 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
configure_file.html
20.36 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
continue.html
6.15 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
create_test_sourcelist.html
7.61 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_build.html
14.87 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_configure.html
10.28 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_coverage.html
9.93 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_empty_binary_directory.html
5.54 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_memcheck.html
11.73 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_read_custom_files.html
5.78 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_run_script.html
6.72 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_sleep.html
5.66 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_start.html
14.68 KB
04/04/2023 07:04:59 PM
rw-r--r--
π
ctest_submit.html
16.18 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
ctest_test.html
38.64 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
ctest_update.html
8.79 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
ctest_upload.html
6.7 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
define_property.html
15.53 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
else.html
5.35 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
elseif.html
5.44 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
enable_language.html
10.08 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
enable_testing.html
6.04 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
endblock.html
5.66 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
endforeach.html
5.88 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
endfunction.html
5.85 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
endif.html
5.71 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
endmacro.html
5.79 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
endwhile.html
5.79 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
exec_program.html
7.52 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
execute_process.html
20.95 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
export.html
19.49 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
export_library_dependencies.html
8.01 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
file.html
128.25 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
find_file.html
39.65 KB
04/04/2023 07:05:00 PM
rw-r--r--
π
find_library.html
46.52 KB
04/04/2023 07:05:01 PM
rw-r--r--
π
find_package.html
91.29 KB
04/04/2023 07:05:01 PM
rw-r--r--
π
find_path.html
40.22 KB
04/04/2023 07:05:01 PM
rw-r--r--
π
find_program.html
37.17 KB
04/04/2023 07:05:01 PM
rw-r--r--
π
fltk_wrap_ui.html
5.8 KB
04/04/2023 07:05:01 PM
rw-r--r--
π
foreach.html
18.65 KB
04/04/2023 07:05:01 PM
rw-r--r--
π
function.html
12.42 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
get_cmake_property.html
8.05 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
get_directory_property.html
9.08 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
get_filename_component.html
12.57 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
get_property.html
15.95 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
get_source_file_property.html
11.47 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
get_target_property.html
9.1 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
get_test_property.html
8.06 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
if.html
41.89 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
include.html
8.04 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
include_directories.html
10.54 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
include_external_msproject.html
8.14 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
include_guard.html
8.85 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
include_regular_expression.html
6.14 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
install.html
126.44 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
install_files.html
9.13 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
install_programs.html
9.09 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
install_targets.html
7 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
link_directories.html
11.99 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
link_libraries.html
7.36 KB
04/04/2023 07:05:02 PM
rw-r--r--
π
list.html
35.79 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
load_cache.html
6.78 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
load_command.html
6.61 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
macro.html
20.49 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
make_directory.html
5.77 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
mark_as_advanced.html
6.73 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
math.html
9.01 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
message.html
27.67 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
option.html
6.48 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
output_required_files.html
6.59 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
project.html
30.84 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
qt_wrap_cpp.html
7.2 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
qt_wrap_ui.html
7.43 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
remove.html
6 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
remove_definitions.html
6.15 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
return.html
16.06 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
separate_arguments.html
11.04 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
set.html
18.35 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
set_directory_properties.html
7.58 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
set_property.html
17.74 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
set_source_files_properties.html
10.63 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
set_target_properties.html
8.33 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
set_tests_properties.html
7.98 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
site_name.html
5.46 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
source_group.html
10.97 KB
04/04/2023 07:05:03 PM
rw-r--r--
π
string.html
64.24 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
subdir_depends.html
5.69 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
subdirs.html
7.51 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_compile_definitions.html
16.09 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_compile_features.html
14.47 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_compile_options.html
18.02 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_include_directories.html
22.39 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_link_directories.html
16.03 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_link_libraries.html
60.83 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_link_options.html
23.66 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_precompile_headers.html
24.42 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
target_sources.html
38.35 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
try_compile.html
57.47 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
try_run.html
24.62 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
unset.html
9.21 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
use_mangled_mesa.html
5.91 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
utility_source.html
6.78 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
variable_requires.html
6.84 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
variable_watch.html
8.98 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
while.html
9.14 KB
04/04/2023 07:05:04 PM
rw-r--r--
π
write_file.html
7.25 KB
04/04/2023 07:05:04 PM
rw-r--r--
Editing: target_link_libraries.html
Close
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" /> <title>target_link_libraries — CMake 3.26.3 Documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/cmake.css" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/_sphinx_javascript_frameworks_compat.js"></script> <script src="../_static/doctools.js"></script> <link rel="shortcut icon" href="../_static/cmake-favicon.ico"/> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="target_link_options" href="target_link_options.html" /> <link rel="prev" title="target_link_directories" href="target_link_directories.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="target_link_options.html" title="target_link_options" accesskey="N">next</a> |</li> <li class="right" > <a href="target_link_directories.html" title="target_link_directories" accesskey="P">previous</a> |</li> <li> <img src="../_static/cmake-logo-16.png" alt="" style="vertical-align: middle; margin-top: -2px" /> </li> <li> <a href="https://cmake.org/">CMake</a> » </li> <li> <a href="../index.html">3.26.3 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="../manual/cmake-commands.7.html" accesskey="U">cmake-commands(7)</a> »</li> <li class="nav-item nav-item-this"><a href="">target_link_libraries</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="target-link-libraries"> <span id="command:target_link_libraries"></span><h1><a class="toc-backref" href="#id2" role="doc-backlink">target_link_libraries</a><a class="headerlink" href="#target-link-libraries" title="Permalink to this heading">ΒΆ</a></h1> <nav class="contents" id="contents"> <p class="topic-title">Contents</p> <ul class="simple"> <li><p><a class="reference internal" href="#target-link-libraries" id="id2">target_link_libraries</a></p> <ul> <li><p><a class="reference internal" href="#overview" id="id3">Overview</a></p></li> <li><p><a class="reference internal" href="#libraries-for-a-target-and-or-its-dependents" id="id4">Libraries for a Target and/or its Dependents</a></p></li> <li><p><a class="reference internal" href="#libraries-for-both-a-target-and-its-dependents" id="id5">Libraries for both a Target and its Dependents</a></p></li> <li><p><a class="reference internal" href="#libraries-for-a-target-and-or-its-dependents-legacy" id="id6">Libraries for a Target and/or its Dependents (Legacy)</a></p></li> <li><p><a class="reference internal" href="#libraries-for-dependents-only-legacy" id="id7">Libraries for Dependents Only (Legacy)</a></p></li> <li><p><a class="reference internal" href="#linking-object-libraries" id="id8">Linking Object Libraries</a></p> <ul> <li><p><a class="reference internal" href="#linking-object-libraries-via-target-objects" id="id9">Linking Object Libraries via <code class="docutils literal notranslate"><span class="pre">$<TARGET_OBJECTS></span></code></a></p></li> </ul> </li> <li><p><a class="reference internal" href="#cyclic-dependencies-of-static-libraries" id="id10">Cyclic Dependencies of Static Libraries</a></p></li> <li><p><a class="reference internal" href="#creating-relocatable-packages" id="id11">Creating Relocatable Packages</a></p></li> <li><p><a class="reference internal" href="#see-also" id="id12">See Also</a></p></li> </ul> </li> </ul> </nav> <p>Specify libraries or flags to use when linking a given target and/or its dependents. <a class="reference internal" href="../manual/cmake-buildsystem.7.html#target-usage-requirements"><span class="std std-ref">Usage requirements</span></a> from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.</p> <section id="overview"> <h2><a class="toc-backref" href="#id3" role="doc-backlink">Overview</a><a class="headerlink" href="#overview" title="Permalink to this heading">ΒΆ</a></h2> <p>This command has several signatures as detailed in subsections below. All of them have the general form</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">target_link_libraries(</span><span class="nv"><target></span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="nv"><item>...</span><span class="w"> </span><span class="p">...</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>The named <code class="docutils literal notranslate"><span class="pre"><target></span></code> must have been created by a command such as <span class="target" id="index-0-command:add_executable"></span><a class="reference internal" href="add_executable.html#command:add_executable" title="add_executable"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">add_executable()</span></code></a> or <span class="target" id="index-0-command:add_library"></span><a class="reference internal" href="add_library.html#command:add_library" title="add_library"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">add_library()</span></code></a> and must not be an <a class="reference internal" href="../manual/cmake-buildsystem.7.html#alias-targets"><span class="std std-ref">ALIAS target</span></a>. If policy <span class="target" id="index-0-policy:CMP0079"></span><a class="reference internal" href="../policy/CMP0079.html#policy:CMP0079" title="CMP0079"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0079</span></code></a> is not set to <code class="docutils literal notranslate"><span class="pre">NEW</span></code> then the target must have been created in the current directory. Repeated calls for the same <code class="docutils literal notranslate"><span class="pre"><target></span></code> append items in the order called.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.13: </span>The <code class="docutils literal notranslate"><span class="pre"><target></span></code> doesn't have to be defined in the same directory as the <code class="docutils literal notranslate"><span class="pre">target_link_libraries</span></code> call.</p> </div> <p>Each <code class="docutils literal notranslate"><span class="pre"><item></span></code> may be:</p> <ul> <li><p><strong>A library target name</strong>: The generated link line will have the full path to the linkable library file associated with the target. The buildsystem will have a dependency to re-link <code class="docutils literal notranslate"><span class="pre"><target></span></code> if the library file changes.</p> <p>The named target must be created by <span class="target" id="index-1-command:add_library"></span><a class="reference internal" href="add_library.html#command:add_library" title="add_library"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">add_library()</span></code></a> within the project or as an <a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">IMPORTED library</span></a>. If it is created within the project an ordering dependency will automatically be added in the build system to make sure the named library target is up-to-date before the <code class="docutils literal notranslate"><span class="pre"><target></span></code> links.</p> <p>If an imported library has the <span class="target" id="index-0-prop_tgt:IMPORTED_NO_SONAME"></span><a class="reference internal" href="../prop_tgt/IMPORTED_NO_SONAME.html#prop_tgt:IMPORTED_NO_SONAME" title="IMPORTED_NO_SONAME"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">IMPORTED_NO_SONAME</span></code></a> target property set, CMake may ask the linker to search for the library instead of using the full path (e.g. <code class="docutils literal notranslate"><span class="pre">/usr/lib/libfoo.so</span></code> becomes <code class="docutils literal notranslate"><span class="pre">-lfoo</span></code>).</p> <p>The full path to the target's artifact will be quoted/escaped for the shell automatically.</p> </li> <li><p><strong>A full path to a library file</strong>: The generated link line will normally preserve the full path to the file. The buildsystem will have a dependency to re-link <code class="docutils literal notranslate"><span class="pre"><target></span></code> if the library file changes.</p> <p>There are some cases where CMake may ask the linker to search for the library (e.g. <code class="docutils literal notranslate"><span class="pre">/usr/lib/libfoo.so</span></code> becomes <code class="docutils literal notranslate"><span class="pre">-lfoo</span></code>), such as when a shared library is detected to have no <code class="docutils literal notranslate"><span class="pre">SONAME</span></code> field. See policy <span class="target" id="index-0-policy:CMP0060"></span><a class="reference internal" href="../policy/CMP0060.html#policy:CMP0060" title="CMP0060"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0060</span></code></a> for discussion of another case.</p> <p>If the library file is in a macOS framework, the <code class="docutils literal notranslate"><span class="pre">Headers</span></code> directory of the framework will also be processed as a <a class="reference internal" href="../manual/cmake-buildsystem.7.html#target-usage-requirements"><span class="std std-ref">usage requirement</span></a>. This has the same effect as passing the framework directory as an include directory.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8: </span>On <a class="reference internal" href="../manual/cmake-generators.7.html#visual-studio-generators"><span class="std std-ref">Visual Studio Generators</span></a> for VS 2010 and above, library files ending in <code class="docutils literal notranslate"><span class="pre">.targets</span></code> will be treated as MSBuild targets files and imported into generated project files. This is not supported by other generators.</p> </div> <p>The full path to the library file will be quoted/escaped for the shell automatically.</p> </li> <li><p><strong>A plain library name</strong>: The generated link line will ask the linker to search for the library (e.g. <code class="docutils literal notranslate"><span class="pre">foo</span></code> becomes <code class="docutils literal notranslate"><span class="pre">-lfoo</span></code> or <code class="docutils literal notranslate"><span class="pre">foo.lib</span></code>).</p> <p>The library name/flag is treated as a command-line string fragment and will be used with no extra quoting or escaping.</p> </li> <li><p><strong>A link flag</strong>: Item names starting with <code class="docutils literal notranslate"><span class="pre">-</span></code>, but not <code class="docutils literal notranslate"><span class="pre">-l</span></code> or <code class="docutils literal notranslate"><span class="pre">-framework</span></code>, are treated as linker flags. Note that such flags will be treated like any other library link item for purposes of transitive dependencies, so they are generally safe to specify only as private link items that will not propagate to dependents.</p> <p>Link flags specified here are inserted into the link command in the same place as the link libraries. This might not be correct, depending on the linker. Use the <span class="target" id="index-0-prop_tgt:LINK_OPTIONS"></span><a class="reference internal" href="../prop_tgt/LINK_OPTIONS.html#prop_tgt:LINK_OPTIONS" title="LINK_OPTIONS"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_OPTIONS</span></code></a> target property or <span class="target" id="index-0-command:target_link_options"></span><a class="reference internal" href="target_link_options.html#command:target_link_options" title="target_link_options"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_options()</span></code></a> command to add link flags explicitly. The flags will then be placed at the toolchain-defined flag position in the link command.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.13: </span><span class="target" id="index-1-prop_tgt:LINK_OPTIONS"></span><a class="reference internal" href="../prop_tgt/LINK_OPTIONS.html#prop_tgt:LINK_OPTIONS" title="LINK_OPTIONS"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_OPTIONS</span></code></a> target property and <span class="target" id="index-1-command:target_link_options"></span><a class="reference internal" href="target_link_options.html#command:target_link_options" title="target_link_options"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_options()</span></code></a> command. For earlier versions of CMake, use <span class="target" id="index-0-prop_tgt:LINK_FLAGS"></span><a class="reference internal" href="../prop_tgt/LINK_FLAGS.html#prop_tgt:LINK_FLAGS" title="LINK_FLAGS"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_FLAGS</span></code></a> property instead.</p> </div> <p>The link flag is treated as a command-line string fragment and will be used with no extra quoting or escaping.</p> </li> <li><p><strong>A generator expression</strong>: A <code class="docutils literal notranslate"><span class="pre">$<...></span></code> <span class="target" id="index-0-manual:cmake-generator-expressions(7)"></span><a class="reference internal" href="../manual/cmake-generator-expressions.7.html#manual:cmake-generator-expressions(7)" title="cmake-generator-expressions(7)"><code class="xref cmake cmake-manual docutils literal notranslate"><span class="pre">generator</span> <span class="pre">expression</span></code></a> may evaluate to any of the above items or to a <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of them. If the <code class="docutils literal notranslate"><span class="pre">...</span></code> contains any <code class="docutils literal notranslate"><span class="pre">;</span></code> characters, e.g. after evaluation of a <code class="docutils literal notranslate"><span class="pre">${list}</span></code> variable, be sure to use an explicitly quoted argument <code class="docutils literal notranslate"><span class="pre">"$<...>"</span></code> so that this command receives it as a single <code class="docutils literal notranslate"><span class="pre"><item></span></code>.</p> <p>Additionally, a generator expression may be used as a fragment of any of the above items, e.g. <code class="docutils literal notranslate"><span class="pre">foo$<1:_d></span></code>.</p> <p>Note that generator expressions will not be used in OLD handling of policy <span class="target" id="index-0-policy:CMP0003"></span><a class="reference internal" href="../policy/CMP0003.html#policy:CMP0003" title="CMP0003"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0003</span></code></a> or policy <span class="target" id="index-0-policy:CMP0004"></span><a class="reference internal" href="../policy/CMP0004.html#policy:CMP0004" title="CMP0004"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0004</span></code></a>.</p> </li> <li><p>A <code class="docutils literal notranslate"><span class="pre">debug</span></code>, <code class="docutils literal notranslate"><span class="pre">optimized</span></code>, or <code class="docutils literal notranslate"><span class="pre">general</span></code> keyword immediately followed by another <code class="docutils literal notranslate"><span class="pre"><item></span></code>. The item following such a keyword will be used only for the corresponding build configuration. The <code class="docutils literal notranslate"><span class="pre">debug</span></code> keyword corresponds to the <code class="docutils literal notranslate"><span class="pre">Debug</span></code> configuration (or to configurations named in the <span class="target" id="index-0-prop_gbl:DEBUG_CONFIGURATIONS"></span><a class="reference internal" href="../prop_gbl/DEBUG_CONFIGURATIONS.html#prop_gbl:DEBUG_CONFIGURATIONS" title="DEBUG_CONFIGURATIONS"><code class="xref cmake cmake-prop_gbl docutils literal notranslate"><span class="pre">DEBUG_CONFIGURATIONS</span></code></a> global property if it is set). The <code class="docutils literal notranslate"><span class="pre">optimized</span></code> keyword corresponds to all other configurations. The <code class="docutils literal notranslate"><span class="pre">general</span></code> keyword corresponds to all configurations, and is purely optional. Higher granularity may be achieved for per-configuration rules by creating and linking to <a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">IMPORTED library targets</span></a>. These keywords are interpreted immediately by this command and therefore have no special meaning when produced by a generator expression.</p></li> </ul> <p>Items containing <code class="docutils literal notranslate"><span class="pre">::</span></code>, such as <code class="docutils literal notranslate"><span class="pre">Foo::Bar</span></code>, are assumed to be <a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">IMPORTED</span></a> or <a class="reference internal" href="../manual/cmake-buildsystem.7.html#alias-targets"><span class="std std-ref">ALIAS</span></a> library target names and will cause an error if no such target exists. See policy <span class="target" id="index-0-policy:CMP0028"></span><a class="reference internal" href="../policy/CMP0028.html#policy:CMP0028" title="CMP0028"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0028</span></code></a>.</p> <p>See the <span class="target" id="index-0-manual:cmake-buildsystem(7)"></span><a class="reference internal" href="../manual/cmake-buildsystem.7.html#manual:cmake-buildsystem(7)" title="cmake-buildsystem(7)"><code class="xref cmake cmake-manual docutils literal notranslate"><span class="pre">cmake-buildsystem(7)</span></code></a> manual for more on defining buildsystem properties.</p> </section> <section id="libraries-for-a-target-and-or-its-dependents"> <h2><a class="toc-backref" href="#id4" role="doc-backlink">Libraries for a Target and/or its Dependents</a><a class="headerlink" href="#libraries-for-a-target-and-or-its-dependents" title="Permalink to this heading">ΒΆ</a></h2> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">target_link_libraries(</span><span class="nv"><target></span><span class="w"></span> <span class="w"> </span><span class="o"><</span><span class="no">PRIVATE</span><span class="p">|</span><span class="no">PUBLIC</span><span class="p">|</span><span class="no">INTERFACE</span><span class="o">></span><span class="w"> </span><span class="nv"><item>...</span><span class="w"></span> <span class="w"> </span><span class="p">[</span><span class="o"><</span><span class="no">PRIVATE</span><span class="p">|</span><span class="no">PUBLIC</span><span class="p">|</span><span class="no">INTERFACE</span><span class="o">></span><span class="w"> </span><span class="nv"><item>...</span><span class="p">]...</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">PUBLIC</span></code>, <code class="docutils literal notranslate"><span class="pre">PRIVATE</span></code> and <code class="docutils literal notranslate"><span class="pre">INTERFACE</span></code> <a class="reference internal" href="../manual/cmake-buildsystem.7.html#target-usage-requirements"><span class="std std-ref">scope</span></a> keywords can be used to specify both the link dependencies and the link interface in one command.</p> <p>Libraries and targets following <code class="docutils literal notranslate"><span class="pre">PUBLIC</span></code> are linked to, and are made part of the link interface. Libraries and targets following <code class="docutils literal notranslate"><span class="pre">PRIVATE</span></code> are linked to, but are not made part of the link interface. Libraries following <code class="docutils literal notranslate"><span class="pre">INTERFACE</span></code> are appended to the link interface and are not used for linking <code class="docutils literal notranslate"><span class="pre"><target></span></code>.</p> </section> <section id="libraries-for-both-a-target-and-its-dependents"> <h2><a class="toc-backref" href="#id5" role="doc-backlink">Libraries for both a Target and its Dependents</a><a class="headerlink" href="#libraries-for-both-a-target-and-its-dependents" title="Permalink to this heading">ΒΆ</a></h2> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">target_link_libraries(</span><span class="nv"><target></span><span class="w"> </span><span class="nv"><item>...</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>Library dependencies are transitive by default with this signature. When this target is linked into another target then the libraries linked to this target will appear on the link line for the other target too. This transitive "link interface" is stored in the <span class="target" id="index-0-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a> target property and may be overridden by setting the property directly. When <span class="target" id="index-0-policy:CMP0022"></span><a class="reference internal" href="../policy/CMP0022.html#policy:CMP0022" title="CMP0022"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0022</span></code></a> is not set to <code class="docutils literal notranslate"><span class="pre">NEW</span></code>, transitive linking is built in but may be overridden by the <span class="target" id="index-0-prop_tgt:LINK_INTERFACE_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/LINK_INTERFACE_LIBRARIES.html#prop_tgt:LINK_INTERFACE_LIBRARIES" title="LINK_INTERFACE_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_INTERFACE_LIBRARIES</span></code></a> property. Calls to other signatures of this command may set the property making any libraries linked exclusively by this signature private.</p> </section> <section id="libraries-for-a-target-and-or-its-dependents-legacy"> <h2><a class="toc-backref" href="#id6" role="doc-backlink">Libraries for a Target and/or its Dependents (Legacy)</a><a class="headerlink" href="#libraries-for-a-target-and-or-its-dependents-legacy" title="Permalink to this heading">ΒΆ</a></h2> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">target_link_libraries(</span><span class="nv"><target></span><span class="w"></span> <span class="w"> </span><span class="o"><</span><span class="no">LINK_PRIVATE</span><span class="p">|</span><span class="no">LINK_PUBLIC</span><span class="o">></span><span class="w"> </span><span class="nv"><lib>...</span><span class="w"></span> <span class="w"> </span><span class="p">[</span><span class="o"><</span><span class="no">LINK_PRIVATE</span><span class="p">|</span><span class="no">LINK_PUBLIC</span><span class="o">></span><span class="w"> </span><span class="nv"><lib>...</span><span class="p">]...</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">LINK_PUBLIC</span></code> and <code class="docutils literal notranslate"><span class="pre">LINK_PRIVATE</span></code> modes can be used to specify both the link dependencies and the link interface in one command.</p> <p>This signature is for compatibility only. Prefer the <code class="docutils literal notranslate"><span class="pre">PUBLIC</span></code> or <code class="docutils literal notranslate"><span class="pre">PRIVATE</span></code> keywords instead.</p> <p>Libraries and targets following <code class="docutils literal notranslate"><span class="pre">LINK_PUBLIC</span></code> are linked to, and are made part of the <span class="target" id="index-1-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a>. If policy <span class="target" id="index-1-policy:CMP0022"></span><a class="reference internal" href="../policy/CMP0022.html#policy:CMP0022" title="CMP0022"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0022</span></code></a> is not <code class="docutils literal notranslate"><span class="pre">NEW</span></code>, they are also made part of the <span class="target" id="index-1-prop_tgt:LINK_INTERFACE_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/LINK_INTERFACE_LIBRARIES.html#prop_tgt:LINK_INTERFACE_LIBRARIES" title="LINK_INTERFACE_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_INTERFACE_LIBRARIES</span></code></a>. Libraries and targets following <code class="docutils literal notranslate"><span class="pre">LINK_PRIVATE</span></code> are linked to, but are not made part of the <span class="target" id="index-2-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a> (or <span class="target" id="index-2-prop_tgt:LINK_INTERFACE_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/LINK_INTERFACE_LIBRARIES.html#prop_tgt:LINK_INTERFACE_LIBRARIES" title="LINK_INTERFACE_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_INTERFACE_LIBRARIES</span></code></a>).</p> </section> <section id="libraries-for-dependents-only-legacy"> <h2><a class="toc-backref" href="#id7" role="doc-backlink">Libraries for Dependents Only (Legacy)</a><a class="headerlink" href="#libraries-for-dependents-only-legacy" title="Permalink to this heading">ΒΆ</a></h2> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">target_link_libraries(</span><span class="nv"><target></span><span class="w"> </span><span class="no">LINK_INTERFACE_LIBRARIES</span><span class="w"> </span><span class="nv"><item>...</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">LINK_INTERFACE_LIBRARIES</span></code> mode appends the libraries to the <span class="target" id="index-3-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a> target property instead of using them for linking. If policy <span class="target" id="index-2-policy:CMP0022"></span><a class="reference internal" href="../policy/CMP0022.html#policy:CMP0022" title="CMP0022"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0022</span></code></a> is not <code class="docutils literal notranslate"><span class="pre">NEW</span></code>, then this mode also appends libraries to the <span class="target" id="index-3-prop_tgt:LINK_INTERFACE_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/LINK_INTERFACE_LIBRARIES.html#prop_tgt:LINK_INTERFACE_LIBRARIES" title="LINK_INTERFACE_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_INTERFACE_LIBRARIES</span></code></a> and its per-configuration equivalent.</p> <p>This signature is for compatibility only. Prefer the <code class="docutils literal notranslate"><span class="pre">INTERFACE</span></code> mode instead.</p> <p>Libraries specified as <code class="docutils literal notranslate"><span class="pre">debug</span></code> are wrapped in a generator expression to correspond to debug builds. If policy <span class="target" id="index-3-policy:CMP0022"></span><a class="reference internal" href="../policy/CMP0022.html#policy:CMP0022" title="CMP0022"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0022</span></code></a> is not <code class="docutils literal notranslate"><span class="pre">NEW</span></code>, the libraries are also appended to the <span class="target" id="index-0-prop_tgt:LINK_INTERFACE_LIBRARIES_<CONFIG>"></span><a class="reference internal" href="../prop_tgt/LINK_INTERFACE_LIBRARIES_CONFIG.html#prop_tgt:LINK_INTERFACE_LIBRARIES_<CONFIG>" title="LINK_INTERFACE_LIBRARIES_<CONFIG>"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_INTERFACE_LIBRARIES_DEBUG</span></code></a> property (or to the properties corresponding to configurations listed in the <span class="target" id="index-1-prop_gbl:DEBUG_CONFIGURATIONS"></span><a class="reference internal" href="../prop_gbl/DEBUG_CONFIGURATIONS.html#prop_gbl:DEBUG_CONFIGURATIONS" title="DEBUG_CONFIGURATIONS"><code class="xref cmake cmake-prop_gbl docutils literal notranslate"><span class="pre">DEBUG_CONFIGURATIONS</span></code></a> global property if it is set). Libraries specified as <code class="docutils literal notranslate"><span class="pre">optimized</span></code> are appended to the <span class="target" id="index-4-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a> property. If policy <span class="target" id="index-4-policy:CMP0022"></span><a class="reference internal" href="../policy/CMP0022.html#policy:CMP0022" title="CMP0022"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0022</span></code></a> is not <code class="docutils literal notranslate"><span class="pre">NEW</span></code>, they are also appended to the <span class="target" id="index-4-prop_tgt:LINK_INTERFACE_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/LINK_INTERFACE_LIBRARIES.html#prop_tgt:LINK_INTERFACE_LIBRARIES" title="LINK_INTERFACE_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_INTERFACE_LIBRARIES</span></code></a> property. Libraries specified as <code class="docutils literal notranslate"><span class="pre">general</span></code> (or without any keyword) are treated as if specified for both <code class="docutils literal notranslate"><span class="pre">debug</span></code> and <code class="docutils literal notranslate"><span class="pre">optimized</span></code>.</p> </section> <section id="linking-object-libraries"> <h2><a class="toc-backref" href="#id8" role="doc-backlink">Linking Object Libraries</a><a class="headerlink" href="#linking-object-libraries" title="Permalink to this heading">ΒΆ</a></h2> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> <p><a class="reference internal" href="../manual/cmake-buildsystem.7.html#object-libraries"><span class="std std-ref">Object Libraries</span></a> may be used as the <code class="docutils literal notranslate"><span class="pre"><target></span></code> (first) argument of <code class="docutils literal notranslate"><span class="pre">target_link_libraries</span></code> to specify dependencies of their sources on other libraries. For example, the code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="no">A</span><span class="w"> </span><span class="no">SHARED</span><span class="w"> </span><span class="nb">a.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_compile_definitions(</span><span class="no">A</span><span class="w"> </span><span class="no">PUBLIC</span><span class="w"> </span><span class="no">A</span><span class="nf">)</span><span class="w"></span> <span class="nf">add_library(</span><span class="nb">obj</span><span class="w"> </span><span class="no">OBJECT</span><span class="w"> </span><span class="nb">obj.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_compile_definitions(</span><span class="nb">obj</span><span class="w"> </span><span class="no">PUBLIC</span><span class="w"> </span><span class="no">OBJ</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">obj</span><span class="w"> </span><span class="no">PUBLIC</span><span class="w"> </span><span class="no">A</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>compiles <code class="docutils literal notranslate"><span class="pre">obj.c</span></code> with <code class="docutils literal notranslate"><span class="pre">-DA</span> <span class="pre">-DOBJ</span></code> and establishes usage requirements for <code class="docutils literal notranslate"><span class="pre">obj</span></code> that propagate to its dependents.</p> <p>Normal libraries and executables may link to <a class="reference internal" href="../manual/cmake-buildsystem.7.html#object-libraries"><span class="std std-ref">Object Libraries</span></a> to get their objects and usage requirements. Continuing the above example, the code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="no">B</span><span class="w"> </span><span class="no">SHARED</span><span class="w"> </span><span class="nb">b.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="no">B</span><span class="w"> </span><span class="no">PUBLIC</span><span class="w"> </span><span class="nb">obj</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>compiles <code class="docutils literal notranslate"><span class="pre">b.c</span></code> with <code class="docutils literal notranslate"><span class="pre">-DA</span> <span class="pre">-DOBJ</span></code>, creates shared library <code class="docutils literal notranslate"><span class="pre">B</span></code> with object files from <code class="docutils literal notranslate"><span class="pre">b.c</span></code> and <code class="docutils literal notranslate"><span class="pre">obj.c</span></code>, and links <code class="docutils literal notranslate"><span class="pre">B</span></code> to <code class="docutils literal notranslate"><span class="pre">A</span></code>. Furthermore, the code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_executable(</span><span class="nb">main</span><span class="w"> </span><span class="nb">main.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">main</span><span class="w"> </span><span class="no">B</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>compiles <code class="docutils literal notranslate"><span class="pre">main.c</span></code> with <code class="docutils literal notranslate"><span class="pre">-DA</span> <span class="pre">-DOBJ</span></code> and links executable <code class="docutils literal notranslate"><span class="pre">main</span></code> to <code class="docutils literal notranslate"><span class="pre">B</span></code> and <code class="docutils literal notranslate"><span class="pre">A</span></code>. The object library's usage requirements are propagated transitively through <code class="docutils literal notranslate"><span class="pre">B</span></code>, but its object files are not.</p> <p><a class="reference internal" href="../manual/cmake-buildsystem.7.html#object-libraries"><span class="std std-ref">Object Libraries</span></a> may "link" to other object libraries to get usage requirements, but since they do not have a link step nothing is done with their object files. Continuing from the above example, the code:</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="nb">obj2</span><span class="w"> </span><span class="no">OBJECT</span><span class="w"> </span><span class="nb">obj2.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">obj2</span><span class="w"> </span><span class="no">PUBLIC</span><span class="w"> </span><span class="nb">obj</span><span class="nf">)</span><span class="w"></span> <span class="nf">add_executable(</span><span class="nb">main2</span><span class="w"> </span><span class="nb">main2.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">main2</span><span class="w"> </span><span class="nb">obj2</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>compiles <code class="docutils literal notranslate"><span class="pre">obj2.c</span></code> with <code class="docutils literal notranslate"><span class="pre">-DA</span> <span class="pre">-DOBJ</span></code>, creates executable <code class="docutils literal notranslate"><span class="pre">main2</span></code> with object files from <code class="docutils literal notranslate"><span class="pre">main2.c</span></code> and <code class="docutils literal notranslate"><span class="pre">obj2.c</span></code>, and links <code class="docutils literal notranslate"><span class="pre">main2</span></code> to <code class="docutils literal notranslate"><span class="pre">A</span></code>.</p> <p>In other words, when <a class="reference internal" href="../manual/cmake-buildsystem.7.html#object-libraries"><span class="std std-ref">Object Libraries</span></a> appear in a target's <span class="target" id="index-5-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a> property they will be treated as <a class="reference internal" href="../manual/cmake-buildsystem.7.html#interface-libraries"><span class="std std-ref">Interface Libraries</span></a>, but when they appear in a target's <span class="target" id="index-0-prop_tgt:LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/LINK_LIBRARIES.html#prop_tgt:LINK_LIBRARIES" title="LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_LIBRARIES</span></code></a> property their object files will be included in the link too.</p> <section id="linking-object-libraries-via-target-objects"> <span id="id1"></span><h3><a class="toc-backref" href="#id9" role="doc-backlink">Linking Object Libraries via <code class="docutils literal notranslate"><span class="pre">$<TARGET_OBJECTS></span></code></a><a class="headerlink" href="#linking-object-libraries-via-target-objects" title="Permalink to this heading">ΒΆ</a></h3> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.21.</span></p> </div> <p>The object files associated with an object library may be referenced by the <span class="target" id="index-0-genex:TARGET_OBJECTS"></span><a class="reference internal" href="../manual/cmake-generator-expressions.7.html#genex:TARGET_OBJECTS" title="TARGET_OBJECTS"><code class="xref cmake cmake-genex docutils literal notranslate"><span class="pre">$<TARGET_OBJECTS></span></code></a> generator expression. Such object files are placed on the link line <em>before</em> all libraries, regardless of their relative order. Additionally, an ordering dependency will be added to the build system to make sure the object library is up-to-date before the dependent target links. For example, the code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="nb">obj3</span><span class="w"> </span><span class="no">OBJECT</span><span class="w"> </span><span class="nb">obj3.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_compile_definitions(</span><span class="nb">obj3</span><span class="w"> </span><span class="no">PUBLIC</span><span class="w"> </span><span class="no">OBJ3</span><span class="nf">)</span><span class="w"></span> <span class="nf">add_executable(</span><span class="nb">main3</span><span class="w"> </span><span class="nb">main3.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">main3</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">a3</span><span class="w"> </span><span class="o">$<</span><span class="no">TARGET_OBJECTS</span><span class="o">:</span><span class="nb">obj3</span><span class="o">></span><span class="w"> </span><span class="nb">b3</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>links executable <code class="docutils literal notranslate"><span class="pre">main3</span></code> with object files from <code class="docutils literal notranslate"><span class="pre">main3.c</span></code> and <code class="docutils literal notranslate"><span class="pre">obj3.c</span></code> followed by the <code class="docutils literal notranslate"><span class="pre">a3</span></code> and <code class="docutils literal notranslate"><span class="pre">b3</span></code> libraries. <code class="docutils literal notranslate"><span class="pre">main3.c</span></code> is <em>not</em> compiled with usage requirements from <code class="docutils literal notranslate"><span class="pre">obj3</span></code>, such as <code class="docutils literal notranslate"><span class="pre">-DOBJ3</span></code>.</p> <p>This approach can be used to achieve transitive inclusion of object files in link lines as usage requirements. Continuing the above example, the code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="nb">iface_obj3</span><span class="w"> </span><span class="no">INTERFACE</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">iface_obj3</span><span class="w"> </span><span class="no">INTERFACE</span><span class="w"> </span><span class="nb">obj3</span><span class="w"> </span><span class="o">$<</span><span class="no">TARGET_OBJECTS</span><span class="o">:</span><span class="nb">obj3</span><span class="o">></span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>creates an interface library <code class="docutils literal notranslate"><span class="pre">iface_obj3</span></code> that forwards the <code class="docutils literal notranslate"><span class="pre">obj3</span></code> usage requirements and adds the <code class="docutils literal notranslate"><span class="pre">obj3</span></code> object files to dependents' link lines. The code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_executable(</span><span class="nb">use_obj3</span><span class="w"> </span><span class="nb">use_obj3.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">use_obj3</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">iface_obj3</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>compiles <code class="docutils literal notranslate"><span class="pre">use_obj3.c</span></code> with <code class="docutils literal notranslate"><span class="pre">-DOBJ3</span></code> and links executable <code class="docutils literal notranslate"><span class="pre">use_obj3</span></code> with object files from <code class="docutils literal notranslate"><span class="pre">use_obj3.c</span></code> and <code class="docutils literal notranslate"><span class="pre">obj3.c</span></code>.</p> <p>This also works transitively through a static library. Since a static library does not link, it does not consume the object files from object libraries referenced this way. Instead, the object files become transitive link dependencies of the static library. Continuing the above example, the code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="nb">static3</span><span class="w"> </span><span class="no">STATIC</span><span class="w"> </span><span class="nb">static3.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">static3</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">iface_obj3</span><span class="nf">)</span><span class="w"></span> <span class="nf">add_executable(</span><span class="nb">use_static3</span><span class="w"> </span><span class="nb">use_static3.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">use_static3</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">static3</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>compiles <code class="docutils literal notranslate"><span class="pre">static3.c</span></code> with <code class="docutils literal notranslate"><span class="pre">-DOBJ3</span></code> and creates <code class="docutils literal notranslate"><span class="pre">libstatic3.a</span></code> using only its own object file. <code class="docutils literal notranslate"><span class="pre">use_static3.c</span></code> is compiled <em>without</em> <code class="docutils literal notranslate"><span class="pre">-DOBJ3</span></code> because the usage requirement is not transitive through the private dependency of <code class="docutils literal notranslate"><span class="pre">static3</span></code>. However, the link dependencies of <code class="docutils literal notranslate"><span class="pre">static3</span></code> are propagated, including the <code class="docutils literal notranslate"><span class="pre">iface_obj3</span></code> reference to <code class="docutils literal notranslate"><span class="pre">$<TARGET_OBJECTS:obj3></span></code>. The <code class="docutils literal notranslate"><span class="pre">use_static3</span></code> executable is created with object files from <code class="docutils literal notranslate"><span class="pre">use_static3.c</span></code> and <code class="docutils literal notranslate"><span class="pre">obj3.c</span></code>, and linked to library <code class="docutils literal notranslate"><span class="pre">libstatic3.a</span></code>.</p> <p>When using this approach, it is the project's responsibility to avoid linking multiple dependent binaries to <code class="docutils literal notranslate"><span class="pre">iface_obj3</span></code>, because they will all get the <code class="docutils literal notranslate"><span class="pre">obj3</span></code> object files on their link lines.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Referencing <span class="target" id="index-1-genex:TARGET_OBJECTS"></span><a class="reference internal" href="../manual/cmake-generator-expressions.7.html#genex:TARGET_OBJECTS" title="TARGET_OBJECTS"><code class="xref cmake cmake-genex docutils literal notranslate"><span class="pre">$<TARGET_OBJECTS></span></code></a> in <code class="docutils literal notranslate"><span class="pre">target_link_libraries</span></code> calls worked in versions of CMake prior to 3.21 for some cases, but was not fully supported:</p> <ul class="simple"> <li><p>It did not place the object files before libraries on link lines.</p></li> <li><p>It did not add an ordering dependency on the object library.</p></li> <li><p>It did not work in Xcode with multiple architectures.</p></li> </ul> </div> </section> </section> <section id="cyclic-dependencies-of-static-libraries"> <h2><a class="toc-backref" href="#id10" role="doc-backlink">Cyclic Dependencies of Static Libraries</a><a class="headerlink" href="#cyclic-dependencies-of-static-libraries" title="Permalink to this heading">ΒΆ</a></h2> <p>The library dependency graph is normally acyclic (a DAG), but in the case of mutually-dependent <code class="docutils literal notranslate"><span class="pre">STATIC</span></code> libraries CMake allows the graph to contain cycles (strongly connected components). When another target links to one of the libraries, CMake repeats the entire connected component. For example, the code</p> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="no">A</span><span class="w"> </span><span class="no">STATIC</span><span class="w"> </span><span class="nb">a.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">add_library(</span><span class="no">B</span><span class="w"> </span><span class="no">STATIC</span><span class="w"> </span><span class="nb">b.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="no">A</span><span class="w"> </span><span class="no">B</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="no">B</span><span class="w"> </span><span class="no">A</span><span class="nf">)</span><span class="w"></span> <span class="nf">add_executable(</span><span class="nb">main</span><span class="w"> </span><span class="nb">main.c</span><span class="nf">)</span><span class="w"></span> <span class="nf">target_link_libraries(</span><span class="nb">main</span><span class="w"> </span><span class="no">A</span><span class="nf">)</span><span class="w"></span> </pre></div> </div> <p>links <code class="docutils literal notranslate"><span class="pre">main</span></code> to <code class="docutils literal notranslate"><span class="pre">A</span> <span class="pre">B</span> <span class="pre">A</span> <span class="pre">B</span></code>. While one repetition is usually sufficient, pathological object file and symbol arrangements can require more. One may handle such cases by using the <span class="target" id="index-0-prop_tgt:LINK_INTERFACE_MULTIPLICITY"></span><a class="reference internal" href="../prop_tgt/LINK_INTERFACE_MULTIPLICITY.html#prop_tgt:LINK_INTERFACE_MULTIPLICITY" title="LINK_INTERFACE_MULTIPLICITY"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_INTERFACE_MULTIPLICITY</span></code></a> target property or by manually repeating the component in the last <code class="docutils literal notranslate"><span class="pre">target_link_libraries</span></code> call. However, if two archives are really so interdependent they should probably be combined into a single archive, perhaps by using <a class="reference internal" href="../manual/cmake-buildsystem.7.html#object-libraries"><span class="std std-ref">Object Libraries</span></a>.</p> </section> <section id="creating-relocatable-packages"> <h2><a class="toc-backref" href="#id11" role="doc-backlink">Creating Relocatable Packages</a><a class="headerlink" href="#creating-relocatable-packages" title="Permalink to this heading">ΒΆ</a></h2> <p>Note that it is not advisable to populate the <span class="target" id="index-7-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="../prop_tgt/INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a> of a target with absolute paths to dependencies. That would hard-code into installed packages the library file paths for dependencies <strong>as found on the machine the package was made on</strong>.</p> <p>See the <a class="reference internal" href="../manual/cmake-packages.7.html#creating-relocatable-packages"><span class="std std-ref">Creating Relocatable Packages</span></a> section of the <span class="target" id="index-0-manual:cmake-packages(7)"></span><a class="reference internal" href="../manual/cmake-packages.7.html#manual:cmake-packages(7)" title="cmake-packages(7)"><code class="xref cmake cmake-manual docutils literal notranslate"><span class="pre">cmake-packages(7)</span></code></a> manual for discussion of additional care that must be taken when specifying usage requirements while creating packages for redistribution.</p> </section> <section id="see-also"> <h2><a class="toc-backref" href="#id12" role="doc-backlink">See Also</a><a class="headerlink" href="#see-also" title="Permalink to this heading">ΒΆ</a></h2> <ul class="simple"> <li><p><span class="target" id="index-0-command:target_compile_definitions"></span><a class="reference internal" href="target_compile_definitions.html#command:target_compile_definitions" title="target_compile_definitions"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_compile_definitions()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_compile_features"></span><a class="reference internal" href="target_compile_features.html#command:target_compile_features" title="target_compile_features"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_compile_features()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_compile_options"></span><a class="reference internal" href="target_compile_options.html#command:target_compile_options" title="target_compile_options"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_compile_options()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_include_directories"></span><a class="reference internal" href="target_include_directories.html#command:target_include_directories" title="target_include_directories"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_include_directories()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_link_directories"></span><a class="reference internal" href="target_link_directories.html#command:target_link_directories" title="target_link_directories"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_directories()</span></code></a></p></li> <li><p><span class="target" id="index-2-command:target_link_options"></span><a class="reference internal" href="target_link_options.html#command:target_link_options" title="target_link_options"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_options()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_precompile_headers"></span><a class="reference internal" href="target_precompile_headers.html#command:target_precompile_headers" title="target_precompile_headers"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_precompile_headers()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_sources"></span><a class="reference internal" href="target_sources.html#command:target_sources" title="target_sources"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_sources()</span></code></a></p></li> </ul> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <div> <h3><a href="../index.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">target_link_libraries</a><ul> <li><a class="reference internal" href="#overview">Overview</a></li> <li><a class="reference internal" href="#libraries-for-a-target-and-or-its-dependents">Libraries for a Target and/or its Dependents</a></li> <li><a class="reference internal" href="#libraries-for-both-a-target-and-its-dependents">Libraries for both a Target and its Dependents</a></li> <li><a class="reference internal" href="#libraries-for-a-target-and-or-its-dependents-legacy">Libraries for a Target and/or its Dependents (Legacy)</a></li> <li><a class="reference internal" href="#libraries-for-dependents-only-legacy">Libraries for Dependents Only (Legacy)</a></li> <li><a class="reference internal" href="#linking-object-libraries">Linking Object Libraries</a><ul> <li><a class="reference internal" href="#linking-object-libraries-via-target-objects">Linking Object Libraries via <code class="docutils literal notranslate"><span class="pre">$<TARGET_OBJECTS></span></code></a></li> </ul> </li> <li><a class="reference internal" href="#cyclic-dependencies-of-static-libraries">Cyclic Dependencies of Static Libraries</a></li> <li><a class="reference internal" href="#creating-relocatable-packages">Creating Relocatable Packages</a></li> <li><a class="reference internal" href="#see-also">See Also</a></li> </ul> </li> </ul> </div> <div> <h4>Previous topic</h4> <p class="topless"><a href="target_link_directories.html" title="previous chapter">target_link_directories</a></p> </div> <div> <h4>Next topic</h4> <p class="topless"><a href="target_link_options.html" title="next chapter">target_link_options</a></p> </div> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/command/target_link_libraries.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3 id="searchlabel">Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/> <input type="submit" value="Go" /> </form> </div> </div> <script>document.getElementById('searchbox').style.display = "block"</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="target_link_options.html" title="target_link_options" >next</a> |</li> <li class="right" > <a href="target_link_directories.html" title="target_link_directories" >previous</a> |</li> <li> <img src="../_static/cmake-logo-16.png" alt="" style="vertical-align: middle; margin-top: -2px" /> </li> <li> <a href="https://cmake.org/">CMake</a> » </li> <li> <a href="../index.html">3.26.3 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="../manual/cmake-commands.7.html" >cmake-commands(7)</a> »</li> <li class="nav-item nav-item-this"><a href="">target_link_libraries</a></li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2000-2023 Kitware, Inc. and Contributors. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.0.2. </div> </body> </html>