美文网首页
编译Android平台运行的C程序

编译Android平台运行的C程序

作者: 爱因私谈 | 来源:发表于2019-01-07 11:34 被阅读0次
  1. (ANDROID)/external/目录入新建代码目录,比如:hello,在(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;
}
  1. 在$(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)
  1. 回到$(ANDROID)根目录,执行下面命令编译:
make helloworld
  1. 编译后的可执行文件存放在out/target/product/rk312x/system/bin/hellodriver目录。
  2. 将helloworld可执行文件push到system/bin目录,执行。

相关文章

网友评论

      本文标题:编译Android平台运行的C程序

      本文链接:https://www.haomeiwen.com/subject/mbzyrqtx.html