Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
blog:2017:0913_cross_compiling_c_libraries_for_android_with_cmake [2017/09/13 09:29] – manu | blog:2017:0913_cross_compiling_c_libraries_for_android_with_cmake [2021/09/02 13:36] (current) – Discussion status changed manu | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~DRAFT~~ | ||
- | |||
====== Cross compiling C++ libraries for Android with CMake ====== | ====== Cross compiling C++ libraries for Android with CMake ====== | ||
+ | {{tag> | ||
+ | |||
+ | As I am now trying to get a better understanding on the Unreal Engine and other major 3D engines it might be time to start considering building libraries and apps properly for other platforms than Windows and Linux. A good choice from that perspective would be Android as this doesn' | ||
+ | |||
+ | I already have abase project called NervSeed that can be used as a base project I want to build with multiple platform support, including Android. So now its time to setup the required environment to support cross compiling this project to Android either from Windows or from Linux. | ||
+ | |||
+ | ====== ====== | ||
+ | |||
+ | ====== Setting up the Android compilation toolchain ====== | ||
+ | |||
+ | First thing we obviously need is the Android NDK: | ||
+ | * NDK can be retrieved as part of a CodeWorks installation | ||
+ | * Or it can be [[https:// | ||
+ | |||
+ | According the the Unreal Engine [[https:// | ||
+ | |||
+ | So would need to retrieve the latest CodeWorks release (**version 1R6** currently) from the [[https:// | ||
+ | |||
+ | Important note: we should upgrade to at least cmake version 3.7 to ensure we get the appropriate support for Android targets: Updated cmake to version 3.9.2 in NervSeed project: **OK** | ||
+ | |||
+ | Let's now extend our build script to support an Android target: | ||
+ | |||
+ | So we generated this kind of toolchain file for cmake in case android target is specified:< | ||
+ | set(CMAKE_SYSTEM_NAME $system) | ||
+ | set(CMAKE_SYSTEM_VERSION 21) # API level | ||
+ | set(CMAKE_ANDROID_ARCH_ABI arm64-v8a) | ||
+ | set(CMAKE_ANDROID_NDK $ndkPath) | ||
+ | set(CMAKE_ANDROID_STL_TYPE gnustl_static) | ||
+ | EOF</ | ||
+ | |||
+ | Above the " | ||
+ | |||
+ | But we get this error from cmake: < | ||
+ | CMake flags: | ||
+ | CMake Error at build/ | ||
+ | Syntax error in cmake code at | ||
+ | |||
+ | D:/ | ||
+ | |||
+ | when parsing string | ||
+ | |||
+ | D: | ||
+ | |||
+ | Invalid escape sequence \A | ||
+ | Call Stack (most recent call first): | ||
+ | tools/ | ||
+ | CMakeLists.txt: | ||
+ | |||
+ | |||
+ | CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage | ||
+ | CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage | ||
+ | -- Configuring incomplete, errors occurred! | ||
+ | make: *** No rule to make target ' | ||
+ | Build completed in 0 seconds.</ | ||
+ | |||
+ | Obviously a stupid escape sequence error... maybe it would be better with quotation marks on our CMAKE_ANDROID_NDK definition line ? => nope, this is not any better. | ||
+ | |||
+ | So just replaced the backward slashes with forward slashes: < | ||
+ | |||
+ | And now cmake seems to find the Android compiler appropriately: | ||
+ | tr: warning: an unescaped backslash at end of string is not portable | ||
+ | CMake flags: | ||
+ | -- Android: Targeting API ' | ||
+ | -- Android: Selected GCC toolchain ' | ||
+ | -- The C compiler identification is GNU 4.9.0 | ||
+ | -- The CXX compiler identification is GNU 4.9.0 | ||
+ | -- Check for working C compiler: D:/ | ||
+ | -- Check for working C compiler: D:/ | ||
+ | -- Detecting C compiler ABI info | ||
+ | -- Detecting C compiler ABI info - done | ||
+ | -- Detecting C compile features | ||
+ | -- Detecting C compile features - done | ||
+ | -- Check for working CXX compiler: D:/ | ||
+ | -- Check for working CXX compiler: D:/ | ||
+ | -- Detecting CXX compiler ABI info | ||
+ | -- Detecting CXX compiler ABI info - done | ||
+ | -- Detecting CXX compile features | ||
+ | -- Detecting CXX compile features - done</ | ||
+ | |||
+ | ====== Fixing NervSeed project compilation errors ====== | ||
+ | |||
+ | Unfortunately we have some compilation errors after that, but this is to be expected as the GCC compiler version is only 4.9.0 in this toolchain... hopefully CodeWorks release 1R6 woud provide a better compiler ? | ||
+ | |||
+ | => Well according to [[https:// | ||
+ | |||
+ | * We had a problem in my DebugHandler class which would call exit(), and connect to signals, etc: I would not be surprised if this is all not available in an Android context, so for now, just not compiling this code at all using an **#ifndef __ANDROID__ | ||
+ | * Next issue is: < | ||
+ | * Found this [[https:// | ||
+ | * Also see: https:// | ||
+ | set(CMAKE_SYSTEM_NAME $system) | ||
+ | set(CMAKE_SYSTEM_VERSION 21) # API level | ||
+ | # | ||
+ | set(CMAKE_ANDROID_NDK " | ||
+ | # | ||
+ | set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang) | ||
+ | EOF</ | ||
+ | |||
+ | * FIXME: check if we can still (and if we should) specify the ARCH_ABI and STL_TYPE entries ? | ||
+ | |||
+ | * We still have the infamous **' | ||
+ | * Including the header **< | ||
+ | * We probably need to upgrade the compiler to fix this, but at the same time this **date.h** header is something far too complex for my current needs, so I should rather just get rid of it. | ||
+ | * Also, clang++ will complain about the provided **-s** command line argument, so we should remove it for this compiler: **OK** | ||
+ | => Apart from those minor issues compiling for Android now seems to work as expected :-) ! So in the end, that wasn't too hard... | ||
- | {{tag>}} | + | ===== References ===== |
+ | * [[https:// | ||
+ | * [[https:// | ||
- | ~~DISCUSSION~~ | + | ~~DISCUSSION:off~~ |