How to build C sample codes

Tutorial to build the C sample code and introduction to Linux toolchain.

Linux Toolchain

Introduction

Linux toolchain contains the necessary libraries and compilers for developing your programs. ioThinx 4530 Series support both native and cross compiling of code. Native compiling is more straightforward since all the coding and compiling can be done directly on the device, but Arm architecture is less powerful, the compiling speed is slower. On the other hand, cross compiling can be done on any Linux machine using a toolchain, and the compiling speed is much faster.

The following compiler tools are provided:

Tool Usage
ar Manage archives (static libraries)
as Assembler
c++, g++ C++ compiler
cpp C preprocessor
gcc C compiler
gdb Debugger
ld Linker
nm Lists symbols from object files
objcopy Copies and translates object files
objdump Displays information about object files
ranlib Generates indexes to archives (static libraries)
readelf Displays information about ELF files
size Lists object file section sizes
strings Prints strings of printable characters from files (usually object files)
strip Removes symbols and sections from object files (usually debugging information)

Native Compilation

The native compiler, ioThinx library and necessary packages are already installed on ioThinx 4530 series devices.

Cross Compilation

To ensure that an application will be able to run correctly when installed on ioThinx 4530 series, you must compile the application and link it to the same libraries that will be present on ioThinx 4530 series. This is particularly true when the Arm-based Cortex processor architecture differs from the CISC x86 processor architecture of the host system, but it is also true if the processor architecture is the same.

The host toolchain that comes with the ioThinx 4530 series devices contains a suite of cross compilers, ioThinx library and other tools, as well as the libraries and headers that are necessary to compile applications for the ioThinx 4530 series devices. The host environment must be running Linux to install the Moxa GNU Tool Chain.

The toolchain will need about 1GB of hard disk space on your PC. To install the toolchain, download the toolchain file from Moxa’s website. After you untar the package, run the following script to install the toolchain.

user@Linux:~$ unzip ioThinx_4530_toolchain_V1.0_Build19030819.zip
user@Linux:~$ sudo ./ioThinx_4530_toolchain_V1.0_Build19030819.sh

You can check the toolchain version using the following command:

user@Linux:~$ /usr/local/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc -v

You can now start compiling programs using this toolchain.


To build the C sample code

Using native toolchain

  1. Copy sample.tar.gz contained in the programing guide from your PC to ioThinx:
    For example, if the IP address of the ioThinx is "192.168.127.254", use the following command:
    user@Linux:~$ scp sample.tar.gz moxa@192.168.127.254:~
  2. Extract the sample code
    moxa@Moxa:~$ tar zxvf sample.tar.gz
  3. Build all sample code
    moxa@Moxa:~$ cd sample/iothinx/c
    moxa@Moxa:~/sample/iothinx/c$ ln -s toolchain-config-native config.mk
    moxa@Moxa:~/sample/iothinx/c$ make
    (You can also build the sample code individually)
    moxa@Moxa:~/sample/iothinx/c$ cd api/io
    moxa@Moxa:~/sample/iothinx/c/api/io$ make di
  4. Run the program
    moxa@Moxa:~/sample/iothinx/c/api/io$ sudo ./di -h

Using cross toolchain

  1. Copy sample.tar.gz contained in the programing guide to your PC
  2. Extract the sample code
    user@Linux:~$ tar zxvf sample.tar.gz
  3. Build all sample code
    user@Linux:~$ cd sample/iothinx/c
    user@Linux:~/sample/iothinx/c$ ln -s toolchain-config-cross config.mk
    user@Linux:~/sample/iothinx/c$ make
    (You can also build the sample code individually)
    user@Linux:~/sample/iothinx/c$ cd api/io
    user@Linux:~/sample/iothinx/c/api/io$ make di
  4. Copy the sample program to ioThinx:
    For example, if the IP address of the ioThinx is "192.168.127.254", use the following command:
    user@Linux:~/sample/iothinx/c/api/io$ scp di moxa@192.168.127.254:~
  5. Run the program on ioThinx
    moxa@Moxa:~$ sudo ./di -h