Android上运行本地c
在android代码中编译一个可执行文件,主要功能是对文件的读写,
简单贴出来:
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- void read_data(void){
- char readBuf[640*1024];
- char *path = "/boot0_nand";
- FILE* fp= fopen(path,"rb");
- if(!fp){
- printf("can‘t open %s\n",path);
- }
- size_t len = fread(readBuf,sizeof(char),sizeof(readBuf),fp);
- printf("read len = %d \n",(int)len);
- fclose(fp);
- }
- int main(int argc, char** argv) {
- int ret;
- int opt;
- read_data();
- }
起初,是用android的编译环境,定义了android.mk文件如下:
- include $(CLEAR_VARS)
- LOCAL_SRC_FILES := writeBoot/write_boot.c
- LOCAL_MODULE := write_boot
- LOCAL_FORCE_STATIC_EXECUTABLE := true
- LOCAL_MODULE_TAGS := eng
- LOCAL_STATIC_LIBRARIES := libstdc++ libc libcutils
- include $(BUILD_EXECUTABLE)
编译通过,但是执行到fread的时候,就报错了:Segmentation
fault
一直搞不明白,那么简单的代码,就报这个错误!
无奈,实在是没办法,就用gcc来编译,方法如下:
- gcc write_boot.c -o write_boot
把write_boot推到小机,修改权限,执行,又报错:not executable: magic
7F45
网上查了一下,通过Android
NDK来编译生成,这个比较简单:
编辑一个脚本如下,命名agcc:
- #!/bin/sh
- OS=‘linux‘
- ANDROIDSDK=‘android-14‘
- ANDROID=‘/home/xiongtie/workspace/A31/android41‘
- PROGDIR=‘/home/xiongtie/workspace/A31/android41/prebuilts/ndk/android-ndk-r7/‘
- PROGDIR=`cd $PROGDIR && pwd`
- ARMEABIGCC=$ANDROID/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/arm-linux-androideabi-gcc
- ARMEABILIB=$PROGDIR/platforms/$ANDROIDSDK/arch-arm/usr/lib
- ARMEABIINC=$PROGDIR/platforms/$ANDROIDSDK/arch-arm/usr/include
- ARMEABICRT=$PROGDIR/platforms/$ANDROIDSDK/arch-arm/usr/lib/crtbegin_dynamic.o
- LINKER=/system/bin/linker
- echo "GCC:"$ARMEABIGCC "LIB:"$ARMEABILIB "LINKER":$LINKER "PARAMS:"$@
- $ARMEABIGCC $@ -Wl,-rpath-link=$ARMEABILIB,-dynamic-linker=$LINKER -L$ARMEABILIB $ARMEABICRT -I$ARMEABIINC -nostdlib -lc
编译名如下:
- ./agcc write_boot.c -o write_boot
再推到小机执行,ok,完成了
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。