Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Embedded C language Compiler (5)

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

We often see GCC and gcc in embedded development, so what's the difference between them? GCC (GNU Compile Collection) refers to the collection of GNU compilers, including compilers of many languages, such as C, C++, Java, D, Objective-C, etc., while gcc refers to the C language compiler in GCC. So what is the relationship between GCC and embedded? Most embedded operating systems are based on GCC for source code compilation, such as Linux, VxWorks and Android. In the actual development, kernel-related development uses gcc, while application development uses gcc/g++/gdc and so on.

Let's take a look at a high-end atmospheric term in embedded development: cross-compilation. So why is there cross-compilation? In the past, embedded devices are often resource-constrained, so it is impossible to program the processor directly on the embedded system. Then the solution at this time is to compile the source code on the development host (PC) and finally generate the executable program of the target host (embedded device). How does gcc cross-compile? 1, configure the compilation tool chain of the target host (such as arm-linux); 2, configure the specific version of the tool chain: select the corresponding version of the tool chain according to the specific object code, and correctly use the special compilation options about the hardware system. Let's take a look at what the embedded development environment of large enterprises looks like, as follows

This server cluster is equivalent to our own company's internal servers, version control refers to the original version after some of our code changes to produce a new version, for each version of the control. File tracking refers to which part of the code can be seen on the server by whom it is rewritten, and can be specific to the file and part of the code. Let's see what the compiler looks like, as follows

The compiler is actually made up of preprocessing period, compiler, assembler, and linker. When we talk about which compiler compiles the generated files, the compiler at this time refers to a wide range of compilers. Then in a narrow sense, the compiler refers to the compiler that we usually hear to produce a certain language, while the compiler at this time refers to translating the specific language into the target platform code. Let's take a look at how a .c file is compiled into a .o file, as follows

We see that it is not a direct step from a .c file to an .o executable file as we thought, but it takes so many steps to generate the final executable program. So at this point, we expand the question, how do we understand "multilingual mixed development"? We may hear about multilingual mixed development in peacetime, which refers to the development of a single application by mixing several languages. So why is there such a hybrid approach to development? For example, a project is done by C++, but some of it can be done through C #, at this time there are very few people who are proficient in C++ (the corresponding salary is very high), and there are a large number of engineers in C#. We can need two C++ engineers and several C# engineers to complete the project together to achieve the effect of completing the project with minimal cost. Or everyone in your group is good at languages in different directions, and this mixed development approach can be adopted in order to maximize everyone's efficiency. Let's take a look at several ways of multilingual mixed development.

The first way is as follows

In this way, the assembly language of the target platform is obtained by assembling several languages, and then the executable program is generated by the assembler of the target platform. A typical example of the industry is. Net framework, which is developed by a mixture of C #, C++ and VB, as follows

The second way is as follows

It is generated by their respective languages and then linked to an executable program through the target platform linker. A typical case is QQ, as follows

The third way is as follows

It is through the respective compilers into the executable program .exe, and then through the interprocess communication protocol to generate the executable program. Industry case: Eclipse, as follows

Let's take a look at the key compilation options for gcc.

Gcc key compilation option 1: a > preprocessing instruction is: gcc-E file.c-o file.i;b > compilation instruction: gcc-S file.i-o file.s;c > assembly instruction: gcc-c file.s-o file.o.

Let's see what the effect is.

Func.h source code

# include void func () {# ifdef TEST printf ("TEST =% s\ n", TEST); # endif return;}

Test.c source code

# include # include "func.h" int g_global = 0 argv main (int argc, char * int []) {func (); printf ("& g_global =% p\ n", & g_global); printf ("& g_test =% p\ n", & g_test); printf ("& func =% p\ n", & func); printf ("& main =% p\ n", & main) Return 0;}

Let's take a look at the effect of preprocessing. Open the test.i file. It starts like this.

The 1 on the first line indicates that the following belongs to the test.c file, and the following is the inclusion of some header files.

# 2 "test.c" 2 means that the inclusion of the test.c header file is over, and # 1 "func.h" 1 represents the beginning of func.h-related content. Finally, there is the content of the main function in the test.c file. Let's take a look at the .s file generated by the compilation instruction

All are generated assembly commands. Let's take a look at the final assembly instruction to generate the .o file.

Gcc key compilation option 2: a > generate mapping file: gcc-WI,-Map=test.map file.c;b > macro definition: gcc-test test = "test" 'file.c;c > get the system header file path: gcc-v file.c.

Gcc key compilation option 3: generate dependencies. A > get the complete dependency of the target: gcc-M test.c;b > get the partial dependency of the target: gcc-MM test.c.

Let's take a look at the effects of-M and-MM, respectively, as follows

We see that there are so many header files that the format is similar to the target-dependency relationship in makefile. The dependencies are test.c and numerous header files, as well as our own included func.h header files. Let's see again-the effect of MM

We see that the effect of-MM is dependent on test.c and func.h, and there are no other header files.

Gcc key compilation option 4: specify the library file and the library file search path. The-L option specifies the search path for library files;-l specifies the library files, such as gcc test.c-L-lfunc.

Func.c source code

# include void func () {# ifdef TEST printf ("TEST =% s\ n", TEST); # endif return;}

Test.c source code

# include int g_global = 0tint g_test = 1boarint main (int argc, char * argv []) {func (); printf ("& g_global =% p\ n", & g_global); printf ("& g_test =% p\ n", & g_test); printf ("& func =% p\ n", & func); printf ("& main =% p\ n", & main); return 0;}

The compilation results are as follows

We see that after the func.o is packaged into a libfunc.a file by the ar crs command, and then through gcc test.c-L. The-lfunc command generates the executable program a.out (where the dots after-L represent the current directory).

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report