c交叉编译成可执行文件在android上运行

平台用的是rk3399-android-10。
C语言小程序在linux上编译运行见:c编译成可执行文件在linux上运行

本文未涉及到命令行传参argc和argv,需要传参的main函数可看上方贴出的linux平台那篇链接。

以下是一个超级简单的例子:本文是不需要用makefile编译的例子,有时小工具只涉及到一个或极少的c文件时,一条指令编译即可。

单独建一个文件夹名为hello_world,里面就一个c文件。

test@ubuntu-07:~/workspace$ ls hello_world/
hello.c

c文件代码:

#include<stdio.h>
int main(void)
{
        printf("hello world\n");
        printf("goodbye\n");
        return 0;
}

编译:因为使用的平台是aarch64架构的,故而编译需要使用到aarch的交叉编译工具链。如果是别的架构就需要对应架构的交叉编译tools

test@ubuntu-07:~/workspace$ cd hello_world/
test@ubuntu-07:~/workspace/hello_world$ aarch64-linux-gnu-gcc -o hello_world --static hello.c
test@ubuntu-07:~/workspace/hello_world$ ls
hello.c  hello_world
test@ubuntu-07:~/workspace/hello_world$ file hello_world 
hello_world: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.10.61, BuildID[sha1]=bf3ff232fec01a5c9db6b037572a1a8514518fd1, not stripped
test@ubuntu-07:~/workspace/hello_world$

以上已经可以看出来是ELF(Executable and Linkable Format)文件了,并且适用于ARM aarch64架构。

如果想查看更多关于这个可执行文件的信息,还可用以下命令readelf:

test@ubuntu-07:~/workspace/hello_world$ readelf --file-header hello_world
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0x4003f0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          4746656 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         6
  Size of section headers:           64 (bytes)
  Number of section headers:         38
  Section header string table index: 37
test@ubuntu-07:~/workspace/hello_world$ 

运行下看看效果。
先通过abd将电脑PC与android平台连接成功,将文件push到android文件系统中。
即:
adb connect 192.168.x.x
adb root
adb connect 192.168.x.x
adb remount
adb push xxx /vendor //不是必须为vendor位置
注:如果android系统是刚烧完的系统,需要adb disable-verity一下,否则push不进去。详细百度。

adb push
然后,可以在串口查看android文件系统中是否存在这个文件,push是否成功:
文件系统中文件列表
存在,但是直接运行会报权限错误。需要修改下可执行文件的权限。
chmod
chmod 777 给了该可执行文件最高权限

运行效果:
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言文件编译可执行文件的过程通常包括以下几个步骤: 1. 编写C语言代码,并保存为一个以.c为后缀名的文件,比如hello.c。 2. 打开终端或命令行界面,进入到保存C代码的文件夹所在目录。 3. 使用C编译器进行编译,最常用的编译器是gcc。 4. 在命令行输入编译命令,例如在Linux环境下,使用gcc -o hello hello.c来将hello.c文件编译可执行文件hello。 5. 执行编译命令后,编译器会将C代码转换机器码,并生一个可执行文件hello。 6. 可执行文件hello就会生在当前目录下,可以通过执行./hello命令来运行可执行文件。 因此,c文件编译可执行文件的过程可以简单归纳为:编写C代码 -> 使用C编译器进行编译 -> 生可执行文件。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C语言文件编译可执行文件过程](https://blog.csdn.net/Frankie0910314/article/details/130620854)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [c语言如何把c程序编译执行的exe文件](https://blog.csdn.net/LxXlc468hW35lZn5/article/details/114609300)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [c#下将.cs文件编译dll](https://download.csdn.net/download/weixin_38686924/14867712)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值