- 在(ANDROID)/external/hello目录下编写hello.c文件,helloworld.c的内容如下:
#include <sys/types.h>
// #include <sys/state.h> //无法找到此头文件
#include <stdio.h>
#include <fcntl.h>
#define DEV_NAME "/dev/testdriver"
int main(int argc, char const *argv[])
{
int fd, num;
// fd = open(DEV_NAME, O_RDWR, S_IRUSR|S_IWUSR); //S_IRUSR|S_IWUSR需要使用到头文件sys/state.h
fd = open(DEV_NAME, O_RDWR);
if (fd < 0) {
printf("Open device fail!\n");
return -1;
}
read(fd, &num, sizeof(int));
printf("The num is: %d\n", num);
printf("Please input a number written to testdriver: \n");
scanf("%d", &num);
write(fd, &num, sizeof(int));
read(fd, &num, sizeof(int));
printf("The num is: %d\n", num);
close(fd);
return 0;
}
- 在$(ANDROID)/external/hello目录入创建Android.mk文件,内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := helloworld.c
LOCAL_MODULE := helloworld
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
- 回到$(ANDROID)根目录,执行下面命令编译:
make helloworld
- 编译后的可执行文件存放在out/target/product/rk312x/system/bin/hellodriver目录。
- 将helloworld可执行文件push到system/bin目录,执行。