利用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( 
<think>嗯,用户想用android-ndk-r18b进行C++项目的交叉编译配置,我得先回想一下之前处理类似问题的经验。根据用户提供的引用内容,特别是引用[1]和[4],他们提到了使用不同版本的NDK进行编译的情况,比如r19和r17。用户现在用的是r18b,可能需要注意一些配置上的差异。 首先,用户需要正确安装NDK。引用[4]里提到了下载NDK到特定目录,所以应该指导用户下载r18b并设置环境变量。然后,交叉编译通常需要工具链,引用[5]提到了独立工具链的使用,可能需要用make-standalone-toolchain.sh脚本来生成。不过,不同的NDK版本可能有不同的方法,比如新版本可能推荐使用cmake,如引用[1]中的工具链文件android.toolchain.cmake。 接下来,用户可能需要配置CMakeLists.txt,指定工具链文件。引用[1]和[3]都提到了使用CMakeandroid.toolchain.cmake,所以应该告诉用户在CMake命令中设置-DCMAKE_TOOLCHAIN_FILE参数,指向NDK中的toolchain文件。同时,需要注意目标架构,比如armeabi-v7a,这在引用[1]的第三方依赖库中提到过。 另外,用户可能会遇到GLES版本的问题,如引用[1]里osg3.6对GLES3支持不够,需要使用GLES2。所以在编译配置中可能需要相关的编译选项,比如-DANDROID_ARM_NEON=TRUE,或者链接特定的库。 还有,用户可能需要处理第三方依赖库,如引用[1]中的3rdpartyAndroid.zip,需要确保这些库与目标ABI兼容,并且正确包含在项目中。在编译命令中,可能需要通过-DCMAKE_FIND_ROOT_PATH来指定库的路径。 最后,编译成功后如何导入Android项目,引用[2]提到将生成的.so和头文件导入到AS的Native C++项目,可能需要配置CMakeLists.txt中的add_library和target_include_directories,以及修改build.gradle的externalNativeBuild部分。 还要考虑用户可能遇到的问题,比如NDK版本与库的兼容性,如引用[3]中用r19编译opencv3.4.2失败,而r17没问题。虽然用户使用的是r18b,但需要提醒他们如果遇到类似问题,可能需要降级NDK或更新第三方库。此外,清理项目(clean)和重新链接C++部分也是解决编译问题的常见方法,如引用[3]所述。 总结步骤应该是:安装NDK,配置环境变量,生成工具链或使用CMake工具链文件,配置CMakeLists.txt,处理第三方依赖,执行编译命令,处理可能的错误,最后集成到Android Studio项目中。同时,生成相关问题,帮助用户深入理解可能遇到的挑战。</think>### 使用 Android NDK r18b 进行 C++ 交叉编译的配置教程 #### 1. **安装 NDK 并配置环境** - **下载 NDK r18b**:从 [Google NDK 存档](https://developer.android.com/ndk/downloads/older_releases) 获取指定版本,解压到本地目录(例如 `/opt/android-ndk-r18b`)。 - **设置环境变量**: ```bash export ANDROID_NDK_HOME=/opt/android-ndk-r18b export PATH=$PATH:$ANDROID_NDK_HOME ``` #### 2. **选择工具链生成方式** - **方法一:使用 CMake 工具链文件**(推荐) 在 CMake 命令中直接指定 NDK 内置的 `android.toolchain.cmake` 文件: ```bash cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \ -DANDROID_ABI=armeabi-v7a \ -DANDROID_PLATFORM=android-16 \ .. ``` - `ANDROID_ABI`:目标架构(如 `armeabi-v7a`, `arm64-v8a`)[^1] - `ANDROID_PLATFORM`:最低支持的 Android API 级别 - **方法二:创建独立工具链** 使用脚本生成针对特定架构的工具链: ```bash $ANDROID_NDK_HOME/build/tools/make-standalone-toolchain.sh \ --arch=arm \ --api=16 \ --install-dir=/opt/android-toolchain-arm ``` #### 3. **配置 CMakeLists.txt** 在项目的 `CMakeLists.txt` 中添加关键配置: ```cmake cmake_minimum_required(VERSION 3.12) project(MyProject) # 设置交叉编译目标 set(CMAKE_SYSTEM_NAME Android) set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a) set(CMAKE_ANDROID_STL_TYPE gnustl_static) # 添加第三方库路径(参考引用[1]中的第三方依赖) set(3RDPARTY_DIR /path/to/3rdpartyAndroid) include_directories(${3RDPARTY_DIR}/include) link_directories(${3RDPARTY_DIR}/lib) ``` #### 4. **处理 GLES 兼容性问题** 若涉及图形库(如 OpenSceneGraph),需注意: ```cmake # 强制使用 GLES2(参考引用[1]中的代码示例) add_definitions(-DOSG_GLES2_AVAILABLE) target_link_libraries(my_target GLESv2) ``` #### 5. **执行编译命令** ```bash mkdir build && cd build cmake .. -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \ -DANDROID_ABI=armeabi-v7a \ -DANDROID_ARM_NEON=TRUE make -j4 ``` #### 6. **集成到 Android 项目** 编译完成后: -生成的 `.so` 文件放入 `app/src/main/jniLibs/<abi>/` - 在 `build.gradle` 中配置: ```groovy android { defaultConfig { externalNativeBuild { cmake { arguments "-DANDROID_STL=c++_shared" abiFilters 'armeabi-v7a' } } } } ``` #### 常见问题处理 - **NDK 版本兼容性**:如果编译失败,尝试清理项目(`Build > Clean Project`)或降低 NDK 版本(参考引用[3]的案例)[^3] - **C++ 异常支持**:在链接时添加 `-lsupc++`(参考引用[5])[^5] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值