4.1. Autotools-Based Projects

Once you have a suitable cross-development toolchain installed, it is very easy to develop a project using the GNU Autotools-based workflow, which is outside of the OpenEmbedded build system.

The following figure presents a simple Autotools workflow.

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 GNOME Developer site.

  1. Create a Working Directory and Populate It: Create a clean directory for your project and then make that directory your working location.

         $ mkdir $HOME/helloworld
         $ cd $HOME/helloworld
                        

    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: hello.c, configure.ac, Makefile.am, and README, respectively.

    Use the following command to create an empty README file, which is required by GNU Coding Standards:

         $ touch README
                        

    Create the remaining three files as follows:

    • hello.c:

           #include <stdio.h>
      
           main()
              {
                 printf("Hello World!\n");
              }
                                  

    • configure.ac:

           AC_INIT(hello,0.1)
           AM_INIT_AUTOMAKE([foreign])
           AC_PROG_CC
           AC_CONFIG_FILES(Makefile)
           AC_OUTPUT
                                  

    • Makefile.am:

           bin_PROGRAMS = hello
           hello_SOURCES = hello.c
                                  

  2. Source the Cross-Toolchain Environment Setup File: 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 Warrior Yocto Project release:

         $ source /opt/poky/2.7/environment-setup-i586-poky-linux
                        

  3. Create the configure Script: Use the autoreconf command to generate the configure script.

         $ autoreconf
                        

    The autoreconf tool takes care of running the other Autotools such as aclocal, autoconf, and automake.

    Note

    If you get errors from configure.ac, which autoreconf runs, that indicate missing files, you can use the "-i" option, which ensures missing auxiliary files are copied to the build host.

  4. Cross-Compile the Project: This command compiles the project using the cross-compiler. The CONFIGURE_FLAGS environment variable provides the minimal arguments for GNU configure:

         $ ./configure ${CONFIGURE_FLAGS}
                        

    For an Autotools-based project, you can use the cross-toolchain by just passing the appropriate host option to configure.sh. 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 armv5te-poky-linux-gnueabi. You will notice that the name of the script is environment-setup-armv5te-poky-linux-gnueabi. Thus, the following command works to update your project and rebuild it using the appropriate cross-toolchain tools:

         $ ./configure --host=armv5te-poky-linux-gnueabi --with-libtool-sysroot=sysroot_dir
                        

  5. Make and Install the Project: These two commands generate and install the project into the destination directory:

         $ make
         $ make install DESTDIR=./tmp
                        

    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 "Makefile-Based Projects" section.

    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.

         $ file ./tmp/usr/local/bin/hello
                        

  6. Execute Your Project: 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:

         $ ./tmp/usr/local/bin/hello
                        

    As expected, the project displays the "Hello World!" message.