summaryrefslogtreecommitdiffstats
path: root/documentation/sdk-manual/sdk-working-projects.xml
blob: d8cc4229dc15cc3e084f25230a0217af660a1882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
[<!ENTITY % poky SYSTEM "../poky.ent"> %poky; ] >

<chapter id='sdk-working-projects'>

    <title>Using the SDK Toolchain Directly</title>

    <para>
        You can use the SDK toolchain directly with Makefile,
        Autotools, and <trademark class='trade'>Eclipse</trademark>-based
        projects.
        This chapter covers the first two, while the
        "<link linkend='sdk-eclipse-project'>Developing Applications Using <trademark class='trade'>Eclipse</trademark></link>"
        Chapter covers the latter.
    </para>

    <section id='autotools-based-projects'>
        <title>Autotools-Based Projects</title>

        <para>
            Once you have a suitable
            <ulink url='&YOCTO_DOCS_REF_URL;#cross-development-toolchain'>cross-development toolchain</ulink>
            installed, it is very easy to develop a project using the
            <ulink url='https://en.wikipedia.org/wiki/GNU_Build_System'>GNU Autotools-based</ulink>
            workflow, which is outside of the
            <ulink url='&YOCTO_DOCS_REF_URL;#build-system-term'>OpenEmbedded build system</ulink>.
        </para>

        <para>
            The following figure presents a simple Autotools workflow.
            <imagedata fileref="figures/sdk-autotools-flow.png" width="7in" height="8in" align="center" />
        </para>

        <para>
            Follow these steps to create a simple Autotools-based
            "Hello World" project:
            <note>
                For more information on the GNU Autotools workflow,
                see the same example on the
                <ulink url='https://developer.gnome.org/anjuta-build-tutorial/stable/create-autotools.html.en'>GNOME Developer</ulink>
                site.
            </note>
            <orderedlist>
                <listitem><para>
                    <emphasis>Create a Working Directory and Populate It:</emphasis>
                    Create a clean directory for your project and then make
                    that directory your working location.
                    <literallayout class='monospaced'>
     $ mkdir $HOME/helloworld
     $ cd $HOME/helloworld
                    </literallayout>
                    After setting up the directory, populate it with files
                    needed for the flow.
                    You need a project source file, a file to help with
                    configuration, and a file to help create the Makefile,
                    and a README file:
                    <filename>hello.c</filename>,
                    <filename>configure.ac</filename>,
                    <filename>Makefile.am</filename>, and
                    <filename>README</filename>, respectively.</para>

                    <para> Use the following command to create an empty README
                    file, which is required by GNU Coding Standards:
                    <literallayout class='monospaced'>
     $ touch README
                    </literallayout>
                    Create the remaining three files as follows:
                    <itemizedlist>
                        <listitem><para>
                            <emphasis><filename>hello.c</filename>:</emphasis>
                            <literallayout class='monospaced'>
     #include &lt;stdio.h&gt;

     main()
        {
           printf("Hello World!\n");
        }
                            </literallayout>
                            </para></listitem>
                        <listitem><para>
                            <emphasis><filename>configure.ac</filename>:</emphasis>
                            <literallayout class='monospaced'>
     AC_INIT(hello,0.1)
     AM_INIT_AUTOMAKE([foreign])
     AC_PROG_CC
     AC_CONFIG_FILES(Makefile)
     AC_OUTPUT
                            </literallayout>
                            </para></listitem>
                        <listitem><para>
                            <emphasis><filename>Makefile.am</filename>:</emphasis>
                            <literallayout class='monospaced'>
     bin_PROGRAMS = hello
     hello_SOURCES = hello.c
                            </literallayout>
                            </para></listitem>
                    </itemizedlist>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Source the Cross-Toolchain
                    Environment Setup File:</emphasis>
                    As described earlier in the manual, installing the
                    cross-toolchain creates a cross-toolchain
                    environment setup script in the directory that the SDK
                    was installed.
                    Before you can use the tools to develop your project,
                    you must source this setup script.
                    The script begins with the string "environment-setup"
                    and contains the machine architecture, which is
                    followed by the string "poky-linux".
                    For this example, the command sources a script from the
                    default SDK installation directory that uses the
                    32-bit Intel x86 Architecture and the
                    &DISTRO_NAME; Yocto Project release:
                    <literallayout class='monospaced'>
     $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
                    </literallayout>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Create the <filename>configure</filename> Script:</emphasis>
                    Use the <filename>autoreconf</filename> command to
                    generate the <filename>configure</filename> script.
                    <literallayout class='monospaced'>
     $ autoreconf
                    </literallayout>
                    The <filename>autoreconf</filename> tool takes care
                    of running the other Autotools such as
                    <filename>aclocal</filename>,
                    <filename>autoconf</filename>, and
                    <filename>automake</filename>.
                    <note>
                        If you get errors from
                        <filename>configure.ac</filename>, which
                        <filename>autoreconf</filename> runs, that indicate
                        missing files, you can use the "-i" option, which
                        ensures missing auxiliary files are copied to the build
                        host.
                    </note>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Cross-Compile the Project:</emphasis>
                    This command compiles the project using the
                    cross-compiler.
                    The
                    <ulink url='&YOCTO_DOCS_REF_URL;#var-CONFIGURE_FLAGS'><filename>CONFIGURE_FLAGS</filename></ulink>
                    environment variable provides the minimal arguments for
                    GNU configure:
                    <literallayout class='monospaced'>
     $ ./configure ${CONFIGURE_FLAGS}
                    </literallayout>
                    For an Autotools-based project, you can use the
                    cross-toolchain by just passing the appropriate host
                    option to <filename>configure.sh</filename>.
                    The host option you use is derived from the name of the
                    environment setup script found in the directory in which
                    you installed the cross-toolchain.
                    For example, the host option for an ARM-based target that
                    uses the GNU EABI is
                    <filename>armv5te-poky-linux-gnueabi</filename>.
                    You will notice that the name of the script is
                    <filename>environment-setup-armv5te-poky-linux-gnueabi</filename>.
                    Thus, the following command works to update your project
                    and rebuild it using the appropriate cross-toolchain tools:
                    <literallayout class='monospaced'>
     $ ./configure --host=armv5te-poky-linux-gnueabi --with-libtool-sysroot=<replaceable>sysroot_dir</replaceable>
                    </literallayout>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Make and Install the Project:</emphasis>
                    These two commands generate and install the project
                    into the destination directory:
                    <literallayout class='monospaced'>
     $ make
     $ make install DESTDIR=./tmp
                    </literallayout>
                    <note>
                        To learn about environment variables established
                        when you run the cross-toolchain environment setup
                        script and how they are used or overridden when
                        the Makefile, see the
                        "<link linkend='makefile-based-projects'>Makefile-Based Projects</link>"
                        section.
                    </note>
                    This next command is a simple way to verify the
                    installation of your project.
                    Running the command prints the architecture on which
                    the binary file can run.
                    This architecture should be the same architecture that
                    the installed cross-toolchain supports.
                    <literallayout class='monospaced'>
     $ file ./tmp/usr/local/bin/hello
                    </literallayout>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Execute Your Project:</emphasis>
                    To execute the project, you would need to run it on your
                    target hardware.
                    If your target hardware happens to be your build host,
                    you could run the project as follows:
                    <literallayout class='monospaced'>
     $ ./tmp/usr/local/bin/hello
                    </literallayout>
                    As expected, the project displays the "Hello World!"
                    message.
                    </para></listitem>
            </orderedlist>
        </para>
    </section>

    <section id='makefile-based-projects'>
        <title>Makefile-Based Projects</title>

        <para>
            Simple Makefile-based projects use and interact with the
            cross-toolchain environment variables established when you run
            the cross-toolchain environment setup script.
            The environment variables are subject to general
            <filename>make</filename> rules.
        </para>

        <para>
            This section presents a simple Makefile development flow and
            provides an example that lets you see how you can use
            cross-toolchain environment variables and Makefile variables
            during development.
            <imagedata fileref="figures/sdk-makefile-flow.png" width="6in" height="7in" align="center" />
        </para>

        <para>
            The main point of this section is to explain the following three
            cases regarding variable behavior:
            <itemizedlist>
                <listitem><para>
                    <emphasis>Case 1 - No Variables Set in the
                    <filename>Makefile</filename> Map to Equivalent
                    Environment Variables Set in the SDK Setup Script:</emphasis>
                    Because matching variables are not specifically set in the
                    <filename>Makefile</filename>, the variables retain their
                    values based on the environment setup script.
                    </para></listitem>
                <listitem><para>
                    <emphasis>Case 2 - Variables Are Set in the Makefile that
                    Map to Equivalent Environment Variables from the SDK
                    Setup Script:</emphasis>
                    Specifically setting matching variables in the
                    <filename>Makefile</filename> during the build results in
                    the environment settings of the variables being
                    overwritten.
                    In this case, the variables you set in the
                    <filename>Makefile</filename> are used.
                    </para></listitem>
                <listitem><para>
                    <emphasis>Case 3 - Variables Are Set Using the Command Line
                    that Map to Equivalent Environment Variables from the
                    SDK Setup Script:</emphasis>
                    Executing the <filename>Makefile</filename> from the
                    command line results in the environment variables being
                    overwritten.
                    In this case, the command-line content is used.
                    </para></listitem>
            </itemizedlist>
            <note>
                Regardless of how you set your variables, if you use
                the "-e" option with <filename>make</filename>, the
                variables from the SDK setup script take precedence:
                <literallayout class='monospaced'>
     $ make -e <replaceable>target</replaceable>
                </literallayout>
            </note>
        </para>

        <para>
            The remainder of this section presents a simple Makefile example
            that demonstrates these variable behaviors.
        </para>

        <para>
            In a new shell environment variables are not established for the
            SDK until you run the setup script.
            For example, the following commands show a null value for the
            compiler variable (i.e.
            <ulink url='&YOCTO_DOCS_REF_URL;#var-CC'><filename>CC</filename></ulink>).
            <literallayout class='monospaced'>
     $ echo ${CC}

     $
            </literallayout>
            Running the SDK setup script for a 64-bit build host and an
            i586-tuned target architecture for a
            <filename>core-image-sato</filename> image using the current
            &DISTRO; Yocto Project release and then echoing that variable
            shows the value established through the script:
            <literallayout class='monospaced'>
     $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
     $ echo ${CC}
     i586-poky-linux-gcc -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux
            </literallayout>
        </para>

        <para>
            To illustrate variable use, work through this simple "Hello World!"
            example:
            <orderedlist>
                <listitem><para>
                    <emphasis>Create a Working Directory and Populate It:</emphasis>
                    Create a clean directory for your project and then make
                    that directory your working location.
                    <literallayout class='monospaced'>
     $ mkdir $HOME/helloworld
     $ cd $HOME/helloworld
                    </literallayout>
                    After setting up the directory, populate it with files
                    needed for the flow.
                    You need a <filename>main.c</filename> file from which you
                    call your function, a <filename>module.h</filename> file
                    to contain headers, and a <filename>module.c</filename>
                    that defines your function.
                    </para>

                    <para>Create the three files as follows:
                        <itemizedlist>
                            <listitem><para>
                                <emphasis><filename>main.c</filename>:</emphasis>
                                <literallayout class='monospaced'>
     #include "module.h"
     void sample_func();
     int main()
     {
     	sample_func();
     	return 0;
     }
                                </literallayout>
                                </para></listitem>
                            <listitem><para>
                                <emphasis><filename>module.h</filename>:</emphasis>
                                <literallayout class='monospaced'>
     #include &lt;stdio.h&gt;
     void sample_func();
                                </literallayout>
                                </para></listitem>
                            <listitem><para>
                                <emphasis><filename>module.c</filename>:</emphasis>
                                <literallayout class='monospaced'>
     #include "module.h"
     void sample_func()
     {
	     printf("Hello World!");
	     printf("\n");
     }
                                </literallayout>
                                </para></listitem>
                        </itemizedlist>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Source the Cross-Toolchain Environment Setup File:</emphasis>
                    As described earlier in the manual, installing the
                    cross-toolchain creates a cross-toolchain environment setup
                    script in the directory that the SDK was installed.
                    Before you can use the tools to develop your project,
                    you must source this setup script.
                    The script begins with the string "environment-setup"
                    and contains the machine architecture, which is
                    followed by the string "poky-linux".
                    For this example, the command sources a script from the
                    default SDK installation directory that uses the
                    32-bit Intel x86 Architecture and the
                    &DISTRO_NAME; Yocto Project release:
                    <literallayout class='monospaced'>
     $ source /opt/poky/&DISTRO;/environment-setup-i586-poky-linux
                    </literallayout>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Create the <filename>Makefile</filename>:</emphasis>
                    For this example, the Makefile contains two lines that
                    can be used to set the <filename>CC</filename> variable.
                    One line is identical to the value that is set when you
                    run the SDK environment setup script, and the other line
                    sets <filename>CC</filename> to "gcc", the default GNU
                    compiler on the build host:
                    <literallayout class='monospaced'>
     # CC=i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux
     # CC="gcc"
     all: main.o module.o
     	 ${CC} main.o module.o -o target_bin
     main.o: main.c module.h
	     ${CC} -I . -c main.c
     module.o: module.c module.h
	     ${CC} -I . -c module.c
     clean:
	     rm -rf *.o
	     rm target_bin
                    </literallayout>
                    </para></listitem>
                <listitem><para>
                    <emphasis>Make the Project:</emphasis>
                    Use the <filename>make</filename> command to create the
                    binary output file.
                    Because variables are commented out in the Makefile,
                    the value used for <filename>CC</filename> is the value
                    set when the SDK environment setup file was run:
                    <literallayout class='monospaced'>
     $ make
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
                    </literallayout>
                    From the results of the previous command, you can see that
                    the compiler used was the compiler established through
                    the <filename>CC</filename> variable defined in the
                    setup script.</para>

                    <para>You can override the <filename>CC</filename>
                    environment variable with the same variable as set from
                    the Makefile by uncommenting the line in the Makefile
                    and running <filename>make</filename> again.
                    <literallayout class='monospaced'>
     $ make clean
     rm -rf *.o
     rm target_bin
     #
     # Edit the Makefile by uncommenting the line that sets CC to "gcc"
     #
     $ make
     gcc -I . -c main.c
     gcc -I . -c module.c
     gcc main.o module.o -o target_bin
                    </literallayout>
                    As shown in the previous example, the cross-toolchain
                    compiler is not used.
                    Rather, the default compiler is used.</para>

                    <para>This next case shows how to override a variable
                    by providing the variable as part of the command line.
                    Go into the Makefile and re-insert the comment character
                    so that running <filename>make</filename> uses
                    the established SDK compiler.
                    However, when you run <filename>make</filename>, use a
                    command-line argument to set <filename>CC</filename>
                    to "gcc":
                    <literallayout class='monospaced'>
     $ make clean
     rm -rf *.o
     rm target_bin
     #
     # Edit the Makefile to comment out the line setting CC to "gcc"
     #
     $ make
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
     $ make clean
     rm -rf *.o
     rm target_bin
     $ make CC="gcc"
     gcc -I . -c main.c
     gcc -I . -c module.c
     gcc main.o module.o -o target_bin
                    </literallayout>
                    In the previous case, the command-line argument overrides
                    the SDK environment variable.</para>

                    <para>In this last case, edit Makefile again to use the
                    "gcc" compiler but then use the "-e" option on the
                    <filename>make</filename> command line:
                    <literallayout class='monospaced'>
     $ make clean
     rm -rf *.o
     rm target_bin
     #
     # Edit the Makefile to use "gcc"
     #
     $ make
     gcc -I . -c main.c
     gcc -I . -c module.c
     gcc main.o module.o -o target_bin
     $ make clean
     rm -rf *.o
     rm target_bin
     $ make -e
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c main.c
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux -I . -c module.c
     i586-poky-linux-gcc  -m32 -march=i586 --sysroot=/opt/poky/2.5/sysroots/i586-poky-linux main.o module.o -o target_bin
                    </literallayout>
                    In the previous case, the "-e" option forces
                    <filename>make</filename> to use the SDK environment
                    variables regardless of the values in the Makefile.
                    </para></listitem>
                <listitem><para>
                    <emphasis>Execute Your Project:</emphasis>
                    To execute the project (i.e.
                    <filename>target_bin</filename>), use the following
                    command:
                    <literallayout class='monospaced'>
     $ ./target_bin
     Hello World!
                    </literallayout>
                    <note>
                        If you used the cross-toolchain compiler to build
                        <filename>target_bin</filename> and your build host
                        differs in architecture from that of the target
                        machine, you need to run your project on the target
                        device.
                    </note>
                    As expected, the project displays the "Hello World!"
                    message.
                    </para></listitem>
            </orderedlist>
        </para>
    </section>
</chapter>
<!--
vim: expandtab tw=80 ts=4
-->