Creating a Sample Application

Use this procedure to create a sample application as a foundation for learning how to create a binary package with eLxr.

About This Task

The application detailed in this procedure demonstrates the Fibonacci series, which takes the number of terms as a command-line parameter. You will create the minimum source files necessary for building the application and identifying how you plan to share it, including:

  • Two source files

  • A header file

  • A Makefile to provide guidance for building the final binary and clean up the working directory

  • A license file

The directory structure for an application package looks like this:

├─ packageName-0.0
│  ├── LICENSE
│  ├── Makefile
│  └── src
│      └── packageBinary.c
└─ packageName-0.0.tar.gz

This example shows how to work with applications that are not yet part of your eLxr 12 image to help you understand the requirements for adding custom application packages. You can use this procedure for any stand-alone application that you want to develop and test apart from your eLxr image.

Note

A set of source files for the fibonacci application is available at eLxr User Documentation: Samples repository.

If you already have an application that you want to use, go to Generating and Modifying Package Template Files to get your application ready for packaging. |

Before You Begin

  • You must have a Linux development host or eLxr 12 image.

  • You must have the required development packages on your Linux development host or eLxr 12 image. For details, see Development Tools Overview and Requirements

Procedure

  1. Create a working directory for your application project.

    $ mkdir -p ~/elxr-dev/fibonacci-1.0
    $ cd ~/elxr-dev/fibonacci-1.0
    
  2. Create a fibonacci-1.0/src subdirectory.

    $ mkdir src
    
  3. Set up the main.c file in the fibonacci-1.0/src directory.

    1. Create the main.c file with a text editor.

      $ vi src/main.c
      
    2. Enter or copy the following text and save the file.

      /*
       * Copyright 2024 Wind River Systems, Inc.
       */
      
      #include <stdio.h>
      #include <stdlib.h>
      #include "math.h"
      
      int main(int argc, char *argv[])
      {
         int i, count=0;
      
         if (argc >= 2)
             count = atoi(argv[1]);
      
         for (i=0; i < count; i++) {
             printf("%d\n", fibonacci(i));
         }
      
         return 0;
      }
      
  4. Set up the math.c file in the fibonacci-1.0/src directory.

    1. Create the math.c file with a text editor.

      $ vi src/math.c
      

    Enter or copy the following text and save the file.

    /*
     * This is public domain software
     */
    
    int fibonacci(int n)
    {
        if (n <= 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return (fibonacci(n-1) + fibonacci(n-2));
    }
    
  5. Create the math.h file in the fibonacci-1.0/src directory.

    1. Create the math.h header file with a text editor.

      $ vi src/math.h
      
    2. Enter or copy the following text and save the file.

      /*
       * This is public domain software
       */
      
      int fibonacci(int n);
      
  6. Create the Makefile in the fibonacci-1.0 directory.

    1. Create the Makefile with a text editor.

      $ vi Makefile
      
    2. Enter or copy the following text.

      #
      # This is public domain software
      #
      VERSION := 1.0
      DEPS := src/math.h
      SRC := src/main.c src/math.c
      OBJ := $(SRC:.c=.o)
      
      all: fibonacci
      
      archive: fibonacci-1.0.tar.gz
      
      fibonacci: $(OBJ)
          @echo "CFLAGS=$(CFLAGS)" | \
               fold -s -w 70 | \
               sed -e 's/^/# /'
          $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
      
      %.o: %.c $(DEPS)
          $(CC) -g -c -o $@ $<
      
      install: fibonacci
          install -D fibonacci \
              $(DESTDIR)$(prefix)/bin/fibonacci
      
      fibonacci-1.0.tar.gz: $(SRC) $(DEPS) Makefile LICENSE
          tar --transform 's,^,fibonacci-$(VERSION)/,' -cvzf $@ $^
          cp *.tar.gz ../
      
      clean:
          @rm -rf fibonacci fibonacci-1.0.tar.gz src/*.o *~
      
  7. Create the LICENSE file in the fibonacci-1.0 directory.

    1. Create the LICENSE file with a text editor.

      $ vi LICENSE
      
    2. Enter or copy the following text and save the file.

      The Fibonacci application is public domain software.
      For the purposes of this example we keep these statements in this file
      but you should include the full text of the license here instead.
      
  8. Compile the program.

    This step is optional, as you will build the application as part of creating the package for it. It provides a good measure as to whether your source is sound, and the Makefile works as expected.

    Navigate to the project directory for the application and compile the application.

    $ make -e
    
    cc -g -c -o main.o main.c
    cc -g -c -o math.o math.c
    # CFLAGS=
    cc -o fibonacci main.o math.o
    

    After the make command completes, it creates a fibonacci binary file in the working directory, compiled to match the host development environment.

  9. Optionally, test the program on the host.

    If your host workstation is compatible with the target architecture of your platform, you can run the program with the following command:

    $ ./fibonacci 5
    

    The program will output:

    0
    1
    1
    2
    3
    

    Now that your application is working, you can clean up the working directory to prepare it for packaging.

  10. Remove the application binary and output files to clean up the working directory. The files removed are defined in the clean section of the Makefile. In this example, this includes the fibonacci binary and the main.o and math.o files left in the src directory by the build.

    $ make clean
    
  11. Create an archive of the application files. This is required by the debmake command to ensure a source archive is available. The Makefile specifies the creation of the fibonacci-1.0.tar.gz tarball, and copies it to the package main directory, which is parent to the working directory.

    $ make archive
    

Once done, the folder structure should look like this.

$ tree

├─ fibonacci-1.0
|  ├── fibonacci-1.0.tar.gz
│  ├── LICENSE
│  ├── Makefile
│  └── src
│      ├── main.c
|      ├── math.c
|      └── math.h
└─ fibonacci-1.0.tar.gz

Results

Now that you have a working application prepared for packaging, you will need to run the debmake command to generate and update the package template files. For details, see Generating and Modifying Package Template Files.