Intel® C++ Compiler 19.0 Developer Guide and Reference Supplement for Intel® System Studio 2019

Building Android* Applications from the Command Line

This topic applies to C/C++ on Linux* and Windows* host systems.

You can build applications for Intel architecture-based Android* devices in two ways:

Note

For Android* version 5.0 and later, you must use the -fpie and -pie options to generate position-independent code while building dynamically linked executables with the Intel® C++ Compiler for Intel® System Studio.

Install the 64-bit Android* NDK from http://developer.android.com/sdk/ndk/index.html or the Android* Open Source Project (AOSP) from http://source.android.com/. Refer to the Release Notes for supported NDK versions.

When you install the Intel® C++ Compiler, the installation program integrates the compiler into the Android* NDK build system automatically.

Working with a GCC Environment

The Intel® C++ Compiler now uses a Clang-only environment. The GCC environment is no longer supported.

Using the Intel® C++ Compiler Toolchain as a Standalone Compiler

The Intel C++ Compiler requires a Clang compiler toolchain for the Android* OS to work.

On Linux* host systems, set up the environment for the Intel compiler using the compilervars file. See Using compilervars File for details.

On a Windows* host system, perform the following steps :

  1. Open the setup environment window, from Start Menu > Intel System Studio 2019 for Linux and Android* targets > Compiler and Performance Libraries > Command Prompt with Intel Compiler v19.0 > IA-32 Android [or Intel 64 Android].

    A command prompt window opens up with the prompt at C:\Program Files (x86)\IntelSWTools\system_studio_2019.

  2. Change directory to \compilers_and_libraries_2019.x.yyy\windows\bin where the compilervars.bat is located.

  3. Set up the environment for the Intel Compiler using the compilervars.bat file. See Using compilervars File for details.

If you see errors requesting to set up ANDROID_SYSROOT and ANDROID_GNU_X86_TOOLCHAIN environment variables, the possible reason is that you used an unsupported NDK version during installation. In such a case, you can set up the --sysroot and -gnu-prefix compiler options manually.

Using -gnu-prefix and --sysroot Compiler Options for Cross-Compiling

This method uses the --sysroot and -gnu-prefix compiler options instead of setting environment variables and using the -platform compiler option. You can use this method for all versions of Android*.

The --sysroot option specifies the target root directory of headers and libraries. For example, if the headers and libraries are normally located in /usr/include and /usr/lib respectively, --sysroot=/mydir causes the compiler to search in /mydir/usr/include and /mydir/usr/lib for the headers and libraries.

The -gnu-prefix option specifies a string that prepends the name of GNU tools called from the Intel® C++ Compiler. The value depends on the GNU toolchain used for a particular operating system. For example, for the Android* platform, the prefix value, for a 64-bit target, is x86_64-linux-android-. Append a hyphen to the prefix only if the toolchain prefix ends with a hyphen.

You need to add the Clang binaries to PATH.

For example, to set the environment variables for 64-bit Android* targets, execute the following commands:

//On Linux* host

export PATH=/<android_ndk_dir>/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin:$PATH
export PATH=/<android_ndk_dir>/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
export SYSROOT=<android_ndk_dir>/platforms/android-<api_level>/arch-x86_64
export GNU_PREFIX=x86_64-linux-android-

//On Windows* host

set PATH=C:\<android_ndk_dir>\toolchains\x86_64-4.9\prebuilt\windows-x86_64\bin;%PATH%
set PATH=C:\<android_ndk_dir>\toolchains\llvm\prebuilt\windows-x86_64\bin;%PATH%
set SYSROOT=C:\<android_ndk_dir>\platforms\android-<api_level>\arch-x86_64
set GNU_PREFIX=x86_64-linux-android-
		

Use icc/icl from the command line with the --sysroot and -gnu-prefix compiler options, as follows:

icc my_source_file.c -gnu-prefix=$GNU_PREFIX --sysroot=$SYSROOT      //Linux* host
icl my_source_file.c -gnu-prefix=%GNU_PREFIX% --sysroot=%SYSROOT%    //Windows* host

