Wei Dongshan Embedded Introductory Notes-Application Development Fundamentals (1)

Note: The development board takes the IMX6ULL PRO development board as an example! ! !

text

One, cross compile hello.c

1. Preparation: It
is more convenient to run on the board or install and use NFS.
Unzip the tool chain, set the PATH environment variable, and determine the compiler name; then you can compile

2. The gcc compiler is only suitable for compiling programs that travel far on Ubuntu

3. The names of the compilers on different am boards are different, and the location of the tool chain is also different. The prefixes of the compilers of different versions are different (xxx-gcc, etc.). You can check it in the cross-compilation directory.

4. Refer to the network setting guide.

5. The default path of the header file of the gcc compiler is /usr/include, and the header file of the arm compiler is in the include directory under the cross-compiler directory

6. Specify the method for compiling header files:
①#include "Header file": Use double quotation marks to declare the header file, indicating that the header file is searched in the current directory
or ②Still use <> to declare the header file, and add the header file to the default search path
Or ③Still use <> to declare the header file, and add "-I [Header Record]" at the end of the compile command

7. The location of the function library:

Function library

The libraries of the gcc compiler are in /lib and /usr/lib.
The default path of the library in the cross-editor: enter the directory of Jiayoubianze, and execute find -name lib to get xxx/lib and xxx/usr/lib. Generally speaking, these two directories are the paths you are looking for.
 

Two, GCC compiler

1. The process of compiling the c file into an executable program:
.c file → preprocessing: insert the header file to be included into the original file, expand the macro definition, and select the code to be used according to the conditional compilation command
            → Compile : Advanced The language is translated into assembly language, tools: ccl, x86 has its own cc1 command, and the ARM board also has its own cc1 command.
            → Assemble: Translate the assembly code into a certain format of machine code, which is generally expressed as an ELF object file (OBJ file) on a Linux system, and the tool used is as. The x86 has its own as command, and the ARM version also has its own as command, which may also be xxxx-as (such as arm-linux-as).
            → Link: Link the OBJ file generated in the previous step with the OBJ file and library file of the system library, and finally generate an executable file that can be run on a specific platform. The tool used is ld or collect2.

2. The gcc compilation process:
(1) Preprocess and compile the .c file with the ccl command to get the .s file containing the assembly code (2) Use the as command to assemble the .s file to get the .o file, the .o file contains the machine code (3) Use the collect2 command to link multiple .o files with other library files to obtain an executable program (APP). 3. Methods to compile multiple files: ①Compile and link together: gcc -o test main.c sub .c ②Separate compilation and unified link: gcc -c -o main.o main.c gcc -c -o sub.o sub.c gcc -o test main.o sub.o Note: (1) gcc -c command :Complete the preprocessing, compile and assemble three steps at one time to get the .o file.          gcc-o command: You can link multiple .o files together to get the program, or you can complete all of them for multiple .c files at once Get the procedure in four steps (namely method ①) (2) Use method ② when the number of files is large













         Reason: When there are files to change, it will be time-consuming to recompile all the files. Only need to re-edit the changed files to get a new .o file, and then link them together.

4. Commonly used options of gcc:

Common options

description

-E

Preprocessing, you can use "-E -dM" if you want to quickly determine a certain macro during the development process

-c

The preprocessing, compilation, and assembly are all done, but not linking

-The

Specify output file

-I

Specify the header file directory

-L

Specify the library file directory when linking

-l

Specify which library file to link

Several very useful parameters:

gcc -E main.c // View the preprocessing results, such as which header file is

gcc -E -dM main.c> 1.txt // Expand all macros and store them in 1.txt (for easy viewing)

gcc -Wp,-MD,abc.dep -c -o main.o main.c // Generate dependent file abc.dep , later Makefile will use

5. Make and use the dynamic library:
(1) Find the prefix of the cross-compilation command in the cross-compiler directory, such as arch64 -linux -gnu-xxx
(2) Compile:

Prefix-gcc-c-o main.o main.c
prefix -gcc-c-o sub.o sub.c

(3) Generate dynamic library: 

Prefix -gcc -shared -o libsub.so sub.o (can be multiple .o files)

(4) Use dynamic library.:

Prefix-gcc-o test main.o -lsub-L [The directory where libsub.so is located]

Note: -shared: indicates that the output result is of the shared library type, -l parameter: specifies the name of the dynamic library, -L parameter: specifies the path of the library


(5) Run the program test:

When executing this program, you need to put the dynamic library in the default path of the library in the current system, such as /lib or /usr/lib for Ubuntu;
or change the PATH variable and add the directory where the current dynamic library is located:

 export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/a

Note: /a is the path where the library is located.

6. Making and using static libraries:
Differences with dynamic libraries:
(1) Generate dynamic libraries: ar crs libsub.a sub.o
(2) Use dynamic libraries: prefix -gcc-o test main.o libsub.a
If the .a file is not in the current directory, you need to specify its path (-L parameter)
(3) Run the program: there is no need to put the static library on the board.

7. Dynamic library and static library
(1) In layman's terms, it is to pack the object files of the functions used together to provide the interface of the corresponding functions for programmers to use.

(2) Suffix:
         Under Windows, the suffix of .dll is a dynamic library, the suffix of .lib is a static library
         under Linux, the suffix of .so is a dynamic library, and the suffix of .a is a static library.

(3) The naming format of the library: lib + "library name" +Suffix
         such as libsub.a is a static library called sub

(4) Static library: For example, the library file stdio.h is a file formed after many object files are compressed and packaged.

For static libraries, you only need to link the library file to the executable file when linking, and the library file is no longer needed when the program is running.

Disadvantages of the static library:
       ①Occupy memory and disk space: how many times the library file needs to be used, the corresponding memory will be occupied (n × file size)
       ②Update trouble: the static library changes will update the entire file.

(5) Dynamic library:

The code of the dynamic library is linked when the program is running, and the code of the library is shared by multiple programs. An executable file linked with a dynamic library only contains a table of function entry addresses used by it, rather than the entire machine code of the object file where the external function is located.

The basic realization idea is to split the program into relatively independent parts according to the modules, and link them together to form a complete program when the program is running.

For example, when running program 1, the system first loads program 1, when it finds that the Lib.o file is needed, it is also loaded into the memory, and then it loads program 2. When it is found that the Lib.o file is also needed, it does not need to be reloaded Lib.o, you only need to link program 2 and the Lib.o file, there is always only one Lib.o file in the memory.

You can use the ldd command to view the dynamic library on which the executable file depends.

View dynamic library

Advantages:
       ①It is undoubtedly to save memory;
       ②Reduce the swap in and swap out of physical pages;
       ③When upgrading a certain module, theoretically only the corresponding old target file needs to be overwritten. The new version of the target file will be automatically loaded into the memory and linked together;
       ④When the program is running, various program modules can be dynamically loaded to realize the expansion of the program.

 

Guess you like

Origin blog.csdn.net/San_a_fish_of_dream/article/details/113203059