選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

176 行
7.5KB

  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>Plugins building (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Plugins building (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Plugins building (GNU Compiler Collection (GCC) Internals)">
  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="Plugins.html#Plugins" rel="up" title="Plugins">
  30. <link href="LTO.html#LTO" rel="next" title="LTO">
  31. <link href="Plugins-tracking.html#Plugins-tracking" rel="prev" title="Plugins tracking">
  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="Plugins-building"></a>
  62. <div class="header">
  63. <p>
  64. Previous: <a href="Plugins-tracking.html#Plugins-tracking" accesskey="p" rel="prev">Plugins tracking</a>, Up: <a href="Plugins.html#Plugins" accesskey="u" rel="up">Plugins</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="Building-GCC-plugins"></a>
  68. <h3 class="section">24.10 Building GCC plugins</h3>
  69. <p>If plugins are enabled, GCC installs the headers needed to build a
  70. plugin (somewhere in the installation tree, e.g. under
  71. <samp>/usr/local</samp>). In particular a <samp>plugin/include</samp> directory
  72. is installed, containing all the header files needed to build plugins.
  73. </p>
  74. <p>On most systems, you can query this <code>plugin</code> directory by
  75. invoking <code>gcc -print-file-name=plugin</code> (replace if needed
  76. <code>gcc</code> with the appropriate program path).
  77. </p>
  78. <p>Inside plugins, this <code>plugin</code> directory name can be queried by
  79. calling <code>default_plugin_dir_name ()</code>.
  80. </p>
  81. <p>Plugins may know, when they are compiled, the GCC version for which
  82. <samp>plugin-version.h</samp> is provided. The constant macros
  83. <code>GCCPLUGIN_VERSION_MAJOR</code>, <code>GCCPLUGIN_VERSION_MINOR</code>,
  84. <code>GCCPLUGIN_VERSION_PATCHLEVEL</code>, <code>GCCPLUGIN_VERSION</code> are
  85. integer numbers, so a plugin could ensure it is built for GCC 4.7 with
  86. </p><div class="smallexample">
  87. <pre class="smallexample">#if GCCPLUGIN_VERSION != 4007
  88. #error this GCC plugin is for GCC 4.7
  89. #endif
  90. </pre></div>
  91. <p>The following GNU Makefile excerpt shows how to build a simple plugin:
  92. </p>
  93. <div class="smallexample">
  94. <pre class="smallexample">HOST_GCC=g++
  95. TARGET_GCC=gcc
  96. PLUGIN_SOURCE_FILES= plugin1.c plugin2.cc
  97. GCCPLUGINS_DIR:= $(shell $(TARGET_GCC) -print-file-name=plugin)
  98. CXXFLAGS+= -I$(GCCPLUGINS_DIR)/include -fPIC -fno-rtti -O2
  99. plugin.so: $(PLUGIN_SOURCE_FILES)
  100. $(HOST_GCC) -shared $(CXXFLAGS) $^ -o $@
  101. </pre></div>
  102. <p>A single source file plugin may be built with <code>g++ -I`gcc
  103. -print-file-name=plugin`/include -fPIC -shared -fno-rtti -O2 plugin.c -o
  104. plugin.so</code>, using backquote shell syntax to query the <samp>plugin</samp>
  105. directory.
  106. </p>
  107. <p>Plugin support on Windows/MinGW has a number of limitations and
  108. additional requirements. When building a plugin on Windows we have to
  109. link an import library for the corresponding backend executable, for
  110. example, <samp>cc1.exe</samp>, <samp>cc1plus.exe</samp>, etc., in order to gain
  111. access to the symbols provided by GCC. This means that on Windows a
  112. plugin is language-specific, for example, for C, C++, etc. If you wish
  113. to use your plugin with multiple languages, then you will need to
  114. build multiple plugin libraries and either instruct your users on how
  115. to load the correct version or provide a compiler wrapper that does
  116. this automatically.
  117. </p>
  118. <p>Additionally, on Windows the plugin library has to export the
  119. <code>plugin_is_GPL_compatible</code> and <code>plugin_init</code> symbols. If you
  120. do not wish to modify the source code of your plugin, then you can use
  121. the <samp>-Wl,--export-all-symbols</samp> option or provide a suitable DEF
  122. file. Alternatively, you can export just these two symbols by decorating
  123. them with <code>__declspec(dllexport)</code>, for example:
  124. </p>
  125. <div class="smallexample">
  126. <pre class="smallexample">#ifdef _WIN32
  127. __declspec(dllexport)
  128. #endif
  129. int plugin_is_GPL_compatible;
  130. #ifdef _WIN32
  131. __declspec(dllexport)
  132. #endif
  133. int plugin_init (plugin_name_args *, plugin_gcc_version *)
  134. </pre></div>
  135. <p>The import libraries are installed into the <code>plugin</code> directory
  136. and their names are derived by appending the <code>.a</code> extension to
  137. the backend executable names, for example, <samp>cc1.exe.a</samp>,
  138. <samp>cc1plus.exe.a</samp>, etc. The following command line shows how to
  139. build the single source file plugin on Windows to be used with the C++
  140. compiler:
  141. </p>
  142. <div class="smallexample">
  143. <pre class="smallexample">g++ -I`gcc -print-file-name=plugin`/include -shared -Wl,--export-all-symbols \
  144. -o plugin.dll plugin.c `gcc -print-file-name=plugin`/cc1plus.exe.a
  145. </pre></div>
  146. <p>When a plugin needs to use <code>gengtype</code>, be sure that both
  147. <samp>gengtype</samp> and <samp>gtype.state</samp> have the same version as the
  148. GCC for which the plugin is built.
  149. </p>
  150. <hr>
  151. <div class="header">
  152. <p>
  153. Previous: <a href="Plugins-tracking.html#Plugins-tracking" accesskey="p" rel="prev">Plugins tracking</a>, Up: <a href="Plugins.html#Plugins" accesskey="u" rel="up">Plugins</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>
  154. </div>
  155. </body>
  156. </html>