sdk-working-projects.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  2. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
  3. [<!ENTITY % poky SYSTEM "../poky.ent"> %poky; ] >
  4. <chapter id='sdk-working-projects'>
  5. <title>Using the SDK Toolchain Directly</title>
  6. <para>
  7. You can use the SDK toolchain directly with Makefile,
  8. Autotools, and <trademark class='trade'>Eclipse</trademark> based
  9. projects.
  10. This chapter covers the first two, while the
  11. "<link linkend='sdk-eclipse-project'>Developing Applications Using <trademark class='trade'>Eclipse</trademark></link>"
  12. Chapter covers the latter.
  13. </para>
  14. <section id='autotools-based-projects'>
  15. <title>Autotools-Based Projects</title>
  16. <para>
  17. Once you have a suitable cross-toolchain installed, it is very easy
  18. to develop a project outside of the OpenEmbedded build system.
  19. This section presents a simple "Helloworld" example that shows how
  20. to set up, compile, and run the project.
  21. </para>
  22. <section id='creating-and-running-a-project-based-on-gnu-autotools'>
  23. <title>Creating and Running a Project Based on GNU Autotools</title>
  24. <para>
  25. Follow these steps to create a simple Autotools-based project:
  26. <orderedlist>
  27. <listitem><para>
  28. <emphasis>Create your directory:</emphasis>
  29. Create a clean directory for your project and then make
  30. that directory your working location:
  31. <literallayout class='monospaced'>
  32. $ mkdir $HOME/helloworld
  33. $ cd $HOME/helloworld
  34. </literallayout>
  35. </para></listitem>
  36. <listitem><para>
  37. <emphasis>Populate the directory:</emphasis>
  38. Create <filename>hello.c</filename>,
  39. <filename>Makefile.am</filename>,
  40. and <filename>configure.ac</filename> files as follows:
  41. <itemizedlist>
  42. <listitem><para>
  43. For <filename>hello.c</filename>, include
  44. these lines:
  45. <literallayout class='monospaced'>
  46. #include &lt;stdio.h&gt;
  47. main()
  48. {
  49. printf("Hello World!\n");
  50. }
  51. </literallayout>
  52. </para></listitem>
  53. <listitem><para>
  54. For <filename>Makefile.am</filename>,
  55. include these lines:
  56. <literallayout class='monospaced'>
  57. bin_PROGRAMS = hello
  58. hello_SOURCES = hello.c
  59. </literallayout>
  60. </para></listitem>
  61. <listitem><para>
  62. For <filename>configure.in</filename>,
  63. include these lines:
  64. <literallayout class='monospaced'>
  65. AC_INIT(hello,0.1)
  66. AM_INIT_AUTOMAKE([foreign])
  67. AC_PROG_CC
  68. AC_PROG_INSTALL
  69. AC_OUTPUT(Makefile)
  70. </literallayout>
  71. </para></listitem>
  72. </itemizedlist>
  73. </para></listitem>
  74. <listitem><para>
  75. <emphasis>Source the cross-toolchain
  76. environment setup file:</emphasis>
  77. As described earlier in the manual, installing the
  78. cross-toolchain creates a cross-toolchain
  79. environment setup script in the directory that the SDK
  80. was installed.
  81. Before you can use the tools to develop your project,
  82. you must source this setup script.
  83. The script begins with the string "environment-setup"
  84. and contains the machine architecture, which is
  85. followed by the string "poky-linux".
  86. Here is an example that sources a script from the
  87. default SDK installation directory that uses the
  88. 32-bit Intel x86 Architecture and the
  89. &DISTRO_NAME; Yocto Project release:
  90. <literallayout class='monospaced'>
  91. $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
  92. </literallayout>
  93. </para></listitem>
  94. <listitem><para>
  95. <emphasis>Generate the local aclocal.m4
  96. files and create the configure script:</emphasis>
  97. The following GNU Autotools generate the local
  98. <filename>aclocal.m4</filename> files and create the
  99. configure script:
  100. <literallayout class='monospaced'>
  101. $ aclocal
  102. $ autoconf
  103. </literallayout>
  104. </para></listitem>
  105. <listitem><para>
  106. <emphasis>Generate files needed by GNU coding
  107. standards:</emphasis>
  108. GNU coding standards require certain files in order
  109. for the project to be compliant.
  110. This command creates those files:
  111. <literallayout class='monospaced'>
  112. $ touch NEWS README AUTHORS ChangeLog
  113. </literallayout>
  114. </para></listitem>
  115. <listitem><para>
  116. <emphasis>Generate the configure file:</emphasis>
  117. This command generates the
  118. <filename>configure</filename>:
  119. <literallayout class='monospaced'>
  120. $ automake -a
  121. </literallayout>
  122. </para></listitem>
  123. <listitem><para>
  124. <emphasis>Cross-compile the project:</emphasis>
  125. This command compiles the project using the
  126. cross-compiler.
  127. The
  128. <ulink url='&YOCTO_DOCS_REF_URL;#var-CONFIGURE_FLAGS'><filename>CONFIGURE_FLAGS</filename></ulink>
  129. environment variable provides the minimal arguments for
  130. GNU configure:
  131. <literallayout class='monospaced'>
  132. $ ./configure ${CONFIGURE_FLAGS}
  133. </literallayout>
  134. </para></listitem>
  135. <listitem><para>
  136. <emphasis>Make and install the project:</emphasis>
  137. These two commands generate and install the project
  138. into the destination directory:
  139. <literallayout class='monospaced'>
  140. $ make
  141. $ make install DESTDIR=./tmp
  142. </literallayout>
  143. </para></listitem>
  144. <listitem><para>
  145. <emphasis>Verify the installation:</emphasis>
  146. This command is a simple way to verify the installation
  147. of your project.
  148. Running the command prints the architecture on which
  149. the binary file can run.
  150. This architecture should be the same architecture that
  151. the installed cross-toolchain supports.
  152. <literallayout class='monospaced'>
  153. $ file ./tmp/usr/local/bin/hello
  154. </literallayout>
  155. </para></listitem>
  156. <listitem><para>
  157. <emphasis>Execute your project:</emphasis>
  158. To execute the project in the shell, simply enter
  159. the name.
  160. You could also copy the binary to the actual target
  161. hardware and run the project there as well:
  162. <literallayout class='monospaced'>
  163. $ ./hello
  164. </literallayout>
  165. As expected, the project displays the "Hello World!"
  166. message.
  167. </para></listitem>
  168. </orderedlist>
  169. </para>
  170. </section>
  171. <section id='passing-host-options'>
  172. <title>Passing Host Options</title>
  173. <para>
  174. For an Autotools-based project, you can use the cross-toolchain
  175. by just passing the appropriate host option to
  176. <filename>configure.sh</filename>.
  177. The host option you use is derived from the name of the
  178. environment setup script found in the directory in which you
  179. installed the cross-toolchain.
  180. For example, the host option for an ARM-based target that uses
  181. the GNU EABI is <filename>armv5te-poky-linux-gnueabi</filename>.
  182. You will notice that the name of the script is
  183. <filename>environment-setup-armv5te-poky-linux-gnueabi</filename>.
  184. Thus, the following command works to update your project and
  185. rebuild it using the appropriate cross-toolchain tools:
  186. <literallayout class='monospaced'>
  187. $ ./configure --host=armv5te-poky-linux-gnueabi \
  188. --with-libtool-sysroot=<replaceable>sysroot_dir</replaceable>
  189. </literallayout>
  190. <note>
  191. If the <filename>configure</filename> script results in
  192. problems recognizing the
  193. <filename>--with-libtool-sysroot=</filename><replaceable>sysroot-dir</replaceable>
  194. option, regenerate the script to enable the support by
  195. doing the following and then run the script again:
  196. <literallayout class='monospaced'>
  197. $ libtoolize --automake
  198. $ aclocal -I ${OECORE_TARGET_SYSROOT}/usr/share/aclocal [-I <replaceable>dir_containing_your_project-specific_m4_macros</replaceable>]
  199. $ autoconf
  200. $ autoheader
  201. $ automake -a
  202. </literallayout>
  203. </note>
  204. </para>
  205. </section>
  206. </section>
  207. <section id='makefile-based-projects'>
  208. <title>Makefile-Based Projects</title>
  209. <para>
  210. For Makefile-based projects, the cross-toolchain environment
  211. variables established by running the cross-toolchain environment
  212. setup script are subject to general <filename>make</filename>
  213. rules.
  214. </para>
  215. <para>
  216. To illustrate this, consider the following four cross-toolchain
  217. environment variables:
  218. <literallayout class='monospaced'>
  219. <ulink url='&YOCTO_DOCS_REF_URL;#var-CC'>CC</ulink>=i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/&DISTRO;/sysroots/i586-poky-linux
  220. <ulink url='&YOCTO_DOCS_REF_URL;#var-LD'>LD</ulink>=i586-poky-linux-ld --sysroot=/opt/poky/&DISTRO;/sysroots/i586-poky-linux
  221. <ulink url='&YOCTO_DOCS_REF_URL;#var-CFLAGS'>CFLAGS</ulink>=-O2 -pipe -g -feliminate-unused-debug-types
  222. <ulink url='&YOCTO_DOCS_REF_URL;#var-CXXFLAGS'>CXXFLAGS</ulink>=-O2 -pipe -g -feliminate-unused-debug-types
  223. </literallayout>
  224. Now, consider the following three cases:
  225. <itemizedlist>
  226. <listitem><para>
  227. <emphasis>Case 1 - No Variables Set in the
  228. <filename>Makefile</filename>:</emphasis>
  229. Because these variables are not specifically set in the
  230. <filename>Makefile</filename>, the variables retain their
  231. values based on the environment.
  232. </para></listitem>
  233. <listitem><para>
  234. <emphasis>Case 2 - Variables Set in the
  235. <filename>Makefile</filename>:</emphasis>
  236. Specifically setting variables in the
  237. <filename>Makefile</filename> during the build results in
  238. the environment settings of the variables being
  239. overwritten.
  240. </para></listitem>
  241. <listitem><para>
  242. <emphasis>Case 3 - Variables Set when the
  243. <filename>Makefile</filename> is Executed from the
  244. Command Line:</emphasis>
  245. Executing the <filename>Makefile</filename> from the
  246. command-line results in the variables being overwritten
  247. with command-line content regardless of what is being set
  248. in the <filename>Makefile</filename>.
  249. In this case, environment variables are not considered
  250. unless you use the "-e" flag during the build:
  251. <literallayout class='monospaced'>
  252. $ make -e <replaceable>file</replaceable>
  253. </literallayout>
  254. If you use this flag, then the environment values of the
  255. variables override any variables specifically set in the
  256. <filename>Makefile</filename>.
  257. </para></listitem>
  258. </itemizedlist>
  259. <note>
  260. For the list of variables set up by the cross-toolchain
  261. environment setup script, see the
  262. "<link linkend='sdk-running-the-sdk-environment-setup-script'>Running the SDK Environment Setup Script</link>"
  263. section.
  264. </note>
  265. </para>
  266. </section>
  267. </chapter>
  268. <!--
  269. vim: expandtab tw=80 ts=4
  270. -->