You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
7.4KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Executing code before main (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Executing code before main (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Executing code before main (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Objective_002dC.html#Objective_002dC" rel="up" title="Objective-C">
  30. <link href="What-you-can-and-what-you-cannot-do-in-_002bload.html#What-you-can-and-what-you-cannot-do-in-_002bload" rel="next" title="What you can and what you cannot do in +load">
  31. <link href="Traditional-GNU-Objective_002dC-runtime-API.html#Traditional-GNU-Objective_002dC-runtime-API" rel="prev" title="Traditional GNU Objective-C runtime API">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Executing-code-before-main"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Type-encoding.html#Type-encoding" accesskey="n" rel="next">Type encoding</a>, Previous: <a href="GNU-Objective_002dC-runtime-API.html#GNU-Objective_002dC-runtime-API" accesskey="p" rel="prev">GNU Objective-C runtime API</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="g_t_002bload_003a-Executing-Code-before-main"></a>
  68. <h3 class="section">8.2 <code>+load</code>: Executing Code before <code>main</code></h3>
  69. <p>This section is specific for the GNU Objective-C runtime. If you are
  70. using a different runtime, you can skip it.
  71. </p>
  72. <p>The GNU Objective-C runtime provides a way that allows you to execute
  73. code before the execution of the program enters the <code>main</code>
  74. function. The code is executed on a per-class and a per-category basis,
  75. through a special class method <code>+load</code>.
  76. </p>
  77. <p>This facility is very useful if you want to initialize global variables
  78. which can be accessed by the program directly, without sending a message
  79. to the class first. The usual way to initialize global variables, in the
  80. <code>+initialize</code> method, might not be useful because
  81. <code>+initialize</code> is only called when the first message is sent to a
  82. class object, which in some cases could be too late.
  83. </p>
  84. <p>Suppose for example you have a <code>FileStream</code> class that declares
  85. <code>Stdin</code>, <code>Stdout</code> and <code>Stderr</code> as global variables, like
  86. below:
  87. </p>
  88. <div class="smallexample">
  89. <pre class="smallexample">
  90. FileStream *Stdin = nil;
  91. FileStream *Stdout = nil;
  92. FileStream *Stderr = nil;
  93. @implementation FileStream
  94. + (void)initialize
  95. {
  96. Stdin = [[FileStream new] initWithFd:0];
  97. Stdout = [[FileStream new] initWithFd:1];
  98. Stderr = [[FileStream new] initWithFd:2];
  99. }
  100. /* <span class="roman">Other methods here</span> */
  101. @end
  102. </pre></div>
  103. <p>In this example, the initialization of <code>Stdin</code>, <code>Stdout</code> and
  104. <code>Stderr</code> in <code>+initialize</code> occurs too late. The programmer can
  105. send a message to one of these objects before the variables are actually
  106. initialized, thus sending messages to the <code>nil</code> object. The
  107. <code>+initialize</code> method which actually initializes the global
  108. variables is not invoked until the first message is sent to the class
  109. object. The solution would require these variables to be initialized
  110. just before entering <code>main</code>.
  111. </p>
  112. <p>The correct solution of the above problem is to use the <code>+load</code>
  113. method instead of <code>+initialize</code>:
  114. </p>
  115. <div class="smallexample">
  116. <pre class="smallexample">
  117. @implementation FileStream
  118. + (void)load
  119. {
  120. Stdin = [[FileStream new] initWithFd:0];
  121. Stdout = [[FileStream new] initWithFd:1];
  122. Stderr = [[FileStream new] initWithFd:2];
  123. }
  124. /* <span class="roman">Other methods here</span> */
  125. @end
  126. </pre></div>
  127. <p>The <code>+load</code> is a method that is not overridden by categories. If a
  128. class and a category of it both implement <code>+load</code>, both methods are
  129. invoked. This allows some additional initializations to be performed in
  130. a category.
  131. </p>
  132. <p>This mechanism is not intended to be a replacement for <code>+initialize</code>.
  133. You should be aware of its limitations when you decide to use it
  134. instead of <code>+initialize</code>.
  135. </p>
  136. <table class="menu" border="0" cellspacing="0">
  137. <tr><td align="left" valign="top">&bull; <a href="What-you-can-and-what-you-cannot-do-in-_002bload.html#What-you-can-and-what-you-cannot-do-in-_002bload" accesskey="1">What you can and what you cannot do in +load</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  138. </td></tr>
  139. </table>
  140. <hr>
  141. <div class="header">
  142. <p>
  143. Next: <a href="Type-encoding.html#Type-encoding" accesskey="n" rel="next">Type encoding</a>, Previous: <a href="GNU-Objective_002dC-runtime-API.html#GNU-Objective_002dC-runtime-API" accesskey="p" rel="prev">GNU Objective-C runtime API</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  144. </div>
  145. </body>
  146. </html>