日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

Android studio 使用Cmake完成C/C++ 的使用以及生成so文件

發(fā)布時(shí)間:2024/4/15 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android studio 使用Cmake完成C/C++ 的使用以及生成so文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


今天,簡單講講android中關(guān)于Cmake進(jìn)行NDK編程的相關(guān)知識(shí)。


Android studio 2.2版本以后對C/C++的支持可以說很方便了,當(dāng)然官方推薦使用Cmake完成對C/C++的支持

2.2版本以上的同學(xué)新建一個(gè)項(xiàng)目就知道了,步驟如下:

File -> New -> New Project,如下圖:



然后勾選Include C++ support,一直next ,最后Finish以后,項(xiàng)目就出現(xiàn)了,和一般的項(xiàng)目略有不同,其實(shí)只要多了幾個(gè)文件,而已:



1:目錄下多了個(gè)CmakeLists.txt文件

2:src目錄下多了一個(gè)cpp目錄,里面有個(gè).cpp文件,C++文件都是以.cpp結(jié)尾的。

3:就是build.gradle內(nèi)容中添加了幾行配置

一一解讀一下,先來看看CmakeLists.txt 文件里面的內(nèi)容是什么:

# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.cmake_minimum_required(VERSION 3.4.1)# Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.native-lib# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).src/main/cpp/native-lib.cpp )# Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build.find_library( # Sets the name of the path variable.log-lib# Specifies the name of the NDK library that# you want CMake to locate.log )# Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library.native-lib# Links the target library to the log library# included in the NDK.${log-lib} )這里有設(shè)置Cmake版本啊,引用的cpp文件的路徑啊等等,如果自己引用的話 其實(shí)值需要修改引用的cpp路徑即可,

其他的暫時(shí)不需要?jiǎng)?/p>

看下native_lib.cpp文件內(nèi)容:

#include <jni.h> #include <string>extern "C" JNIEXPORT jstring JNICALL Java_com_process_main_myapplication_MainActivity_stringFromJNI(JNIEnv* env,jobject /* this */) {std::string hello = "Hello from C++";return env->NewStringUTF(hello.c_str()); }



很簡單 其實(shí)就是輸出Hello from C++這段文字:

再看下build.gradle的配置改變:

apply plugin: 'com.android.application'android {compileSdkVersion 25buildToolsVersion "25.0.2"defaultConfig {applicationId "com.process.main.myapplication"minSdkVersion 22targetSdkVersion 25versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"externalNativeBuild {cmake {cppFlags ""}}}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}externalNativeBuild {cmake {path "CMakeLists.txt"}} }dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {exclude group: 'com.android.support', module: 'support-annotations'})compile 'com.android.support:appcompat-v7:25.1.1'compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'testCompile 'junit:junit:4.12' }


主要多了兩個(gè)地方的改變:

1:defaultConfig中添加:

externalNativeBuild {cmake {cppFlags ""}}

2:在android{}中添加:

externalNativeBuild {cmake {path "CMakeLists.txt"} }

其實(shí)也就是引用CmakeLists.txt文件。

現(xiàn)在重點(diǎn)講講CmakeLists.txt里面的代碼的含義。



以上的CmakeLists.txt對于與圖片的目錄結(jié)構(gòu),可以看出主要是add_liberary里寫入需要編譯的C文件的路徑,其余的基本不用修改。
還有就是要是在add_liberary中添加.cpp文件的話記得路徑,如果你的CMakeLists和你的.cpp在一個(gè)問價(jià)夾下就不用管路徑。

Android studio 使用Cmake完成C/C++ 的使用以及生成so文件就講完了。
就這么簡單。
 

總結(jié)

以上是生活随笔為你收集整理的Android studio 使用Cmake完成C/C++ 的使用以及生成so文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。