利用Cmake以及NDK-Standalone toolchain生成静态库用于Android C++ 文件~
本博客从某种意义上起着一个备忘录的作用。但是如果能帮助到因为某些问题而在百度上到处乱撞而一无所获的你,也是我本来的初衷。又: 我拿英文写了啊。英文这个东西嘛,要多看才能看的快。所以但愿你不会有抵触情绪~~
1. Introduce
I am tring to use SiftGPU in Android. Though I haven’t reached my goal yet, I have figured out how to build a static library using standalone toolchain together with cmake and use it in the Android project. Here is the process:
2. Build NDK Standalone toolchain
Currently I am using the Ubuntu 17.04. First, download the android-ndk-r15c-linux-x86_64.zip from here: https://developer.android.com/ndk/downloads/index.html
Extract it and enter the android-ndk-r15c folder, run the following command:
sudo sh ./build/tools/make-standalone-toolchain.sh --verbose --platform=android-15
--install-dir=/home/YourUserName/Downloads/my-tool --toolchain=arm-linux-androideabi-4.9
Here is the explaination of this command:
--verbose: show debug messages
--platform=android-15: select the version of Android
--install-dir=/home/YourUserName/Downloads/my-tool:
Notice that you should change "YourUserName" to your own user name.
In my case, it will be generated under my-tool folder.
--toolchain=arm-linux-androideabi-4.9:
The standalone-toolchain files will be generated under this folder.
You can refer to this article to select the toolchain you need:
https://developer.android.com/ndk/guides/standalone_toolchain.html
3. Use Cmake to build a static library
Then, We are going to use cmake and build a static library. Note that the ndk standalone toolchain is not used in this step. First, you can check your cmake version by the following command:
cmake --version
Mine is:
cmake version 3.7.2.
Then, we can build a simple cpp file:
// main.cpp
#include <stdio.h>
#include "Structure.h"
int main() {
int a = 8;
int b = 9;
int c = sum( a, b );
printf( "sum of %d and %d is %d\n", a, b, c );
return 0;
}
After that, we can also build the header file of our function:
//Structure.h
int sum( int a, int b );
And the c++ file of our function:
//Structure.cpp
int sum(