In the beginning, since I have already learned the bare metal program of JZ2440V3 development board, I want to test the learning results, so from today on, I will test the knowledge points I learned before on the tiny4412 development board. The bare metal part of learning ends when I complete the uboot porting; then, I will learn the kernel driver and other subsystem frameworks. Now let's get back to the topic and start learning how to use the cross-compilation tool chain.
Source files need to be compiled to generate executable files. When developing under Windows, you only need to click a few buttons to compile. The integrated development environment (such as Visual studio) has encapsulated the use of various compilation tools.
There are also excellent integrated development tools under Linux, but more often the compilation tools are run directly in command mode; even when using integrated development tools, you still need to master some compilation options.
The compilation tool chain on the PC is gcc, ld, objcopy, objdump, etc. The programs compiled by them run on the x86 platform. To compile a program that can run on the ARM platform, you must use cross-compilation tools such as arm-linux-gcc and arm-linux-ld, which are introduced below.
1. Introduction to arm-linux-gcc tools
A C/C++ file must go through four steps, namely preprocessing, compilation, assembly, and linking, before it can become an executable file, as shown in Table 1.1. In daily communication, "compilation" is usually used to refer to these four steps. If it is not specifically referring to one of the four steps, this book also uses the general term "compilation" according to convention.
1.1. Preprocessing
In C/C++ source files, commands starting with "#" are called preprocessing commands, such as the include command "#include", the macro definition command "#define", the conditional compilation commands "#if", "#ifdef", etc. Preprocessing is to insert the files to be included into the original file, expand the macro definition, select the code to be used according to the conditional compilation command, and finally output these things to a ".i" file for further processing. The arm-linux-cpp tool will be used for preprocessing.
1.2. Compilation
Compilation is to "translate" C/C++ code (such as the ".i" file mentioned above) into assembly code. The tool used is cc1 (its name is cc1, not arm-linux-cc1).
1.3. Assembly
Assembly is to translate the assembly code output in the second step into machine code that conforms to a certain format. On Linux systems, it is generally presented as an ELF target file (OBJ file), and the tool used is arm-linux-as. "Disassembly" refers to converting machine code into assembly code, which is often used when debugging programs.
1.4. Connection
Linking is to connect 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 run on a specific platform. The tool used is arm-linux-ld.
The compiler uses one or more of these four steps to process the input file. The suffix of the source file indicates the language used by the source file. The suffix controls the default action of the compiler, as shown in Table 1.1.
Files with other suffixes are passed to the linker, usually including:
o: Object file (OBJ file)
a: Archive file
During the compilation process, unless the "-c", "-S" or "-E" option is used (or a compilation error prevents the complete process), the last step is always linking. During the linking stage, all .o files corresponding to the source program, library files specified by the "-l" option, and unrecognized file names (including specified ".o" object files and ".a" library files) are passed to the linker in the order given on the command line.
Take a simple "Hello, world!" C program as an example. Its code is as follows. Its function is to print the "Hello World!" string.
/* File: hello.c */
#include int main(int argc, char *argv[]) { printf("Hello World!n"); return 0; } Using arm-linux-gcc, only one command is needed to generate the executable file hello, which includes the above 4 steps: arm-linux-gcc -o hello hello.c Add the "-v" option, that is, use the "arm-linux-gcc -v -o hello hello.c" command to view the compilation details. The following is an extract of the key parts: cc1 hello.c -o /tmp/cctETob7.s as -o /tmp/ccvv2KbL.o /tmp/cctETob7.s collect2 -o hello crt1.o crti.o crtbegin.o /tmp/ccvv2KbL.o crtend.o crtn.o The above three commands correspond to the preprocessing, compiling, assembling and linking in the compilation steps. ld is called by collect2 to link the program. Preprocessing and compiling are put into one command (cc1), which can be further divided into the following two steps: cpp -o hello.i hello.c cc1 hello.i -o /tmp/cctETob7.s The actions of arm-linux-gcc can be controlled by various options. Here are some commonly used options: Overall options: -c option: Preprocess, compile and assemble source files, but do not link them. The compiler generates OBJ files based on the source files. By default, GCC generates OBJ file names by replacing the suffixes of the source file names with ".c", ".i", ".s", etc. with ".o". You can use the -o option to select other names. GCC ignores any unrecognized input files after the -c option. -S option: Stop after compiling without assembling. For each non-assembly language file input, the output result is an assembly language file. By default, GCC generates assembly file names by replacing the source file name suffix ".c", ".i", etc. with ".s". You can use the -o option to select other names. GCC ignores any input files that do not need to be assembled. -E option: Stop after preprocessing and do not compile. The preprocessed code is sent to standard output. GCC ignores any input files that do not need preprocessing. -o option: specifies the output file as file. This option can be used regardless of preprocessing, compiling, assembling or linking. If the '-o' option is not used, the default output result is: the executable file is 'a.out'; if the name of the input file is modified to 'source.suffix', its OBJ file is 'source.o', the assembly file is 'source.s', and the preprocessed C source code is sent to the standard output. -v option: Displays the configuration commands used to build the GCC tool itself; also displays the version numbers of the compiler driver, preprocessor, and compiler. Take a program as an example, it contains three files, the source code is listed below: //File: main.c #include #include "sub.h" int main(int argc, char *argv[]) { int i; printf("Main fun!n"); sub_fun(); return 0; } //File: sub.h void sub_fun(void); //File: sub.c void sub_fun(void) { printf("Sub fun!n"); } The arm-linux-gcc, arm-linux-ld and other tools are similar to the gcc, ld and other tools in terms of usage, and many options are the same. This section uses gcc, ld and other tools for compilation and linking, so that the running results can be directly seen on the PC. Use the options introduced above to compile, and the command is as follows: $ gcc -c -o main.o main.c $ gcc -c -o sub.o sub.c $ gcc -o test main.o sub.o Among them, main.o and sub.o are OBJ files generated after preprocessing, compilation, and assembly. They have not yet been connected into executable files. The last step is to connect them into an executable file test. You can directly run the following command: $ ./test Main fun! Sub fun! Now let's try other options. The following command generates main.s which is the assembly language file of main.c: gcc -S -o main.s main.c The following command preprocesses main.c and prints the result. It expands all included files and all defined macros. When writing a program, it is sometimes very tedious to find a macro definition. You can use the '-dM –E' option to view it. The command is as follows: $ gcc -E main.c Warning options: -Wall option: This option basically turns on all warning messages that need attention, such as declarations without specified types, functions used before declaration, local variables that are not used again except for declaration, etc. In the main.c file above, the variable i defined in line 6 is not used, but no prompt appears when compiling with "gcc –c –o main.o main.c". You can add the -Wall option, as follows: $ gcc -Wall -c main.c After executing the above command, the following warning message is obtained: main.c: In function `main': main.c:6: warning: unused variable `i Although this warning has no bad impact on the program, some warnings need attention, such as type matching warnings. Debug options: -g option: Generate debugging information in the native format of the operating system (stabs, COFF, XCOFF, or DWARF), which GDB can use. On most systems that use the stabs format, the '-g' option adds additional debugging information that only GDB uses. The following options can be used to generate additional information: '-gstabs+', '-gstabs', '-gxcoff+', '-gxcoff', '-gdwarf+', or '-gdwarf'. Please refer to the GCC manual for specific usage. Optimization options: -O or -O1 option: Optimization: For large functions, the optimization compilation process will take slightly more time and a considerable amount of memory. The purpose of not using the "-O" or "-O1" option is to reduce the compilation overhead, make the compilation result debuggable, and make the statements independent; if you use a breakpoint to stop the program between two statements, you can reassign any variable, or point the program counter to other statements in the function body, and get the exact results you expect from the source program. -O2 option: Optimize more. Perform almost all optimizations except those that involve trading off space for speed. For example, no loop unrolling or function inlining. Compared to '-O' or '-O1' options, this option increases compilation time and also improves the performance of the generated code. -O3 option: Optimizes even more. In addition to turning on everything -O2 does, it also turns on the -finline-functions option.
Previous article:Exynos4412 chip clock management unit
Next article:Linux system learning 1-8: The first ARM bare board program and extension
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- I3C standard information, sharing
- Harmonic suppression and utilization
- Bluetooth Low Energy GAP, GATT
- Summary of Common/Uncommon IOT Protocols
- [TI recommended course] # Strengthen power supply knowledge through the TI Power Management Lab Kit (TI-PMLK) series#
- Share: Which domestic MCU is the best?
- Pingtouge Wi-Fi AT Command User Manual
- A Single Chip Microcomputer Application Design of Infrared Induction Liquid Pump
- Note: Questioning skills
- What does this mean? #define MQTTString_initializer {NULL, {0, NULL}}