平台用的是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不进去。详细百度。
然后,可以在串口查看android文件系统中是否存在这个文件,push是否成功:
存在,但是直接运行会报权限错误。需要修改下可执行文件的权限。
chmod 777 给了该可执行文件最高权限
运行效果: