利用Cmake以及NDK-Standalone toolchain生成静态库用于android C++ 文件~

本博客介绍了如何利用Cmake和NDK-Standalone toolchain在Ubuntu 17.04上构建静态库,用于Android C++项目。详细步骤包括下载NDK,使用Cmake构建静态库,以及为Android设备生成ARM架构的库文件。通过修改CmakeLists.txt,最终将静态库应用于Android Studio项目中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用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( 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值