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