Chapter 3 Compile the first APP: hello
1.1 Introduction to Clang, LLVM and GCC
When we compile the program for the PC in Ubuntu, execute this command:
gcc -o hello hello.c
When we compile the program for the ARM board in Ubuntu, execute this command (the gcc prefix may be different):
arm-linux-gcc -o hello hello.c
In Liteos-a, LLVM is used to compile the program. The original meaning of LLVM is "Low Level Virtual Machine", a low-level virtual machine. But it has now developed into a framework system for a compiler (compiler). Simply put, LLVM can replace GCC, LLVM is easy to expand and can provide better performance.
The framework of LLVM is as follows:
Clang is "Frontend" (front end), used to perform lexical analysis, grammatical analysis, simply put: check syntax errors, include header files, expand macros.
In the middle is the optimizer, which converts the code analyzed by the front-end into LLVM IR (intermediate representation). IR has nothing to do with the machine, and a series of optimizations can be easily added.
The IR obtained after optimization is converted into the machine code of various machines by "Backend".
The above content comes from the Internet. I don't have a deep understanding of Clang and LLVM, so I just give a brief introduction.
1.2 Compile the hello program
When using Clang, we have to specify a lot of parameters: for which type of CPU to compile the program (ARM or Risc-V), what is the CPU architecture (cortex A7 or cortex A15), where are the header files/library files?
When compiling the program for Liteos-a, taking the hello program as an example, you can execute the following command:
cd /home/book/openharmony_for_imx6ull/apps/hello
clang -target arm-liteos --sysroot=/home/book/openharmony/prebuilts/lite/sysroot/ \
-o hello hello.c
In the sysroot parameter of the above command, the directories of standard header files and library files are specified.
You can also use the "-I" and "-L" parameters like GCC to specify the directories of other header files and library files. For example, this program will be compiled later:
cd /home/book/openharmony_for_imx6ull/apps/freetype
clang -target arm-liteos --sysroot=/home/book/openharmony/prebuilts/lite/sysroot/ \
-I /home/book/openharmony/third_party/freetype/include \
-L /home/book/openharmony/out/ipcamera_hi3518ev300/libs/usr \
-lfreetype \
-o show_line show_line.c
1.3 Put the hello program into rootfs
After compiling the hello program, you can put it into the bin letter in the rootfs directory, for example:
cd /home/book/openharmony_for_imx6ull/apps/hello
cp hello /home/book/openharmony/kernel/liteos_a/out/imx6ull/rootfs/bin
Then re-create rootfs.jffs2, execute the following command:
> cd /home/book/openharmony/kernel/liteos_a/out/imx6ull/ mkfs.jffs2 -s
> 0x10000 -e 0x10000 -d rootfs -o rootfs.jffs2
Assuming that the hello program is located in the /home/book/apps/hello directory, the operation screenshot is as follows:
Put the obtained rootfs.jffs2 in the files directory of the burning tool, and you can start it with the burning tool: After
entering the command line, you can execute the hello program, as shown in the following figure:
Note: You must use "./bin/ hello", the absolute path "/bin/hello" cannot be used
Full text download
Technical Exchange Group (
Hongmeng Development/Linux/Embedded/Driver/Data Download) QQ Group: 869222007