1<html lang="en"> 2<head> 3<title>Executing code before main - 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="GNU-Objective_002dC-runtime-API.html#GNU-Objective_002dC-runtime-API" title="GNU Objective-C runtime API"> 10<link rel="next" href="Type-encoding.html#Type-encoding" title="Type encoding"> 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="Executing-code-before-main"></a> 49<p> 50Next: <a rel="next" accesskey="n" href="Type-encoding.html#Type-encoding">Type encoding</a>, 51Previous: <a rel="previous" accesskey="p" href="GNU-Objective_002dC-runtime-API.html#GNU-Objective_002dC-runtime-API">GNU Objective-C runtime API</a>, 52Up: <a rel="up" accesskey="u" href="Objective_002dC.html#Objective_002dC">Objective-C</a> 53<hr> 54</div> 55 56<h3 class="section">8.2 <code>+load</code>: Executing code before main</h3> 57 58<p>This section is specific for the GNU Objective-C runtime. If you are 59using a different runtime, you can skip it. 60 61 <p>The GNU Objective-C runtime provides a way that allows you to execute 62code before the execution of the program enters the <code>main</code> 63function. The code is executed on a per-class and a per-category basis, 64through a special class method <code>+load</code>. 65 66 <p>This facility is very useful if you want to initialize global variables 67which can be accessed by the program directly, without sending a message 68to the class first. The usual way to initialize global variables, in the 69<code>+initialize</code> method, might not be useful because 70<code>+initialize</code> is only called when the first message is sent to a 71class object, which in some cases could be too late. 72 73 <p>Suppose for example you have a <code>FileStream</code> class that declares 74<code>Stdin</code>, <code>Stdout</code> and <code>Stderr</code> as global variables, like 75below: 76 77<pre class="smallexample"> 78 FileStream *Stdin = nil; 79 FileStream *Stdout = nil; 80 FileStream *Stderr = nil; 81 82 @implementation FileStream 83 84 + (void)initialize 85 { 86 Stdin = [[FileStream new] initWithFd:0]; 87 Stdout = [[FileStream new] initWithFd:1]; 88 Stderr = [[FileStream new] initWithFd:2]; 89 } 90 91 /* <span class="roman">Other methods here</span> */ 92 @end 93 94</pre> 95 <p>In this example, the initialization of <code>Stdin</code>, <code>Stdout</code> and 96<code>Stderr</code> in <code>+initialize</code> occurs too late. The programmer can 97send a message to one of these objects before the variables are actually 98initialized, thus sending messages to the <code>nil</code> object. The 99<code>+initialize</code> method which actually initializes the global 100variables is not invoked until the first message is sent to the class 101object. The solution would require these variables to be initialized 102just before entering <code>main</code>. 103 104 <p>The correct solution of the above problem is to use the <code>+load</code> 105method instead of <code>+initialize</code>: 106 107<pre class="smallexample"> 108 @implementation FileStream 109 110 + (void)load 111 { 112 Stdin = [[FileStream new] initWithFd:0]; 113 Stdout = [[FileStream new] initWithFd:1]; 114 Stderr = [[FileStream new] initWithFd:2]; 115 } 116 117 /* <span class="roman">Other methods here</span> */ 118 @end 119 120</pre> 121 <p>The <code>+load</code> is a method that is not overridden by categories. If a 122class and a category of it both implement <code>+load</code>, both methods are 123invoked. This allows some additional initializations to be performed in 124a category. 125 126 <p>This mechanism is not intended to be a replacement for <code>+initialize</code>. 127You should be aware of its limitations when you decide to use it 128instead of <code>+initialize</code>. 129 130<ul class="menu"> 131<li><a accesskey="1" href="What-you-can-and-what-you-cannot-do-in-_002bload.html#What-you-can-and-what-you-cannot-do-in-_002bload">What you can and what you cannot do in +load</a> 132</ul> 133 134 </body></html> 135 136