Creating a Sample C++ 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. For a C-based application, this includes the following files. These files are specific to the application itself, and required to build and test the application prior to packaging it.

  • 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

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 C++ fibonacci application is available at eLxr User Documentation: Samples repository. Simply create your folder structure and copy these files into your working directory to save yourself some time.

If you already have an application that you want to use, go to Modifying Package Template Files for C++ Applications 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-cpp-1.0
    $ cd ~/elxr-dev/fibonacci-cpp-1.0
    
  2. Create a fibonacci-cpp-1.0/src subdirectory.

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

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

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

      /*
       * Copyright 2025 Wind River Systems, Inc.
       */
      
      #include <iostream>
      #include <cstdlib>
      #include "math.h"
      
      int main(int argc, char* argv[]) {
      int count = 0;
      
      if (argc >= 2)
              count = std::atoi(argv[1]);
      
      for (int i = 0; i < count; ++i) {
              std::cout << fibonacci(i) << std::endl;
          }
      
      return 0;
      }
      
  4. Set up the math.cpp file in the fibonacci-cpp-1.0/src directory.

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

      $ vi src/math.cpp
      

    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-cpp-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-cpp-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.cpp src/math.cpp
      OBJ := $(SRC:.cpp=.o)
      CXXFLAGS := -Wall -g
      
      all: fibonaccicpp
      
      archive: fibonaccicpp/fibonacci-cpp/
      
      fibonaccicpp: $(OBJ)
              @echo "CXXFLAGS=$(CXXFLAGS)" | fold -s -w 70 | sed -e 's/^/# /'
              $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
      
      %.o: %.cpp $(DEPS)
              $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
      
      install: fibonaccicpp
              install -D fibonaccicpp $(DESTDIR)$(prefix)/bin/fibonaccicpp
      
      fibonaccicpp/fibonacci-cpp/
              tar --transform 's,^,fibonaccicpp-$(VERSION)/,' -cvzf $@ $^
              cp $@ ../
      
      clean:
              @rm -rf fibonaccicpp fibonaccicpp-$(VERSION).tar.gz src/*.o *~
      
  7. Create the LICENSE file in the fibonacci-cpp-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
    
    g++  -Wall -g -c -o src/main.o src/main.cpp
    g++  -Wall -g -c -o src/math.o src/math.cpp
    # CXXFLAGS=-Wall -g
    g++  -Wall -g  -o fibonaccicpp src/main.o src/math.o
    

    After the make command completes, it creates a fibonaccicpp 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:

    $ ./fibonaccicpp 5
    

    The program will output:

    0
    1
    1
    2
    3
    
  10. Now that your application is working, you can clean up the working directory to prepare it for packaging.

    $ make clean
    
  11. Create an archive of the application files.

    This is required by the debmake command to ensure a source archive is available.

    $ make archive
    

    Once done, the folder structure should look like this.

    $ tree
    
        └── fibonacci-cpp-1.0
            ├── fibonaccicpp-1.0.tar.gz
            ├── LICENSE
            ├── Makefile
            └── src
                ├── main.cpp
                ├── math.cpp
                └── math.h
    

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 Modifying Package Template Files for C++ Applications.