1<html lang="en"> 2<head> 3<title>Constant string objects - 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="Objective_002dC.html#Objective_002dC" title="Objective-C"> 9<link rel="prev" href="Garbage-Collection.html#Garbage-Collection" title="Garbage Collection"> 10<link rel="next" href="compatibility_005falias.html#compatibility_005falias" title="compatibility_alias"> 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="Constant-string-objects"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="compatibility_005falias.html#compatibility_005falias">compatibility_alias</a>, 53Previous: <a rel="previous" accesskey="p" href="Garbage-Collection.html#Garbage-Collection">Garbage Collection</a>, 54Up: <a rel="up" accesskey="u" href="Objective_002dC.html#Objective_002dC">Objective-C</a> 55<hr> 56</div> 57 58<h3 class="section">8.5 Constant string objects</h3> 59 60<p>GNU Objective-C provides constant string objects that are generated 61directly by the compiler. You declare a constant string object by 62prefixing a C constant string with the character ‘<samp><span class="samp">@</span></samp>’: 63 64<pre class="smallexample"> id myString = @"this is a constant string object"; 65</pre> 66 <p>The constant string objects are by default instances of the 67<code>NXConstantString</code> class which is provided by the GNU Objective-C 68runtime. To get the definition of this class you must include the 69<samp><span class="file">objc/NXConstStr.h</span></samp> header file. 70 71 <p>User defined libraries may want to implement their own constant string 72class. To be able to support them, the GNU Objective-C compiler provides 73a new command line options <samp><span class="option">-fconstant-string-class=</span><var>class-name</var></samp>. 74The provided class should adhere to a strict structure, the same 75as <code>NXConstantString</code>'s structure: 76 77<pre class="smallexample"> 78 @interface MyConstantStringClass 79 { 80 Class isa; 81 char *c_string; 82 unsigned int len; 83 } 84 @end 85 86</pre> 87 <p><code>NXConstantString</code> inherits from <code>Object</code>; user class 88libraries may choose to inherit the customized constant string class 89from a different class than <code>Object</code>. There is no requirement in 90the methods the constant string class has to implement, but the final 91ivar layout of the class must be the compatible with the given 92structure. 93 94 <p>When the compiler creates the statically allocated constant string 95object, the <code>c_string</code> field will be filled by the compiler with 96the string; the <code>length</code> field will be filled by the compiler with 97the string length; the <code>isa</code> pointer will be filled with 98<code>NULL</code> by the compiler, and it will later be fixed up automatically 99at runtime by the GNU Objective-C runtime library to point to the class 100which was set by the <samp><span class="option">-fconstant-string-class</span></samp> option when the 101object file is loaded (if you wonder how it works behind the scenes, the 102name of the class to use, and the list of static objects to fixup, are 103stored by the compiler in the object file in a place where the GNU 104runtime library will find them at runtime). 105 106 <p>As a result, when a file is compiled with the 107<samp><span class="option">-fconstant-string-class</span></samp> option, all the constant string objects 108will be instances of the class specified as argument to this option. It 109is possible to have multiple compilation units referring to different 110constant string classes, neither the compiler nor the linker impose any 111restrictions in doing this. 112 113<!-- ========================================================================= --> 114 </body></html> 115 116