1<html lang="en"> 2<head> 3<title>Vague Linkage - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" title="C++ Extensions"> 9<link rel="prev" href="Restricted-Pointers.html#Restricted-Pointers" title="Restricted Pointers"> 10<link rel="next" href="C_002b_002b-Interface.html#C_002b_002b-Interface" title="C++ Interface"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Funding Free Software'', the Front-Cover 19Texts being (a) (see below), and with the Back-Cover Texts being (b) 20(see below). A copy of the license is included in the section entitled 21``GNU Free Documentation License''. 22 23(a) The FSF's Front-Cover Text is: 24 25 A GNU Manual 26 27(b) The FSF's Back-Cover Text is: 28 29 You have freedom to copy and modify this GNU Manual, like GNU 30 software. Copies published by the Free Software Foundation raise 31 funds for GNU development.--> 32<meta http-equiv="Content-Style-Type" content="text/css"> 33<style type="text/css"><!-- 34 pre.display { font-family:inherit } 35 pre.format { font-family:inherit } 36 pre.smalldisplay { font-family:inherit; font-size:smaller } 37 pre.smallformat { font-family:inherit; font-size:smaller } 38 pre.smallexample { font-size:smaller } 39 pre.smalllisp { font-size:smaller } 40 span.sc { font-variant:small-caps } 41 span.roman { font-family:serif; font-weight:normal; } 42 span.sansserif { font-family:sans-serif; font-weight:normal; } 43--></style> 44<link rel="stylesheet" type="text/css" href="../cs.css"> 45</head> 46<body> 47<div class="node"> 48<a name="Vague-Linkage"></a> 49<p> 50Next: <a rel="next" accesskey="n" href="C_002b_002b-Interface.html#C_002b_002b-Interface">C++ Interface</a>, 51Previous: <a rel="previous" accesskey="p" href="Restricted-Pointers.html#Restricted-Pointers">Restricted Pointers</a>, 52Up: <a rel="up" accesskey="u" href="C_002b_002b-Extensions.html#C_002b_002b-Extensions">C++ Extensions</a> 53<hr> 54</div> 55 56<h3 class="section">7.3 Vague Linkage</h3> 57 58<p><a name="index-vague-linkage-3508"></a> 59There are several constructs in C++ that require space in the object 60file but are not clearly tied to a single translation unit. We say that 61these constructs have “vague linkage”. Typically such constructs are 62emitted wherever they are needed, though sometimes we can be more 63clever. 64 65 <dl> 66<dt>Inline Functions<dd>Inline functions are typically defined in a header file which can be 67included in many different compilations. Hopefully they can usually be 68inlined, but sometimes an out-of-line copy is necessary, if the address 69of the function is taken or if inlining fails. In general, we emit an 70out-of-line copy in all translation units where one is needed. As an 71exception, we only emit inline virtual functions with the vtable, since 72it always requires a copy. 73 74 <p>Local static variables and string constants used in an inline function 75are also considered to have vague linkage, since they must be shared 76between all inlined and out-of-line instances of the function. 77 78 <br><dt>VTables<dd><a name="index-vtable-3509"></a>C++ virtual functions are implemented in most compilers using a lookup 79table, known as a vtable. The vtable contains pointers to the virtual 80functions provided by a class, and each object of the class contains a 81pointer to its vtable (or vtables, in some multiple-inheritance 82situations). If the class declares any non-inline, non-pure virtual 83functions, the first one is chosen as the “key method” for the class, 84and the vtable is only emitted in the translation unit where the key 85method is defined. 86 87 <p><em>Note:</em> If the chosen key method is later defined as inline, the 88vtable is still emitted in every translation unit that defines it. 89Make sure that any inline virtuals are declared inline in the class 90body, even if they are not defined there. 91 92 <br><dt><code>type_info</code> objects<dd><a name="index-g_t_0040code_007btype_005finfo_007d-3510"></a><a name="index-RTTI-3511"></a>C++ requires information about types to be written out in order to 93implement ‘<samp><span class="samp">dynamic_cast</span></samp>’, ‘<samp><span class="samp">typeid</span></samp>’ and exception handling. 94For polymorphic classes (classes with virtual functions), the ‘<samp><span class="samp">type_info</span></samp>’ 95object is written out along with the vtable so that ‘<samp><span class="samp">dynamic_cast</span></samp>’ 96can determine the dynamic type of a class object at run time. For all 97other types, we write out the ‘<samp><span class="samp">type_info</span></samp>’ object when it is used: when 98applying ‘<samp><span class="samp">typeid</span></samp>’ to an expression, throwing an object, or 99referring to a type in a catch clause or exception specification. 100 101 <br><dt>Template Instantiations<dd>Most everything in this section also applies to template instantiations, 102but there are other options as well. 103See <a href="Template-Instantiation.html#Template-Instantiation">Where's the Template?</a>. 104 105 </dl> 106 107 <p>When used with GNU ld version 2.8 or later on an ELF system such as 108GNU/Linux or Solaris 2, or on Microsoft Windows, duplicate copies of 109these constructs will be discarded at link time. This is known as 110COMDAT support. 111 112 <p>On targets that don't support COMDAT, but do support weak symbols, GCC 113uses them. This way one copy overrides all the others, but 114the unused copies still take up space in the executable. 115 116 <p>For targets that do not support either COMDAT or weak symbols, 117most entities with vague linkage are emitted as local symbols to 118avoid duplicate definition errors from the linker. This does not happen 119for local statics in inlines, however, as having multiple copies 120almost certainly breaks things. 121 122 <p>See <a href="C_002b_002b-Interface.html#C_002b_002b-Interface">Declarations and Definitions in One Header</a>, for 123another way to control placement of these constructs. 124 125 </body></html> 126 127