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

How to use the Linux GCC command

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the Linux GCC command". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the Linux GCC command.

1. Brief introduction

GCC means only GNU C Compiler. After so many years of development, GCC has not only supported C language; it now supports Ada language, C++ language, Java language, Objective C language, Pascal language, COBOL language, and Mercury language which supports functional programming and logical programming, and so on. And GCC is no longer just the meaning of GNU C language compiler, but has become the meaning of GNU Compiler Collection, that is, the family of GNU compilers. On the other hand, when it comes to GCC's support for operating system platforms and hardware platforms, it can be summed up as a sentence: omnipresent.

two。 Simple compilation

The example program is as follows:

/ / test.c#includeint main (void) {printf ("Hello World!\ n"); return 0;}

The compilation instructions for this program in one step are:

Gcc test.c-o test

In essence, the above compilation process is divided into four stages, namely preprocessing (also known as precompilation, Preprocessing), compilation (Compilation), assembly (Assembly), and Linking (Linking).

2.1 Pretreatment

Gcc-E test.c-o test.i or gcc-E test.c

You can output the preprocessed test.c code in the test.i file. Open the test.i file, take a look, and you'll see. The latter instruction outputs the preprocessed code directly in the command line window. The-E option of gcc allows the compiler to stop after preprocessing and output the preprocessing result. In this case, the result of the preprocessing is to insert the contents of the stdio.h file into the test.c.

2.2 compiled into assembly code (Compilation)

After preprocessing, the generated test.i file can be compiled directly to generate assembly code:

Gcc-S test.i-o test.s

The-S option of gcc indicates that during the compilation of the program, after generating the assembly code, stop,-o output the assembly code file.

2.3 compilation (Assembly)

The assembly code file test.s,gas assembler generated in the previous section is responsible for compiling it into an object file, as follows:

Gcc-c test.s-o test.o

2.4 connection (Linking)

The gcc connector is provided by gas and is responsible for connecting the program's object file with all the additional object files needed, resulting in an executable file. Additional object files include static link libraries and dynamic link libraries. For the test.o generated in the previous section, connect it with the C standard I / O library to generate the program test

Gcc test.o-o test

In the command line window, execute. / test and let it say HelloWorld!

3. Compilation of multiple program files

Usually the whole program is composed of multiple source files, which correspondingly forms multiple compilation units, which can be well managed by using GCC. Suppose you have a program that consists of two source files, test1.c and test2.c. In order to compile them and eventually generate the executable program test, you can use the following command:

Gcc test1.c test2.c-o test

If more than one file is processed at the same time, GCC still follows the process of preprocessing, compilation, and linking. If you dig deeper, the above command is roughly equivalent to executing the following three commands in turn:

Gcc-c test1.c-o test1.ogcc-c test2.c-o test2.ogcc test1.o test2.o-o test4. Error detection gcc-pedantic illcode.c-o illcode

The-pedantic compilation option does not guarantee that the compiled program is fully compatible with the ANSI/ISO C standard, it can only be used to help Linux programmers get closer and closer to this goal. Or in other words, the-pedantic option can help programmers find some code that does not conform to the ANSI/ISO C standard, but not all of it. In fact, only those situations in the ANSI/ISO C language standard that require compiler diagnostics are likely to be discovered and warned by GCC.

In addition to-pedantic, GCC has a number of other compilation options that can also generate useful warnings. Most of these options start with-W, the most valuable of which is-Wall, which enables GCC to generate as many warnings as possible.

Gcc-Wall illcode.c-o illcode

While the warning message given by GCC is not strictly an error, it is likely to be the wrong place to live. A good Linux programmer should try to avoid generating warning messages and keep his code standard and robust. So it is commendable to treat warning messages as coding errors! So, with the-Werror option when compiling the program, GCC stops compiling wherever warnings are generated, forcing programmers to make changes to their code, as follows:

Gcc-Werror test.c-o test5. Library file connection

When developing software, it is rare not to use third-party function libraries at all. generally speaking, we need the support of many function libraries to complete the corresponding functions. From the programmer's point of view, a function library is actually a collection of header files (.h) and library files (so, or lib, dll). \. Although most functions under Linux put the header files in the / usr/include/ directory by default, and the library files in the / usr/lib/ directory; the library files used by Windows are mainly placed in the include and lib under the Visual Stido directory, as well as under the system folder. But there are times when the libraries we want to use are no longer in these directories, so GCC must find the header files and library files it needs when compiling.

For example, our program test.c uses c to connect to mysql on linux. At this time, we need to go to the official website of mysql to download MySQL Connectors's C library. After downloading and decompressing it, there is an include folder containing the header files of mysql connectors and a lib folder containing binary so files libmysqlclient.so where the path of the inclulde folder is / usr/dev/mysql/include and the lib folder is / usr/dev/mysql/lib.

5.1 compiled to an executable file

First of all, we need to compile test.c as the object file, which needs to be executed

Gcc-c-I / usr/dev/mysql/include test.c-o test.o

5.2 Link

Finally, we link all the target files into executable files:

Gcc-L / usr/dev/mysql/lib-lmysqlclient test.o-o test

The library files under Linux are divided into two categories: dynamic link libraries (usually ending in .so) and static link libraries (usually ending in .a). The only difference between them lies in whether the code needed for program execution is dynamically loaded at run time or statically loaded at compile time.

5.3 use static link libraries when forcing links

By default, GCC gives priority to dynamic link libraries when linking, and static link libraries are considered only when they do not exist. If necessary, you can add the-static option at compile time to force the use of static link libraries. The library files libmysqlclient.so and libmysqlclient.an are needed when there are links in the / usr/dev/mysql/lib directory. To make GCC use only static link libraries when linking, you can use the following command:

Gcc-L / usr/dev/mysql/lib-static-lmysqlclient test.o-o test

Search path order when static library links:

\ 1. Ld will look for the parameter-L in the GCC command

\ 2. Then find the environment variable LIBRARY_PATH of gcc

\ 3. Then find the internal directory / lib / usr/lib / usr/local/lib, which was written in the program at the time of compile gcc.

Search path order when dynamic linking and execution:

\ 1. The dynamic library search path specified when compiling the object code

\ 2. The dynamic library search path specified by the environment variable LD_LIBRARY_PATH

\ 3. Dynamic library search path specified in configuration file / etc/ld.so.conf

\ 4. The default dynamic library search path / lib\ 5. Default dynamic library search path / usr/lib

About environment variables:

LIBRARY_PATH environment variable: specify the search path for program static link library files

LD_LIBRARY_PATH environment variable: specify the search path for program dynamic link library files

At this point, I believe you have a deeper understanding of "how to use the Linux GCC command". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report