Instructions for using the cross-compilation tool

Publisher:cwk2003Latest update time:2020-03-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

image.png

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. 

[1] [2] [3]
Reference address:Instructions for using the cross-compilation tool

Previous article:Exynos4412 chip clock management unit
Next article:Linux system learning 1-8: The first ARM bare board program and extension

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号