blob: d8cc4229dc15cc3e084f25230a0217af660a1882 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001<!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
5<chapter id='sdk-working-projects'>
6
7 <title>Using the SDK Toolchain Directly</title>
8
9 <para>
10 You can use the SDK toolchain directly with Makefile,
Brad Bishop316dfdd2018-06-25 12:45:53 -040011 Autotools, and <trademark class='trade'>Eclipse</trademark>-based
Patrick Williamsc0f7c042017-02-23 20:41:17 -060012 projects.
Brad Bishopd7bf8c12018-02-25 22:55:05 -050013 This chapter covers the first two, while the
14 "<link linkend='sdk-eclipse-project'>Developing Applications Using <trademark class='trade'>Eclipse</trademark></link>"
15 Chapter covers the latter.
Patrick Williamsc0f7c042017-02-23 20:41:17 -060016 </para>
17
18 <section id='autotools-based-projects'>
19 <title>Autotools-Based Projects</title>
20
21 <para>
Brad Bishop316dfdd2018-06-25 12:45:53 -040022 Once you have a suitable
23 <ulink url='&YOCTO_DOCS_REF_URL;#cross-development-toolchain'>cross-development toolchain</ulink>
24 installed, it is very easy to develop a project using the
25 <ulink url='https://en.wikipedia.org/wiki/GNU_Build_System'>GNU Autotools-based</ulink>
26 workflow, which is outside of the
27 <ulink url='&YOCTO_DOCS_REF_URL;#build-system-term'>OpenEmbedded build system</ulink>.
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028 </para>
29
Brad Bishop316dfdd2018-06-25 12:45:53 -040030 <para>
31 The following figure presents a simple Autotools workflow.
32 <imagedata fileref="figures/sdk-autotools-flow.png" width="7in" height="8in" align="center" />
33 </para>
Patrick Williamsc0f7c042017-02-23 20:41:17 -060034
Brad Bishop316dfdd2018-06-25 12:45:53 -040035 <para>
36 Follow these steps to create a simple Autotools-based
37 "Hello World" project:
38 <note>
39 For more information on the GNU Autotools workflow,
40 see the same example on the
41 <ulink url='https://developer.gnome.org/anjuta-build-tutorial/stable/create-autotools.html.en'>GNOME Developer</ulink>
42 site.
43 </note>
44 <orderedlist>
45 <listitem><para>
46 <emphasis>Create a Working Directory and Populate It:</emphasis>
47 Create a clean directory for your project and then make
48 that directory your working location.
49 <literallayout class='monospaced'>
Patrick Williamsc0f7c042017-02-23 20:41:17 -060050 $ mkdir $HOME/helloworld
51 $ cd $HOME/helloworld
Brad Bishop316dfdd2018-06-25 12:45:53 -040052 </literallayout>
53 After setting up the directory, populate it with files
54 needed for the flow.
55 You need a project source file, a file to help with
56 configuration, and a file to help create the Makefile,
57 and a README file:
58 <filename>hello.c</filename>,
59 <filename>configure.ac</filename>,
60 <filename>Makefile.am</filename>, and
61 <filename>README</filename>, respectively.</para>
62
63 <para> Use the following command to create an empty README
64 file, which is required by GNU Coding Standards:
65 <literallayout class='monospaced'>
66 $ touch README
67 </literallayout>
68 Create the remaining three files as follows:
69 <itemizedlist>
70 <listitem><para>
71 <emphasis><filename>hello.c</filename>:</emphasis>
72 <literallayout class='monospaced'>
Patrick Williamsc0f7c042017-02-23 20:41:17 -060073 #include &lt;stdio.h&gt;
74
75 main()
76 {
77 printf("Hello World!\n");
78 }
Brad Bishop316dfdd2018-06-25 12:45:53 -040079 </literallayout>
80 </para></listitem>
81 <listitem><para>
82 <emphasis><filename>configure.ac</filename>:</emphasis>
83 <literallayout class='monospaced'>
Patrick Williamsc0f7c042017-02-23 20:41:17 -060084 AC_INIT(hello,0.1)
85 AM_INIT_AUTOMAKE([foreign])
86 AC_PROG_CC
Brad Bishop316dfdd2018-06-25 12:45:53 -040087 AC_CONFIG_FILES(Makefile)
88 AC_OUTPUT
89 </literallayout>
90 </para></listitem>
91 <listitem><para>
92 <emphasis><filename>Makefile.am</filename>:</emphasis>
93 <literallayout class='monospaced'>
94 bin_PROGRAMS = hello
95 hello_SOURCES = hello.c
96 </literallayout>
97 </para></listitem>
98 </itemizedlist>
99 </para></listitem>
100 <listitem><para>
101 <emphasis>Source the Cross-Toolchain
102 Environment Setup File:</emphasis>
103 As described earlier in the manual, installing the
104 cross-toolchain creates a cross-toolchain
105 environment setup script in the directory that the SDK
106 was installed.
107 Before you can use the tools to develop your project,
108 you must source this setup script.
109 The script begins with the string "environment-setup"
110 and contains the machine architecture, which is
111 followed by the string "poky-linux".
112 For this example, the command sources a script from the
113 default SDK installation directory that uses the
114 32-bit Intel x86 Architecture and the
115 &DISTRO_NAME; Yocto Project release:
116 <literallayout class='monospaced'>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600117 $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
Brad Bishop316dfdd2018-06-25 12:45:53 -0400118 </literallayout>
119 </para></listitem>
120 <listitem><para>
121 <emphasis>Create the <filename>configure</filename> Script:</emphasis>
122 Use the <filename>autoreconf</filename> command to
123 generate the <filename>configure</filename> script.
124 <literallayout class='monospaced'>
125 $ autoreconf
126 </literallayout>
127 The <filename>autoreconf</filename> tool takes care
128 of running the other Autotools such as
129 <filename>aclocal</filename>,
130 <filename>autoconf</filename>, and
131 <filename>automake</filename>.
132 <note>
133 If you get errors from
134 <filename>configure.ac</filename>, which
135 <filename>autoreconf</filename> runs, that indicate
136 missing files, you can use the "-i" option, which
137 ensures missing auxiliary files are copied to the build
138 host.
139 </note>
140 </para></listitem>
141 <listitem><para>
142 <emphasis>Cross-Compile the Project:</emphasis>
143 This command compiles the project using the
144 cross-compiler.
145 The
146 <ulink url='&YOCTO_DOCS_REF_URL;#var-CONFIGURE_FLAGS'><filename>CONFIGURE_FLAGS</filename></ulink>
147 environment variable provides the minimal arguments for
148 GNU configure:
149 <literallayout class='monospaced'>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150 $ ./configure ${CONFIGURE_FLAGS}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400151 </literallayout>
152 For an Autotools-based project, you can use the
153 cross-toolchain by just passing the appropriate host
154 option to <filename>configure.sh</filename>.
155 The host option you use is derived from the name of the
156 environment setup script found in the directory in which
157 you installed the cross-toolchain.
158 For example, the host option for an ARM-based target that
159 uses the GNU EABI is
160 <filename>armv5te-poky-linux-gnueabi</filename>.
161 You will notice that the name of the script is
162 <filename>environment-setup-armv5te-poky-linux-gnueabi</filename>.
163 Thus, the following command works to update your project
164 and rebuild it using the appropriate cross-toolchain tools:
165 <literallayout class='monospaced'>
166 $ ./configure --host=armv5te-poky-linux-gnueabi --with-libtool-sysroot=<replaceable>sysroot_dir</replaceable>
167 </literallayout>
168 </para></listitem>
169 <listitem><para>
170 <emphasis>Make and Install the Project:</emphasis>
171 These two commands generate and install the project
172 into the destination directory:
173 <literallayout class='monospaced'>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600174 $ make
175 $ make install DESTDIR=./tmp
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600176 </literallayout>
Brad Bishop316dfdd2018-06-25 12:45:53 -0400177 <note>
178 To learn about environment variables established
179 when you run the cross-toolchain environment setup
180 script and how they are used or overridden when
181 the Makefile, see the
182 "<link linkend='makefile-based-projects'>Makefile-Based Projects</link>"
183 section.
184 </note>
185 This next command is a simple way to verify the
186 installation of your project.
187 Running the command prints the architecture on which
188 the binary file can run.
189 This architecture should be the same architecture that
190 the installed cross-toolchain supports.
191 <literallayout class='monospaced'>
192 $ file ./tmp/usr/local/bin/hello
193 </literallayout>
194 </para></listitem>
195 <listitem><para>
196 <emphasis>Execute Your Project:</emphasis>
197 To execute the project, you would need to run it on your
198 target hardware.
199 If your target hardware happens to be your build host,
200 you could run the project as follows:
201 <literallayout class='monospaced'>
202 $ ./tmp/usr/local/bin/hello
203 </literallayout>
204 As expected, the project displays the "Hello World!"
205 message.
206 </para></listitem>
207 </orderedlist>
208 </para>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600209 </section>
210
211 <section id='makefile-based-projects'>
212 <title>Makefile-Based Projects</title>
213
214 <para>
Brad Bishop316dfdd2018-06-25 12:45:53 -0400215 Simple Makefile-based projects use and interact with the
216 cross-toolchain environment variables established when you run
217 the cross-toolchain environment setup script.
218 The environment variables are subject to general
219 <filename>make</filename> rules.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600220 </para>
221
222 <para>
Brad Bishop316dfdd2018-06-25 12:45:53 -0400223 This section presents a simple Makefile development flow and
224 provides an example that lets you see how you can use
225 cross-toolchain environment variables and Makefile variables
226 during development.
227 <imagedata fileref="figures/sdk-makefile-flow.png" width="6in" height="7in" align="center" />
228 </para>
229
230 <para>
231 The main point of this section is to explain the following three
232 cases regarding variable behavior:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233 <itemizedlist>
234 <listitem><para>
235 <emphasis>Case 1 - No Variables Set in the
Brad Bishop316dfdd2018-06-25 12:45:53 -0400236 <filename>Makefile</filename> Map to Equivalent
237 Environment Variables Set in the SDK Setup Script:</emphasis>
238 Because matching variables are not specifically set in the
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600239 <filename>Makefile</filename>, the variables retain their
Brad Bishop316dfdd2018-06-25 12:45:53 -0400240 values based on the environment setup script.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600241 </para></listitem>
242 <listitem><para>
Brad Bishop316dfdd2018-06-25 12:45:53 -0400243 <emphasis>Case 2 - Variables Are Set in the Makefile that
244 Map to Equivalent Environment Variables from the SDK
245 Setup Script:</emphasis>
246 Specifically setting matching variables in the
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600247 <filename>Makefile</filename> during the build results in
248 the environment settings of the variables being
249 overwritten.
Brad Bishop316dfdd2018-06-25 12:45:53 -0400250 In this case, the variables you set in the
251 <filename>Makefile</filename> are used.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600252 </para></listitem>
253 <listitem><para>
Brad Bishop316dfdd2018-06-25 12:45:53 -0400254 <emphasis>Case 3 - Variables Are Set Using the Command Line
255 that Map to Equivalent Environment Variables from the
256 SDK Setup Script:</emphasis>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257 Executing the <filename>Makefile</filename> from the
Brad Bishop316dfdd2018-06-25 12:45:53 -0400258 command line results in the environment variables being
259 overwritten.
260 In this case, the command-line content is used.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600261 </para></listitem>
262 </itemizedlist>
263 <note>
Brad Bishop316dfdd2018-06-25 12:45:53 -0400264 Regardless of how you set your variables, if you use
265 the "-e" option with <filename>make</filename>, the
266 variables from the SDK setup script take precedence:
267 <literallayout class='monospaced'>
268 $ make -e <replaceable>target</replaceable>
269 </literallayout>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600270 </note>
271 </para>
Brad Bishop316dfdd2018-06-25 12:45:53 -0400272
273 <para>
274 The remainder of this section presents a simple Makefile example
275 that demonstrates these variable behaviors.
276 </para>
277
278 <para>
279 In a new shell environment variables are not established for the
280 SDK until you run the setup script.
281 For example, the following commands show a null value for the
282 compiler variable (i.e.
283 <ulink url='&YOCTO_DOCS_REF_URL;#var-CC'><filename>CC</filename></ulink>).
284 <literallayout class='monospaced'>
285 $ echo ${CC}
286
287 $
288 </literallayout>
289 Running the SDK setup script for a 64-bit build host and an
290 i586-tuned target architecture for a
291 <filename>core-image-sato</filename> image using the current
292 &DISTRO; Yocto Project release and then echoing that variable
293 shows the value established through the script:
294 <literallayout class='monospaced'>
295 $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
296 $ echo ${CC}
297 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux
298 </literallayout>
299 </para>
300
301 <para>
302 To illustrate variable use, work through this simple "Hello World!"
303 example:
304 <orderedlist>
305 <listitem><para>
306 <emphasis>Create a Working Directory and Populate It:</emphasis>
307 Create a clean directory for your project and then make
308 that directory your working location.
309 <literallayout class='monospaced'>
310 $ mkdir $HOME/helloworld
311 $ cd $HOME/helloworld
312 </literallayout>
313 After setting up the directory, populate it with files
314 needed for the flow.
315 You need a <filename>main.c</filename> file from which you
316 call your function, a <filename>module.h</filename> file
317 to contain headers, and a <filename>module.c</filename>
318 that defines your function.
319 </para>
320
321 <para>Create the three files as follows:
322 <itemizedlist>
323 <listitem><para>
324 <emphasis><filename>main.c</filename>:</emphasis>
325 <literallayout class='monospaced'>
326 #include "module.h"
327 void sample_func();
328 int main()
329 {
330 sample_func();
331 return 0;
332 }
333 </literallayout>
334 </para></listitem>
335 <listitem><para>
336 <emphasis><filename>module.h</filename>:</emphasis>
337 <literallayout class='monospaced'>
338 #include &lt;stdio.h&gt;
339 void sample_func();
340 </literallayout>
341 </para></listitem>
342 <listitem><para>
343 <emphasis><filename>module.c</filename>:</emphasis>
344 <literallayout class='monospaced'>
345 #include "module.h"
346 void sample_func()
347 {
348 printf("Hello World!");
349 printf("\n");
350 }
351 </literallayout>
352 </para></listitem>
353 </itemizedlist>
354 </para></listitem>
355 <listitem><para>
356 <emphasis>Source the Cross-Toolchain Environment Setup File:</emphasis>
357 As described earlier in the manual, installing the
358 cross-toolchain creates a cross-toolchain environment setup
359 script in the directory that the SDK was installed.
360 Before you can use the tools to develop your project,
361 you must source this setup script.
362 The script begins with the string "environment-setup"
363 and contains the machine architecture, which is
364 followed by the string "poky-linux".
365 For this example, the command sources a script from the
366 default SDK installation directory that uses the
367 32-bit Intel x86 Architecture and the
368 &DISTRO_NAME; Yocto Project release:
369 <literallayout class='monospaced'>
370 $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
371 </literallayout>
372 </para></listitem>
373 <listitem><para>
374 <emphasis>Create the <filename>Makefile</filename>:</emphasis>
375 For this example, the Makefile contains two lines that
376 can be used to set the <filename>CC</filename> variable.
377 One line is identical to the value that is set when you
378 run the SDK environment setup script, and the other line
379 sets <filename>CC</filename> to "gcc", the default GNU
380 compiler on the build host:
381 <literallayout class='monospaced'>
382 # CC=i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux
383 # CC="gcc"
384 all: main.o module.o
385 ${CC} main.o module.o -o target_bin
386 main.o: main.c module.h
387 ${CC} -I . -c main.c
388 module.o: module.c module.h
389 ${CC} -I . -c module.c
390 clean:
391 rm -rf *.o
392 rm target_bin
393 </literallayout>
394 </para></listitem>
395 <listitem><para>
396 <emphasis>Make the Project:</emphasis>
397 Use the <filename>make</filename> command to create the
398 binary output file.
399 Because variables are commented out in the Makefile,
400 the value used for <filename>CC</filename> is the value
401 set when the SDK environment setup file was run:
402 <literallayout class='monospaced'>
403 $ make
404 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
405 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
406 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
407 </literallayout>
408 From the results of the previous command, you can see that
409 the compiler used was the compiler established through
410 the <filename>CC</filename> variable defined in the
411 setup script.</para>
412
413 <para>You can override the <filename>CC</filename>
414 environment variable with the same variable as set from
415 the Makefile by uncommenting the line in the Makefile
416 and running <filename>make</filename> again.
417 <literallayout class='monospaced'>
418 $ make clean
419 rm -rf *.o
420 rm target_bin
421 #
422 # Edit the Makefile by uncommenting the line that sets CC to "gcc"
423 #
424 $ make
425 gcc -I . -c main.c
426 gcc -I . -c module.c
427 gcc main.o module.o -o target_bin
428 </literallayout>
429 As shown in the previous example, the cross-toolchain
430 compiler is not used.
431 Rather, the default compiler is used.</para>
432
433 <para>This next case shows how to override a variable
434 by providing the variable as part of the command line.
435 Go into the Makefile and re-insert the comment character
436 so that running <filename>make</filename> uses
437 the established SDK compiler.
438 However, when you run <filename>make</filename>, use a
439 command-line argument to set <filename>CC</filename>
440 to "gcc":
441 <literallayout class='monospaced'>
442 $ make clean
443 rm -rf *.o
444 rm target_bin
445 #
446 # Edit the Makefile to comment out the line setting CC to "gcc"
447 #
448 $ make
449 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
450 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
451 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
452 $ make clean
453 rm -rf *.o
454 rm target_bin
455 $ make CC="gcc"
456 gcc -I . -c main.c
457 gcc -I . -c module.c
458 gcc main.o module.o -o target_bin
459 </literallayout>
460 In the previous case, the command-line argument overrides
461 the SDK environment variable.</para>
462
463 <para>In this last case, edit Makefile again to use the
464 "gcc" compiler but then use the "-e" option on the
465 <filename>make</filename> command line:
466 <literallayout class='monospaced'>
467 $ make clean
468 rm -rf *.o
469 rm target_bin
470 #
471 # Edit the Makefile to use "gcc"
472 #
473 $ make
474 gcc -I . -c main.c
475 gcc -I . -c module.c
476 gcc main.o module.o -o target_bin
477 $ make clean
478 rm -rf *.o
479 rm target_bin
480 $ make -e
481 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
482 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
483 i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
484 </literallayout>
485 In the previous case, the "-e" option forces
486 <filename>make</filename> to use the SDK environment
487 variables regardless of the values in the Makefile.
488 </para></listitem>
489 <listitem><para>
490 <emphasis>Execute Your Project:</emphasis>
491 To execute the project (i.e.
492 <filename>target_bin</filename>), use the following
493 command:
494 <literallayout class='monospaced'>
495 $ ./target_bin
496 Hello World!
497 </literallayout>
498 <note>
499 If you used the cross-toolchain compiler to build
500 <filename>target_bin</filename> and your build host
501 differs in architecture from that of the target
502 machine, you need to run your project on the target
503 device.
504 </note>
505 As expected, the project displays the "Hello World!"
506 message.
507 </para></listitem>
508 </orderedlist>
509 </para>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600510 </section>
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600511</chapter>
512<!--
513vim: expandtab tw=80 ts=4
514-->