gcc compiler directives under Linux

Source: Internet
Author: User

1 Introduction

The meaning of GCC is only GNU C Compiler. After so many years of development, GCC has not only supported the C language; it now also supports the ADA, C + +, Java, Objective C, Pascal, COBOL, and Mercury languages that support functional programming and logical programming, and so on. And GCC is no longer just the GNU C language compiler meaning, but into the GNU Compiler Collection is also the GNU compiler family meaning. On the other hand, when it comes to GCC's support for operating system platforms and hardware platforms, it is a word: ubiquitous.

2 Simple compilation

The sample program is as follows:

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

For this program, the one Step compiler command is:

GCC Test.c-o Test



In essence, the compilation process described above is performed in four phases, namely preprocessing (also known as precompilation, preprocessing), compilation (compilation), assembly (Assembly), and connection (linking).

2.1 Preprocessing
GCC-E test.c-o test.i or GCC-E test.c

You can output the code in the Test.i file that contains the test.c after preprocessing. Open the Test.i file and take a look, and you'll see. The subsequent instruction is to output 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 results. In this case, the preprocessing result is to insert the contents of the stdio.h file into the test.c.

2.2 Compiling to Assembly code (compilation)

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

Gcc-s Test.i-o Test.s

The-s option of GCC, which indicates that the assembly code is stopped and the-O output assembly code file is generated during program compilation.

2.3 Assembly (Assembly)

For the assembly code file generated in the previous section, the Test.s,gas assembler is responsible for compiling it as a 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 additional target files needed to eventually generate the executable file. Additional target files include a static connection library and a dynamic connection library.

For the TEST.O generated in the previous section, connect it to the C standard input and output library, and the resulting program test

GCC Test.o-o Test

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

Compilation of more than 3 program files

Typically, the entire program is composed of multiple source files, which in turn form a number of compilation units, which can be well managed using GCC. Suppose you have a program that consists of two source files of test1.c and test2.c, in order to compile them and eventually generate executable test, you can use this command:

GCC test1.c test2.c-o Test

If more than one file is processed at the same time, GCC will still follow the process of preprocessing, compiling, and linking. If you delve into it, the above command is roughly equivalent to executing the following three commands in turn:

Gcc-c Test1.c-o TEST1.O
Gcc-c Test2.c-o TEST2.O
GCC test1.o test2.o-o Test



4 Error detection
Gcc-pedantic Illcode.c-o Illcode

The-pedantic compilation option does not guarantee the full compatibility of the compiled program with the Ansi/iso C standard, it can only be used to help Linux programmers get closer to this goal. Or in other words, the-pedantic option can help programmers discover some code that does not conform to the Ansi/iso C standard, but not all, in fact only those cases in which the compiler is required to diagnose in the Ansi/iso C language standard can be discovered and warned by GCC.

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

Gcc-wall Illcode.c-o Illcode

The warning message given by GCC, though strictly not a mistake, is likely to be the wrong shelter. A good Linux programmer should try to avoid generating warning messages so that their code remains standard and robust. So it is a commendable act to treat the warning message as a coding error! So, when compiling the program with the-WERROR option, GCC stops compiling at all warnings, forcing the programmer to modify its code as follows:

Gcc-werror Test.c-o Test

5 Library file connections

When developing software, it is relatively uncommon to not use a third-party library at all, usually with the support of many libraries to complete the function. From the programmer's point of view, the library is actually a collection of header files (. h) and library files (so, or LIB, DLLs). Although most of the functions under Linux are placed in the/usr/include/directory by default, and the library files are placed in the/usr/lib/directory, the library files used by Windows are mainly placed in the include and Lib under the directory of Visual Stido. and the System folder. But there are times when we need to use a library that is no longer in these directories, so GCC must use its own method to find the required header and library files at compile time.

For example, our program test.c is to use C to connect MySQL on Linux, this time we need to go to MySQL official website download MySQL connectors c library, download and unzip, there is an include folder, which contains MySQL Connectors's header file, and a Lib folder containing binary so files libmysqlclient.so

Where the path to the Inclulde folder is the/usr/dev/mysql/include,lib folder is/usr/dev/mysql/lib

5.1 Compiling into an executable file

First we want to compile test.c as the target file, this time need to execute

Gcc–c–i/usr/dev/mysql/include Test.c–o TEST.O
5.2 Links

Finally, we link all the target files to the executable file:

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 with. So) and static-link libraries (usually ending with. A), except that the code required to execute the program is dynamically loaded at run time, or statically at compile time.

5.3 Using static link libraries when linking is mandatory

By default, GCC takes precedence over dynamic-link libraries when linking, and only considers static-link libraries when the dynamic-link library does not exist, and, if needed, with the-static option at compile time, forcing static-link libraries to be used.

In order for GCC to use only static link libraries when linking, you can use the following command when you need the library files libmysqlclient.so and libmysqlclient.a in the/usr/dev/mysql/lib directory:

Gcc–l/usr/dev/mysql/lib–static–lmysqlclient test.o–o Test

Search path order when static library links:

1. LD will go to the parameters in the GCC command-l
2. Re-search for GCC environment variables Library_path
3. Find the default directory/lib/usr/lib/usr/local/lib This is the original compile GCC when written in the program

Dynamic Link-time, execution-time search path order:

1. The dynamic library search path specified when compiling the target code
2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path
3. The dynamic library search path specified in configuration file/etc/ld.so.conf
4. Default dynamic library search path/lib
5. Default dynamic Library search path/usr/lib

About environment variables:
Library_path environment variable: Specifies the program static link library file search path
LD_LIBRARY_PATH environment variable: Specifies the program dynamic link library file search path

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.