当然这个是我们在实践中,在cmd窗口中能看到文件生成流程,让我们知道ndk-build生成elf可执行文件原理:对于后期在破解中更能得到启发;
写一个简单的冒泡算法:(结尾:.c结尾)maopao.c
#include "stdio.h"
#include "assert.h"
void maopao(int *arr, int sz){
int i =0;
int j =0;
assert(arr); //判断异常
for(i=0; i<sz-1; i++){
for(j=0; j<sz-i-1; j++){
if(arr[j] > arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
int main(){
int i =0;
int arr[] ={111,3,3,4,5,6,7};
int as = sizeof(arr)/sizeof(arr[0]);
maopao(arr,as);
printf("从小到大:");
for (i=0; i<as; i++){
printf(" %d",arr[i]);
}
printf("\n");
getchar();
}
Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_MODULE := maopao
LOCAL_SRC_FILES := maopao.c
include $(BUILD_EXECUTABLE)
LOCAL_CFLAGS += -pie -fPIE
LOCAL_LDFLAGS += -pie -fPIE
如果是生成.so文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := maopao #模块名称
LOCAL_SRC_FILES := maopao.c #源文件 .c或者.cpp
LOCAL_ARM_MODE := arm #编译后的指令集 ARM指令
LOCAL_LDLIBS += -llog #依赖库
include $(BUILD_SHARED_LIBRARY) #指定编译文件的类型
Application.mk文件
APP_ABI := x86 armeabi-v7a
![debugwj](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
注意:将三个文件放入jni文件里面,文件名jni一定要相同;
然后进入cmd中:
![ndk-build](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
使用adb devices进入模拟器或者原生安卓手机;
将生成maopao文件adb push xxxxx /data/local/tmp中;
赋予权限:chmod 777 maopao;
./maopao执行即可;
![pie](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
执行中出现pie问题出现:参考 运行elf文件出错 error
![执行成功](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
该文章属于:计算机技术网原创
转载请注明标题:如何将.c文件或者c代码利用ndk-build来生成elf可执行文件-计算机技术网
转载请注明出处:https://www.ctvol.com/asreverse/9931.html