Common Linux GCC commands

Source: Internet
Author: User
Common Linux GCC commands

1 Overview

2. Simple Compilation

2.1 preprocessing

2.2 compile as compilation)

2.3 assembly)

2.4 connection (linking)

3. Compilation of multiple Program Files

4. Check for errors

5. database file connection

5.1 compile into an executable file

5.2 Link

5.3 use static Link Library for force Link

1 Overview

GCC only means gnu c compiler. After so many years of development, GCC not only supports the C language, but also supports the Ada Language, C ++ language, Java language, Objective C language, PASCAL Language, and COBOL language, and Mercury languages that support functional and logical programming. GCC doesn't just mean the gnu c compiler, But it turns into the GNU Compiler Collection, which is also the meaning of the GNU Compiler family. On the other hand, speaking of GCC's support for operating system platforms and hardware platforms, it is simply a word: ubiquitous.

2. Simple Compilation

The example program is as follows:

//test.c#include <stdio.h>int main(void){    printf("Hello World!\n");    return 0;}

The one-step compilation command for this program is:

gcc test.c -o test

In essence, the above compilation process is divided into four stages: preprocessing (also called preprocessing), compilation, assembly, and linking ).

2.1 preprocessing
Gcc-E test. C-O test. I or GCC-E test. c

 

You can output the pre-processed code of test. c stored in the test. I file. Open the test. I file and take a look. The subsequent command directly outputs the pre-processed code in the command line window.

The-E Option of GCC allows the compiler to stop preprocessing and output preprocessing results. In this example, the pre-processing result is to insert the content in the stdio. h file to test. C.

2.2 compile as compilation)

After preprocessing, you can directly compile the generated test. I file to generate the assembly code:

gcc -S test.i -o test.s

The-s option of GCC indicates that, during program compilation, after the assembly code is generated, the-O outputs the assembly code file.

2.3 assembly)

For the assembly code file test. s generated in the previous section, the gas assembler is responsible for compiling it into the target 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 target file with all the required additional target files to generate executable files. The attached target files include static and dynamic connection libraries.

For test. O generated in the previous section, connect it with the c Standard Input and Output library, and finally 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

Generally, the entire program is composed of multiple source files, and multiple compilation units are formed accordingly. using gcc, these compilation units can be well managed. Suppose there is a program composed of two source files: test1.c and test2.c. To compile the program and generate the executable program test, run the following command:

GCC test1.c test2.c-o Test

If more than one file is processed at the same time, GCC will continue to follow the preprocessing, compilation, and link processes. If we look into it, the above command is roughly equivalent to executing the following three commands in sequence:

gcc -c test1.c -o test1.ogcc -c test2.c -o test2.ogcc test1.o test2.o -o test

4. Check for errors

gcc -pedantic illcode.c -o illcode

-The pedantic compilation option does not ensure that the program to be compiled 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. In other words, the-pedantic option can help programmers find some codes that do not conform to the ANSI/iso c standard, but not all of them, in fact, only those situations that require compiler diagnosis in the ANSI/iso c language standards can be detected and warned by GCC.

In addition to-pedantic, GCC also has some other compilation options that can generate useful warning information. Most of these options start with-W, the most valuable of which is "number-wall". Using this option enables GCC to generate as many warning messages as possible.

gcc -Wall illcode.c -o illcode

Although the warning information given by GCC cannot be regarded as an error in a strict sense, it is likely to become a place where errors occur. A good Linux programmer should try to avoid generating warning information so that his code always maintains standard and robust features. Therefore, treating the warning information as a code error is a commendable behavior! Therefore, when the-werror option is added during program compilation, GCC stops compilation in all the places where warnings are generated, forcing programmers to modify their own Code as follows:

gcc -Werror test.c -o test

 

5. database file connection

When developing software, it is rare to use no third-party function libraries at all. Generally, it is necessary to use the support of many function libraries to complete the corresponding functions. From the programmer's perspective, the function library is actually a collection of header files (. h) and library files (So, or Lib, DLL .. Although most functions in Linux place the header file in the/usr/include/directory by default, and the library file in the/usr/lib/directory; the library files used by windows are mainly placed under the include and lib directory of visual stido, and under the system folder. However, sometimes, the library we use is no longer under these directories, so GCC must use its own method to find the required header files and library files during compilation.

For example, our program test. C is to use C to connect to MySQL on Linux. At this time, we need to download the C library of MySQL connectors from the MySQL official website. After downloading and decompressing it, there is an include folder, it contains the header file of MySQL connectors and a lib folder, which contains the binary so file libmysqlclient. so

The Directory of the inclulde folder is/usr/dev/MySQL/include, and the Lib folder is/usr/dev/MySQL/lib.

 

5.1 compile into an executable file

First, we need to compile test. C as the target file. At this time, we need to execute

gcc –c –I /usr/dev/mysql/include test.c –o test.o
5.2 Link

Finally, we link all target files to executable files:

gcc –L /usr/dev/mysql/lib –lmysqlclient test.o –o test

Library files in Linux are divided into two categories. so) and static link library (usually. end A), the difference is only that the Code required for program execution is dynamically loaded at runtime, or static load at compilation.

5.3 use static Link Library for force Link

By default, GCC preferentially uses the dynamic link library when linking. Static Link Library is considered only when the dynamic link library does not exist. If necessary, you can add the-static option during compilation, use a static Link Library.

Libmysqlclient is the library file that is required to have a link under the/usr/dev/MySQL/lib directory. so and libmysqlclient. a. To enable GCC to only use the static link library when linking, run the following command:

gcc –L /usr/dev/mysql/lib –static –lmysqlclient test.o –o test

 

Search Path sequence for static library links:

1. LD will go to the parameter-l in the GCC command
2. Find the GCC environment variable LIBRARY_PATH.
3. Find the internal directory/lib/usr/local/lib, which was written in the program at the time of compile GCC.

Search Path sequence during dynamic link and execution:

1. The dynamic library search path specified when the target code is compiled
2. The dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH
3. The dynamic library search path specified in the configuration file/etc/lD. So. conf
4. Default dynamic library search path/lib
5. Default dynamic library search path/usr/lib

Environment variables:
LIBRARY_PATH environment variable: Specifies the path for searching files in the static Link Library of the program.
LD_LIBRARY_PATH environment variable: Specifies the path for searching files in the dynamic link library of a program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.