Embedded Linux (study one on the second day)-write Linux applications and understand its principles

GCC compiler under Linux

GCC (GNU Compiler Collection, GNU Compiler Collection) is a programming language translator developed by GNU. The GNU compiler suite includes C, C++, Objective-C, Fortran, Java, Ada, and Go language front ends, as well as libraries for these languages ​​(such as libstdc++, libgcj, etc.) [1]
The original intention of GCC is to be dedicated to the GNU operating system A compiler written. The GNU system is completely free software. Here, the meaning of "freedom" is that it respects the freedom of users-Baidu Baike

The usage is just like a general application under Linux, directly gcc <parameter> <file> after installation

See the common parameters

https://www.runoob.com/w3cnote/gcc-parameter-detail.html

gcc and g++ are respectively gnu's c & c++ compiler gcc/g++. When performing compilation work, a total of 4 steps are required:

1. Preprocess and generate .i files [preprocessor cpp]
2. Convert the preprocessed files into assembly language and generate files .s [Compiler egcs]
3. There is assembly into object code (machine code) Generate .o files [Assembler as]
4. Link object code to generate executable program [Linker ld]

The first application HelloWorld

A .c
vimHello
Insert picture description here
compilation of Vim
Insert picture description here
will produce a.out
Insert picture description here
Hello World!
Insert picture description here
I tried it with gcc -o, two files are needed, the first is the output, the second is the source file
Insert picture description here
Insert picture description here
Insert picture description here

Program execution

Insert picture description here
exec: refer to (https://blog.csdn.net/qq_31186123/article/details/82190776)
brk: create program data segment
mmap: open shared library file and read into memory
write: output, it is a function in shared library

Specific operations can be used strace to see the specific process
Insert picture description here
output

execve("./a.out", ["./a.out"], [/* 62 vars */]) = 0
brk(NULL)                               = 0x148d000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {
    
    st_mode=S_IFREG|0644, st_size=108826, ...}) = 0
mmap(NULL, 108826, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f59a3c0e000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\t\2\0\0\0\0\0"..., 832) = 832
fstat(3, {
    
    st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f59a3c0d000
mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f59a363a000
mprotect(0x7f59a37fa000, 2097152, PROT_NONE) = 0
mmap(0x7f59a39fa000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f59a39fa000
mmap(0x7f59a3a00000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f59a3a00000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f59a3c0c000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f59a3c0b000
arch_prctl(ARCH_SET_FS, 0x7f59a3c0c700) = 0
mprotect(0x7f59a39fa000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ)     = 0
mprotect(0x7f59a3c29000, 4096, PROT_READ) = 0
munmap(0x7f59a3c0e000, 108826)          = 0
fstat(1, {
    
    st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
brk(NULL)                               = 0x148d000
brk(0x14ae000)                          = 0x14ae000
write(1, "Hello World!\n", 13Hello World!
)          = 13
exit_group(0)                           = ?
+++ exited with 0 +++

Take a look, it's similar to the flowchart.

The whole process of compilation

Before our gcc helloworld will produce an a.out which actually involves two processes, compilation and linking. After learning the principles of computer composition, I probably know that compilation is divided into five processes: lexical analysis, syntax analysis, intermediate code generation, code optimization, and target code generation.

gcc -c helloworld.c

Generating .o file
-c
only activates preprocessing, compilation, and assembly, that is, it only makes the program into an obj file
. The o file cannot be run directly, and it needs to be generated through connection to be recognized and run by the operating system.

. . . After the connection code is too much, I will directly take a screenshot from the book. . .
Insert picture description here
Oh my god...I don't believe it...is it really that troublesome...you have to type so many code connections...fortunately, there is a makefile once and for all. Those who have used Git should always use make to build other people's programs.

MakeFile management project

Applying Haoel's article... I just took a brief look at (1) When I have time, I will continue to read, first learn embedded Linux.

https://blog.csdn.net/haoel/article/details/2886

Served... The Makefile in the emmmm book says this

hello : helloworld.c
        gcc -o hello helloworld.c
clean :
        rm -fr hello *.o *.core

After make, there will be an extra hello,
Insert picture description here
make clean will delete hello

Guess you like

Origin blog.csdn.net/u010594449/article/details/105597310