How to Build C/C++ Code for Raspberry Pi and Android on Windows

If you are still focusing on Windows desktop development, it is time to migrate to IoT and mobile technologies that have a big boost in the near future. In this post, let’s take a glimpse of how to build C/C++ “hello world” program for Raspberry Pi and Android on Windows using GNU toolchain and NDK.

Windows Toolchain for Raspberry Pi

Download and install raspberry-gcc4.6.3.exe:

E:\SYSGCC\RASPBERRY\BIN
    arm-linux-gnueabihf-addr2line.exe
    arm-linux-gnueabihf-ar.exe
    arm-linux-gnueabihf-as.exe
    arm-linux-gnueabihf-c++.exe
    arm-linux-gnueabihf-c++filt.exe
    arm-linux-gnueabihf-cpp.exe
    arm-linux-gnueabihf-elfedit.exe
    arm-linux-gnueabihf-g++.exe
    arm-linux-gnueabihf-gcc-4.6.exe
    arm-linux-gnueabihf-gcc.exe
    arm-linux-gnueabihf-gcov.exe
    arm-linux-gnueabihf-gdb.exe
    arm-linux-gnueabihf-gprof.exe
    arm-linux-gnueabihf-ld.bfd.exe
    arm-linux-gnueabihf-ld.exe
    arm-linux-gnueabihf-nm.exe
    arm-linux-gnueabihf-objcopy.exe
    arm-linux-gnueabihf-objdump.exe
    arm-linux-gnueabihf-ranlib.exe
    arm-linux-gnueabihf-readelf.exe
    arm-linux-gnueabihf-run.exe
    arm-linux-gnueabihf-size.exe
    arm-linux-gnueabihf-strings.exe
    arm-linux-gnueabihf-strip.exe
    make.exe
    test.exe

Add %SysGCC%\Raspberry\bin to the system environment.

Create dynamsoft.c:

#include <stdio.h>

int main()
{
    printf("Hello Raspberry Pi\n");
    return 0;
}

Build the program:

arm-linux-gnueabihf-gcc dynamsoft.c -o dynamsoft

Download and install SmarTTY.

Connect to Raspberry Pi:

smartty ssh connection

Upload the program file to Raspberry Pi:

smartty file upload

Change the permission to run the program:

smartty execute program

Windows NDK for Android

Run AVD manager to create Android virtual devices on Windows:

android emulator

Considering the performance of Android emulator, you can create a new AVD with Intel x86 Atom_64 System Image instead of ARM EABI v7a System Image.

To build native C/C++ applications for Android, you need to use NDK. NDK simplifies the rules for writing complicated Makefile.

Create a project:

F:\ANDROID

└─jni
    Android.mk
    Application.mk
    dynamsoft.c

According to your target AVDs, define ABI in Application.mk:

APP_ABI := x86_64 armeabi-v7a

Create Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := dynamsoft
LOCAL_SRC_FILES := dynamsoft.c
include $(BUILD_EXECUTABLE)

Create dynamsoft.c:

#include <stdio.h>

int main(void)
{
    printf("Hello Android\n");
    return 0;
}

Build the source code with ndk-build:

ndk build

Create a tmp folder on emulator:

adb shell
mkdir tmp

If you see the error “Read-only file system”, make it writable as follows:

mount -o rw,remount /

Push the file to emulator:

adb push f:\android\libs\x86_64\dynamsoft /tmp

Change the permission to run the program:

android exe