We will create a simple program that can detect the endianness of the target platform. Follow these steps to do so:
- In your working directory, that is, ~/test, create a subdirectory called endianness.
- 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;
}
}
- 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++)
- Build the application and copy the resulting executable binary to the target system. Use the recipes from Chapter 2, Setting Up the Environment, to do so.
- Switch to the target system's Terminal. Log in using your user credentials, if needed.
- Run the binary.