[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
        apt-get install -y build-essential
      

    NOTICE: Dependent packages automake, autotools-dev, libtool, gettext 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!


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