[Manual] How to make native C/C++ application step by step.



  • These are many pieces of manuals to do it. But it's not convenient at all. I suggest to show all of us the simplest way to start coding the native applications via OpenWRT 18.06 SDK and GNU Autotools.

    I studied many documentation about and make my own "template" for it (see below). First, let's figure out what we need:

    • cross-compilation. Why? Cross-compile runs on powerful fast machine (Virtual Machine) to make our applications.

      • If you will compile on the Omega directly, it does not have sufficient memory resources to run a C/C++ build tool chain - only a simple applications.
      • Second problem in third-party libraries - Omega; opkg manager doesn't install -dev files of libraries and it's will be a problem in your application.

      PLEASE FORGET DIRECT COMPILATION FOREVER !

    • Using GNU Autotools build system. You should adjust write platform-independent application. May be in future you want use application in other Linux-based machine. Why Autotools such an old system?

      The Autotools is the most convenient way for embedded systems. You will have usual command to compile and install the application, like ./configure && make && make install.

      • Autotools is more flexible to embedded systems. This build system works with make utility. It's very simple way to start coding in any IDE. I'm using Visual Studio 2017 for Makefile-solutions.

      • You can make your own Makefile or use Automake friend utility.

      • Autotools has simple mechanism to cross-compilation via third-party toolchain.


    About C/C++ template for native applications

    I created my own template to start any native application for Linux. Here is repo link:
    https://bitbucket.org/hlorka/myapptemplate/

    This template is simple Hello World application with next features:

    • You can compile/install/uninstall it on the any Linux;

    • You can compile application via Onion SDK or OpenWRT 18.06 SDK for x86_64 machines (Virtual Machines);

    • You can remotely install/uninstall your application on Onion Omega (rsync, ssh utilities).

    • It's using Autotools and generate bash-scripts to manage application. You no need install any other build system, such CMake.

    Thus I can have coding in my favorite IDE, do sync sources to Virtual Machine and compile it there, remote or normally install. But first I usually write and test application for virtual machine except case application touches hardware peripherals.

    The detailed manual about my template you can see in the README.md from repo. Unfortunately the document is still only in Russian; please use auto translation.



  • Hello all. I decided translate my README.md file to all from Russian. The file is large and I will translate it in parts. And sorry if my translation will be not good.


    How it use. Step-by-step guide.

    This guide is intended for the start Linux embedded programmer. If any steps and actions are familiar to You, you can skip it.

    For my cross-compiling template , the steps applicable for Linux-based OS, such as Debian, Ubuntu, for other one it may be differences.

    It is assumed that you already have a dedicated or virtual machine (host) with this OS installed. All actions below are performed on the host via a standard Linux terminal.

    Let's begin...



  • Step 1. Setup host environment, copy template's files into host

    1. Before using the template, you have to install the software tools GNU Autotools, build-essential. If none, the commands below are for automatically downloading and installing them into Debian, Ubuntu.

        apt-get install -y autoconf libtool
        apt-get install -y build-essential
      

    NOTICE: Dependent packages automake, autotools-dev, autoheader etc. will be also installed.

    1. Then, go to any convenient directory of your own C/C++ projects and copy (clone) all the template's files from the repository there. Below is the default clone command.

        git clone https://bitbucket.org/hlorka/myapptemplate.git [<my-app-dir-name>]
      

      If your project will use the Automake utility (see below for details), you must clone special Automake git-branch using another command:

        git clone --single-branch --branch Automake https://bitbucket.org/hlorka/myapptemplate.git [<my-app-dir-name>]
      

      NOTICE: The root directory of the template myapptemplate will be created automatically, after cloning is complete, it is recommended to rename and give a more appropriate name to your own application, or you can do it immediately when cloning, by adding the name to the end of cloning command [<my-app-dir-name>].

    2. The link to this repository is no longer needed and you have to delete it by delete the hidden subdirectory .git. See next command to do it.

        rm -rf ./.git


  • Step 2. Creating Autotools working files

    The Autotools set utilities help us to create large files from short special templates like configure.ac, Makefile.am, etc. it is necessary to generate a script known to everyone (in Linux) ./configure.

    To do this, you need to run autoconfig command in the directory with mytemplate's files, but stop! I think base file configure.ac later you will change and the most convenient way to execute another command every time after change autotools' files:

    autoreconf -iv
    

    Then you will see the executable bash-script configure, as well as many auxiliary files and a directory. All these files are distributive (portable and independent of the target platform), i.e. they must be copied together with the source code of the program for its configuration, compilation, installation on the target platform (without Autotools).

    This is a necessary action before the first compilation.

    NOTE: The Automake utility always generates a set of dist, distdir, distclean targets for automatically creating/deleting archived file (by default .tar.gz) - distribution of the project.



  • Step 3.1. General compilation on the target machine

    To compile, in the target machine you have to install C/C++ compiler before with all the friendly utilities and standard libraries.

    Compiling and installing an application is not different from the usual (familiar in Linux) build applications from source code. First, you need to configure the project for a specific OS and architecture. Typical set of commands for configuring, compiling, and installing into the system are below:

    ./configure && make && make install
    

    After the configure script will successful done, from .in-files (special blanks) the script will generated full-fledged ready-to-use files, such as Makefile, config.h.

    NOTICE: The configuration Script also checks the system for all necessary project files (packages, libraries, headers, utilities, etc.), if they are not present, the configuration will fail. Satisfying dependencies is a user's task to do it manually. All developers usually accompany a project with a small installation guide that includes all the dependencies.



  • Step 3.2. Cross-compilation (Omega OS)

    The most interesting for us is a case of cross-compiling the application. This step provides an example of configuring and compiling this project via a third-party SDK using the Linux OpenWRT SDK for a single-board minicomputer Onion Omega2+.

    The Omega 2 single-board computer provided with pre-installed its own Linux Onion OS operating system, which has a lot of limitations and unfortunately the vendor does not provide a convenient ready-to-use SDK.

    To use it we need to compile Onion SDK from sources. Docker's variant is not working on Windows 10 Home Edition. In this step it is no need!

    Onion OS is based on another operating system OpenWRT 18.06 which has the SDK (it already compiled for MIPS architecture and processor Mediatek MT7688 as our Omega2 builded). It ready to run on a x86-x64 host architecture. Therefore, the fastest way to create cross-platform user space applications is download OpenWRT 18.06 SDK x86_x64 MIPS MT76x8

    Now let's begin cross-compiling.

    1. Select a convenient SDK installation directory in the host (x86/x64) and go to it. By the known link OpenWRT SDK x86_x64 MIPS MT76x8 we find the archive file .tar.xz with the text "-sdk-", download, unpack; example below:

        wget https://downloads.openwrt.org/releases/18.06.2/targets/ramips/mt76x8/openwrt-sdk-18.06.2-ramips-mt76x8_gcc-7.3.0_musl.Linux-x86_64.tar.xz
        tar -xf ./openwrt-sdk-18.06.2-ramips-mt76x8_gcc-7.3.0_musl.Linux-x86_64.tar.xz
        mv ./openwrt-sdk-18.06.2-ramips-mt76x8_gcc-7.3.0_musl.Linux-x86_64 ./openwrt-sdk-mt76x8
      
    2. Go back to the folder with the template project (hello world). You need to edit file configure-mips-openwrt.sh and write your own SDK paths. Below are the lines from my file that will need to be changed.

        # define the pathes (by default) to the buildroot
        BUILDROOT_PATH="/home/vladimir/OpenWRT-sdk"
      
        # define the toolchain and target names
        ...
        TOOLCHAIN_DIR="$STAGING_DIR/toolchain-mipsel_24kc_gcc-7.3.0_musl"
        TARGET_DIR="$STAGING_DIR/target-mipsel_24kc_musl"
      

      Once properly edited this file it can be applied to other projects with the same SDK's paths without changes.

    3. Now you need to run configure script, through the previously edited script configure-mips-openwrt.sh. It's important!

        ./configure-mips-openwrt.sh
      

      If all paths are correct, there should be no errors, working files will appear from the blank files with the suffix .in.
      In the cross-compilation case (using SDK), you will find script file xmake.sh (see details below).

      NOTICE: The configure script (configure-mips-openwrt.sh) can accept arguments, for more details see the relevant documentation

      WARNING: In cross-compilation case the configure script incorrectly sets the installation paths, set it by hand via command-line arguments, type key --help for help.

    4. Cross-compilation via SDK. The last thing is running the make utility through the script xmake.sh.

        ./xmake.sh
      

      NOTICE: xmake.sh supports command-line arguments that are passed to the make utility.

    That's all!


    And last you can easy switch compilation of your application between Linux host x86/x64 and Onion OS by run configure or configure-mips-openwrt.sh at any time.



  • This post is deleted!


  • Hi all! I glad to show my template of project Hello world that uses CMake build system, it's named as myapptemplate.

    My last sample was use GNU Autotools, it's a difficult system. Today we have many projects with CMake (cmake.org). For new projects I recommend use CMake. But it will have problems with cross-compilation for beginners. Let's find it out!


    Step 1. Creating you own CMake project

    First you have to install git and cmake applications in the host if there are none.

    Next follow into you projects directory in the host, e.g. $HOME/projects and clone there my CMake template myapptemplate project. Then enter new project directory and remove .git dir from there.

    git clone --single-branch --branch CMake https://bitbucket.org/hlorka/myapptemplate.git <my_app_name>
    cd ./<my_app_name>
    rm -rf ./.git
    

    The files CMakeSettings.json and myapptemplate.vcxproj for MS Visual Studio IDE and can be deleted too.

    Step 2A. General compilation for the host machine

    It's very simple. First you have to configure and make cache CMake's files. Look at the command below:

    cmake . [--config Debug]
    

    --config Debug will append debug information (symbols) and postfix -d to the output binary file (helloworld-d).

    Then you can build (compile) project, using next commands below

    • Via CMake:

      cmake --build . [--clean-first]
      
    • Or via Makefile (make utility)

      make
      

    Step 2B. Cross-compilation using OpenWRT SDK

    First open file cross-compile/openwrt-mips-toolchain.cmake for modify.

    nano ./cross-compile/openwrt-mips-toolchain.cmake
    

    Fragment of this file below:

    ...
    # Локальные переменные
    set(openwrt_sdk_dir $ENV{HOME}/OpenWRT-sdk)
    
    # Экспортируемая переменная, в скрипт `xcmake.sh`
    set(staging_dir ${openwrt_sdk_dir}/staging_dir)
    set(toolchain_dir ${staging_dir}/toolchain-mipsel_24kc_gcc-7.3.0_musl)
    ...
    

    You have to check/modify OpenWRT SDK paths.

    Next run cmake with parameters:

    	cmake -DCMAKE_TOOLCHAIN_FILE=./cross-compile/openwrt-mips-toolchain.cmake [--config Debug] [-DREMOTE_INSTALL_IP=<remote-IP> -DREMOTE_INSTALL_USER=root]
    

    Parameters REMOTE_INSTALL_* will make targets to help you copy binary files to the Omega via rsync. You also need SSH access by key.

    To build (cross-compile) project you have to run script:

    ./xcmake.sh --build . [--clean-first]
    

    Step 3 (optional). Remote-installation

    If you set REMOTE_INSTALL_* parameters, after build you able using remote-install, remote-uninstall targets for example below:

    cmake --build . --target remote-install
    

    or

    make remote-install
    

    Step 4 (optional). Making IPK-package

    After cross-compilation you will see files openwrt-manifest-git.mk, openwrt-manifest-local.mk in the root project directory. There are two variants of manifest (renamed Makefile).

    • openwrt-manifest-git.mk - variant of manifest file of project to make package that sources will be download from Git repository.

    • openwrt-manifest-local.mk - variant of manifest file of project to make package localy. It's useful for debugging or internal using.

    To make package, first you have to create feed directory outside this project, e.g. $HOME/my-openwrt-packages. Create sub-directory in it with you project name, e.g. $HOME/my-openwrt-packages/<my_app_name>

    Then make symlink there for one of the Manifest file, e.g.:

    ln -sf $HOME/projects/<my_app_name>/openwrt-manifest-local.mk $HOME/my-openwrt-packages/<my_app_name>/Makefile
    

    Next change working dir to the root of OpenWRT SDK. Open file feeds.conf.default in any text editor and append your own feed there like below string.

    ...
    src-link my_packages /home/<my_user_name>/projects/my-openwrt-packages
    

    Then run once sequence of commands:

    ./scripts/feeds update my_packages
    ./scripts/feeds install <my_app_name>
    

    Now you can build IPK-package:

    • Variant openwrt-manifest-local.mk:

      make package/<my_app_name>/prepare
      make package/<my_app_name>/compile
      
    • Variant openwrt-manifest-git.mk:

      make package/<my_app_name>/download
      make package/<my_app_name>/compile
      

    Your IPK-files you can find in the directory <OpenWRT_SDK_dir>/bin/packages/mipsel_24kc/my_packages. It will named like <my_app_name>_1.0-1_mipsel_24kc.ipk

    To install package from IPK-file in the Omega, run next command:

    opkg install ./<my_app_name>_1.0-1_mipsel_24kc.ipk


Looks like your connection to Community was lost, please wait while we try to reconnect.