If you installed the 32-bit version of the Android* NDK, set the environment variables as follows:

//On Linux* host

export PATH=/<android_ndk_dir>/toolchains/x86-4.9/prebuilt/linux-x86_64/bin:$PATH
export PATH=/<android_ndk_dir>/toolchains/llvm/prebuilt/linux-x86/bin:$PATH
export SYSROOT=<android_ndk_dir>/platforms/android-<api_level>/arch-x86
export GNU_PREFIX=i686-linux-android-

//On Windows* host

set PATH=C:\<android_ndk_dir>\toolchains\x86-4.9\prebuilt\windows-x86\bin;%PATH%
set PATH=C:\<android_ndk_dir>\toolchains\llvm\prebuilt\windows-x86\bin;%PATH%
set SYSROOT=C:\<android_ndk_dir>\platforms\android-<api_level>\arch-x86
set GNU_PREFIX=i686-linux-android-

Note

Starting with Android NDK r16, you must specify -D__ANDROID_API__=<API version> for the compile stage as follows:

>>icc -gnu-prefix=$GNU_PREFIX --sysroot=$SYSROOT -D__ANDROID_API__=<API version> -c hello-jni.c
>>icc -gnu-prefix=$GNU_PREFIX --sysroot=$SYSROOT -shared -o libhello-jni.so hello-jni.o

For more details, see Unified Headers at https://android.googlesource.com/platform/ndk/+/ndk-release-r16/docs/UnifiedHeaders.md .

Using the AOSP Environment:

If you are using the AOSP environment on a Linux* host, set the variables as follows:

export TOPDIR= #set to the AOSP root directory (example: /export/JB_WW36) 
export ANDROID_SYSROOT=$TOPDIR/prebuilts/ndk/8/platforms/android-9/arch-x86
export ANDROID_GNU_X86_TOOLCHAIN=$TOPDIR/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.8

Compiling Source Code

You are now ready to use icc/icpc from a command line in the build environment to compile C/C++ source code.

Here is an example on a Linux* host:

$ cd $NDK_DIR/samples/hello-jni/jni 
$ icc -platform=android* -c hello-jni.c 
$ icc -platform=android* –shared -o libhello-jni.so hello-jni.o

Enabling the Intel Toolchain within the Android* NDK Build System

To understand how to enable the Intel toolchain within the Android* NDK build system, follow the steps to build the hello-jni sample application that ships with the Android* NDK. You can locate the sample in $NDK_DIR/samples/hello-jni/jni/.

Build the sample application with the Intel® C++ Compiler either from the command line or using the *.mk file. In either approach, you need to configure APP_ABI if you want to use ICC. If multiple versions of the Intel compiler are installed, a full compiler package version can be specified.

To build an application with the Intel® C++ Compiler 19.0 toolchain using the Android* NDK, do one of the following:

From the Command Line

  1. Open the terminal window and navigate to the jni folder of the sample:

    cd $NDK_DIR/samples/hello-jni

  2. Enter the following command:

    $ $NDK_DIR/ndk-build V=1 -B APP_ABI=x86_64 NDK_TOOLCHAIN:=x86_64-icc  // 64-bit Linux host
    >>%NDK_DIR%\ndk-build V=1 -B APP_ABI=x86_64 NDK_TOOLCHAIN:=x86_64-icc // 64-bit Windows host

    Where:

    V=1 enables verbose output (optional)

    -B means rebuild all (optional)

    APP_ABI indicates target architecture

Using the Application.mk File

  1. Open or create the Application.mk file in your application’s jni folder at $NDK_DIR/samples/hello-jni/jni/.

  2. Add the following lines in Application.mk:

    • For 32-bit targets:

      APP_ABI:=x86
      NDK_TOOLCHAIN:=x86-icc
    • For 64-bit targets:

      APP_ABI:=x86_64
      NDK_TOOLCHAIN:=x86_64-icc
  3. Run the command to build the application:

    $ $NDK_DIR/ndk-build V=1 –B    // on Linux* host
    >>%NDK_DIR%\ndk-build V=1 –B   // on Windows* host