WARNING
📌This tutorial teaches how to compile C and C++ programs in a virtual machine running Ubuntu 18.04.6, and then transfer the compiled files to the Taishan-RK3566 (running Ubuntu 20.04) for execution.
Virtual Machine Ubuntu 18.04 Version Information:
lipeng@lipeng:~/share/TaiShanPai/test$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
2
3
4
5
6
7
8
9
10
11
12
13
1. Virtual Machine Environment Setup
- Update the apt package list in the virtual machine by entering the following command:
sudo apt update
- Install the cross-compilers and the Vim text editor
Vim is a text editor we use for writing programs.
sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu vim
After installation, you can verify whether gcc, g++, and vim have been installed successfully by entering the following commands. gcc-aarch64-linux-gnu:
aarch64-linux-gnu-gcc --version
g++-aarch64-linux-gnu:
aarch64-linux-gnu-g++ --version
Vim:
vim --version
2. Writing and Compiling Programs
We have two cross-compilers:
- gcc cross-compiler:():For compiling C programs
- g++ cross-compiler(): For compiling C++ programs
For C programs, although aarch64-linux-gnu-g++
can also compile them, it will default to linking the C++ standard library, even if the program doesn't require these libraries. This usually doesn't cause issues but will make the resulting executable larger compared to using only the C standard library. If your C program doesn't need the C++ library, using aarch64-linux-gnu-gcc
may be a better choice, as it defaults to linking only the C standard library.
1. Writing and Compiling a C Program
- Create a .c file in the current directory, where we will write the C program.
touch hello.c
- Use the Vim editor to open the hello.c file and write your program.
Note: After entering the vim editor, you cannot start editing directly. You need to press the i key on your keyboard to enter editing mode. Once you've finished writing the program, press the ESC key, then press Shift + : to make a colon appear at the bottom of the vim editor. Now, type wq and press Enter to save and exit.
sudo vi hello.c
- Use the cross-compiler to compile hello.c
sudo aarch64-linux-gnu-gcc hello.c
After compiling, an a.out file will be generated in the current directory.
Next, transfer the a.out file to the TaiShan board! Now, proceed to Chapter 3 for running the program on the TaiShan board.
2. Writing & Compiling C++ Programs
- Create a .cpp file in the current directory, where we will write the C++ program.
touch hello.cpp
- Open the hello.cpp file using the Vim editor and write the program.
Note: After entering the vim editor, you cannot start editing directly. You need to press the i key on your keyboard to enter editing mode. After writing the program, press the ESC key, then press Shift + : to make a colon appear at the bottom of the vim editor. Now, type wq and press Enter to save and exit.
sudo vi hello.cpp
Now the program is written, and when you run it, it will display: hello world!!
- Use the cross-compiler to compile hello.cpp.
sudo aarch64-linux-gnu-g++ hello.c
After compiling, an a.out file will be generated in the current directory.
Next, transfer the a.out file to the TaiShan board! Now, proceed to Chapter 3 for running the program on the TaiShan board.
3. Running the Program on LCSC Taishan-RK3566-Linux Dev Board
Copy the a.out file to a directory on the TaiShan board, then run the program.
sudo ./a.out
Now, the program is running on the LCSC Taishan-RK3566-Linux Dev Board
Important Notes
Programs compiled with an arm64 cross-compiler can only run on systems with an arm64 architecture.
Programs compiled with the cross-compiler cannot run on the virtual machine (since the VM’s system architecture is not arm64):