android下的c程序

来源:互联网 发布:葡萄牙语翻译软件 编辑:程序博客网 时间:2024/06/05 05:19

http://blog.csdn.net/luoshengyang/article/details/6571210

1 源代码

derek@u10:~/ics/ics-4.0.4/external/hello$ ls
Android.mk  hello.c

hello.c

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#define DEVICE_NAME "/dev/hello"int main(int argc, char** argv){int fd = -1;int val = 0;fd = open(DEVICE_NAME, O_RDWR);if(fd == -1) {printf("Failed to open device %s.\n", DEVICE_NAME);return -1;}printf("Read original value:\n");read(fd, &val, sizeof(val));printf("%d.\n\n", val);val = 5;printf("Write value %d to %s.\n\n", val, DEVICE_NAME);        write(fd, &val, sizeof(val));printf("Read the value again:\n");        read(fd, &val, sizeof(val));        printf("%d.\n\n", val);close(fd);return 0;}

Android.mk  

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_MODULE := helloLOCAL_SRC_FILES := $(call all-subdir-c-files)include $(BUILD_EXECUTABLE)

2 编译

derek@u10:~/ics/ics-4.0.4/$ .  ./build/envsetup.sh

derek@u10:~/ics/ics-4.0.4/$ mmm ./external/hello

derek@u10:~/ics/ics-4.0.4/$ make snod


3 运行虚拟机,测试bin文件

derek@u10:~/ics/ics-4.0.4$ adb shell# # cd /bincd: can't cd to /bin# helloRead original value:5.Write value 5 to /dev/hello.Read the value again:5.