How to do it...

We will create a simple program that can detect the endianness of the target platform. Follow these steps to do so:

  1. In your working directory, that is, ~/testcreate a subdirectory called endianness.
  2. Use your favorite text editor to create a file called loop.cpp in the loop subdirectory. Copy the following code snippet into the endianness.cpp file:
#include <iostream>

int main() {
union {
uint32_t i;
uint8_t c[4];
} data;
data.i = 0x01020304;
if (data.c[0] == 0x01) {
std::cout << "Big-endian" << std::endl;
} else {
std::cout << "Little-endian" << std::endl;
}
}
  1. Create a file called CMakeLists.txt in the loop subdirectory with the following content:
cmake_minimum_required(VERSION 3.5.1)
project(endianness)
add_executable(endianness endianness.cpp)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

SET(CMAKE_CXX_FLAGS "--std=c++11")
set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabi-g++)

  1. Build the application and copy the resulting executable binary to the target system. Use the recipes from Chapter 2Setting Up the Environment, to do so.
  2. Switch to the target system's Terminal. Log in using your user credentials, if needed.
  3. Run the binary.