Linux嵌入式学习笔记二
实现Ubuntu交叉编译程序并通过SSH发送到Nvida Nano上运行程序
前言
最近在学习嵌入式Linux,遇到一些问题想要记录下来,实现Ubuntu交叉编译程序并通过SSH发送到Nvida Nano上运行程序,设备:一台装有Ubuntu系统(主机)的电脑,一个Nvida Nano。
一、交叉编译是什么?
由于嵌入式系统资源匮乏,一般不能像PC一样安装本地编译器和调试器,不能在本地编写,编译和调试自身运行的程序,而需借助其他系统如PC来完成这些工作,这样的系统通常称为宿主机。 宿主机通常是 Linux 系统,并安装交叉编译器、调试器等工具;宿主机也可以是 Windows系统,安装嵌入式 Linux 集成开发环境。在宿主机上编写和编译代码,通过串口、网口或者硬件调试器将程序下载到目标系统里面运行 所谓的交叉编译,就是在宿主机平台上使用某种特定的交叉编译器,为某种与宿主机不 同平台的目标系统编译程序,得到的程序在目标系统上运行而非在宿主机本地运行。
通俗点说就是:一般的电脑采用因特尔的CPU为X86架构,而Linux嵌入式系统为ARM架构,所以X86架构的编译出来的文件不可以给ARM架构的运行,所以要采用交叉编译来实现X86架构下的编译文件可以给ARM架构运行
二、安装交叉编译工具
采用的交叉编译工具为:aarch64-linux-gnu-gcc
1.查看可以安装的版本
在终端上输入:apt-cache search aarch64
有如下版本可以安装
cpp-9-aarch64-linux-gnu - GNU C preprocessor
cpp-aarch64-linux-gnu - GNU C preprocessor (cpp) for the arm64 architecture
g++-9-aarch64-linux-gnu - GNU C++ compiler (cross compiler for arm64 architecture)
g++-aarch64-linux-gnu - GNU C++ compiler for the arm64 architecture
gcc-9-aarch64-linux-gnu - GNU C compiler (cross compiler for arm64 architecture)
gcc-9-aarch64-linux-gnu-base - GCC,GNU 编译器套装(基本软件包)
gcc-aarch64-linux-gnu - GNU C compiler for the arm64 architecture
binutils-aarch64-linux-gnu - GNU binary utilities, for aarch64-linux-gnu target
binutils-aarch64-linux-gnu-dbg - GNU binary utilities, for aarch64-linux-gnu target (debug symbols)
qemu-efi-aarch64 - UEFI firmware for 64-bit ARM virtual machines
qemu-system-arm - QEMU full system emulation binaries (arm)
cpp-10-aarch64-linux-gnu - GNU C 预处理器
g++-10-aarch64-linux-gnu - GNU C++ compiler (cross compiler for arm64 architecture)
gcc-10-aarch64-linux-gnu - GNU C compiler (cross compiler for arm64 architecture)
gcc-10-aarch64-linux-gnu-base - GCC,GNU 编译器套装(基本软件包)
gccgo-10-aarch64-linux-gnu - GNU Go compiler
gccgo-9-aarch64-linux-gnu - GNU Go compiler
gccgo-aarch64-linux-gnu - Go compiler (based on GCC) for the arm64 architecture
cpp-8-aarch64-linux-gnu - GNU C preprocessor
g++-8-aarch64-linux-gnu - GNU C++ compiler (cross compiler for arm64 architecture)
gcc-10-plugin-dev-aarch64-linux-gnu - Files for GNU GCC plugin development.
gcc-8-aarch64-linux-gnu - GNU C compiler (cross compiler for arm64 architecture)
gcc-8-aarch64-linux-gnu-base - GCC, the GNU Compiler Collection (base package)
gcc-8-plugin-dev-aarch64-linux-gnu - Files for GNU GCC plugin development.
gcc-9-plugin-dev-aarch64-linux-gnu - Files for GNU GCC plugin development.
gccgo-8-aarch64-linux-gnu - GNU Go compiler
gdc-10-aarch64-linux-gnu - GNU D compiler (version 2) (cross compiler for arm64 architecture)
gdc-8-aarch64-linux-gnu - GNU D compiler (version 2) (cross compiler for arm64 architecture)
gdc-9-aarch64-linux-gnu - GNU D compiler (version 2) (cross compiler for arm64 architecture)
gdc-aarch64-linux-gnu - GNU D compiler (based on GCC) for the arm64 architecture
gfortran-10-aarch64-linux-gnu - GNU Fortran compiler
gfortran-8-aarch64-linux-gnu - GNU Fortran compiler
gfortran-9-aarch64-linux-gnu - GNU Fortran compiler
gfortran-aarch64-linux-gnu - GNU Fortran 95 compiler for the arm64 architecture
gm2-10-aarch64-linux-gnu - GNU Modula-2 compiler (cross compiler for arm64 architecture)
gm2-9-aarch64-linux-gnu - GNU Modula-2 compiler (cross compiler for arm64 architecture)
gm2-aarch64-linux-gnu - GNU Modula-2 compiler (based on GCC) for the arm64 architecture
gnat-10-aarch64-linux-gnu - GNU Ada compiler
gnat-8-aarch64-linux-gnu - GNU Ada compiler
gnat-8-sjlj-aarch64-linux-gnu - GNU Ada compiler (setjump/longjump runtime library)
gnat-9-aarch64-linux-gnu - GNU Ada compiler
gobjc++-10-aarch64-linux-gnu - GNU Objective-C++ compiler
gobjc++-8-aarch64-linux-gnu - GNU Objective-C++ compiler
gobjc++-9-aarch64-linux-gnu - GNU Objective-C++ compiler
gobjc++-aarch64-linux-gnu - GNU Objective-C++ compiler for the arm64 architecture
gobjc-10-aarch64-linux-gnu - GNU Objective-C compiler
gobjc-8-aarch64-linux-gnu - GNU Objective-C compiler
gobjc-9-aarch64-linux-gnu - GNU Objective-C compiler
gobjc-aarch64-linux-gnu - GNU Objective-C compiler for the arm64 architecture
pkg-config-aarch64-linux-gnu - manage compile and link flags for libraries for arm64 architecture
qemu-efi - transitional dummy package
2.选择某个版本安装
apt-get install gcc-10-aarch64-linux-gnu
3.验证是否安装成功
如下图所示。
dengbei@dengbei-ThinkPad-E480:~$ aarch64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/aarch64-linux-gnu/9/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
4.编译程序
事先在ubuntu上写了一个hello.c程序
#include <stdio.h>
int main(int argc, char **argv)
{
if (argc >= 2)
printf("Hello, %s!\n", argv[1]);
else
printf("Hello, world!\n");
return 0;
}
dengbei@dengbei-ThinkPad-E480:~$ aarch64-linux-gnu-gcc hello.c
dengbei@dengbei-ThinkPad-E480:~$ aarch64-linux-gnu-gcc hello.c -o hello
会出现一个名叫hello的可执行文件,为64位的。
engbei@dengbei-ThinkPad-E480:~$ file hello
hello: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=631c735d078c1f9b83a11ed94b4e919e54393346, for GNU/Linux 3.7.0, not stripped
三、传输可执行文件并运行程序
通过SSH将交叉编译出来的可执行文件发送Nvida Nano然后运行程序 关于SSH连接的教程:SSH教程
vidia@nvidia-desktop:~$ sudo scp dengbei@192.168.1.111:/home/dengbei/hello ./
[sudo] password for nvidia:
dengbei@192.168.1.111's password:
hello 100% 9336 1.7MB/s 00:00
nvidia@nvidia-desktop:~$ ls
Desktop Downloads hello Music Public Videos
Documents examples.desktop hello.c Pictures Templates
nvidia@nvidia-desktop:~$ ./hello
Hello, world!
|