Cmake
一、环境配置
1、NDK:
ctrl alt shift + s 里SDK Location里可以下载,或者在官网上下载后,直接在local.properties里配置下就可以
ndk.dir=/home/sdduser/Android/Sdk/ndk/21.4.7075529
2、gradle配置:
在app的build.gradle里设置Cmake路径:
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}
二、cmake文件CMakeLists.txt
# 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.
##ZSG cmake 的版本
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
#ZSG 项目名字
project("myCpp")
#ZSG 设置源文件
file(GLOB SOURCE ${CMAKE_SOURCE_DIR}/*.cpp ${CMAKE_SOURCE_DIR}/*.c)
# 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.
#zsg 编译时添加-g 就有了调试信息,可以用ddd调试
set(CMAKE_CXX_FLAGS "-g3")
#zsg编译成so库 最终生成的so的名字
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).
${SOURCE} )
# 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} )]]
#add_executable(app_src_main_cpp_myCpp ${CMAKE_SOURCE_DIR}/myCpp.cpp)
#zsg编译成可执行文件 最终生成的可执行文件的名字
add_executable(myCpp ${SOURCE})
三、编译:
在CMakeLists.txt目录下打开终端,直接执行cmake 会生成对应Makefile,
然后在执行make即可生成对应文件
四、运行:
./ 或者 ddd
五、ddd
安装:sudo apt-get install ddd
六、在AS中通过创建library编译so
build.gradle:
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.xxxxx.xxxxxmodule'
compileSdk 32
defaultConfig {
minSdk 28
targetSdk 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
}
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
}
externalNativeBuild {
cmake {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
ndkVersion '21.4.7075529'
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.9.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
INCLUDE_DIRECTORIES(
.
)
add_library(
lalalaxxxxx
SHARED
xxxxxx.c //zsg 源文件
)
find_library(
log-lib
log)
target_link_libraries(
lalalaxxxxx
GLESv3
EGL
${log-lib})
so 生成路径 : build/intermediates/cmake
七、两个算法:
//
// Created by sdduser on 2022/6/9.
//
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main2() {
for (int i = 0; i < 10; ++i) {
cout<<"ila: "<< i <<endl;
}
int a;
double s,t,x;
cout<<"请输入要扩展到第几项:"<<endl;
int n;
scanf("%lf",&x);
s = x;
t = x;
n = 1;
do {
n+=2;
t*= (-x * x) / ((float) (n) - 1) / (float) n;//注意这里第二个除号,是乘在了分母里
s+= t;
} while (fabs(t) >= 1e-7);
printf("sin(%lf) = %lf\n", x, s);
printf("a = %d\n", a);
switch (a) {
case 1:
cout<<a<<endl;
break;
case 2:
cout<<"lala"<<endl;
break;
default:
cout<<"haha"<<endl;
}
return 0;
}
//
// Created by sdduser on 2022/6/10.
/*一元二次方程求解*/
/*分a == 0 和a != 0 的情况即可*/
//
#include <stdio.h>
#include <math.h>
#include <iostream>
//ax^2 +bx +c = 0
using namespace std;
int main111(){
float a,b,c;
float x1,x2;
float s;
cout<<"请输入常数项a b c"<<endl;
scanf("%f%f%f", &a, &b, &c);
if (a == 0) {
if (b != 0) {
x1 = -c/b;
} else if (c =0) {
cout<<"常数项不应该全为零"<<endl;
return 0;
} else {
cout<<"c不可能等于0"<<endl;
return 0;
}
} else {
s = pow(b,2) - 4*a*c;
if (s > 0) {
x1 = -0.5 * (b + sqrt(s)) / a;//zsg 这里必须是-0.5 不能用-1/2 float型会舍去变成0
x2 = -0.5 * (b - sqrt(s)) / a;
} else if (s == 0) {
x1 = x2 = -0.5 * b / a;
} else {
cout<<"方程无实根"<<endl;
return 0;
}
}
cout<<"x1 = "<<x1<<" x2 = "<<x2<<endl;
return 0;
};