one brief introduction
GCC It just means GNU C Compiler nothing more. After so many years of development, GCC It can not only support C Language; It also now supports Ada Language C++ Language Java Language Objective C Language Pascal Language COBOL Language, and support for functional programming and logical programming Mercury Language, etc. and GCC It's no longer just GNU C Language compiler means GNU Compiler Collection That is GNU The meaning of compiler family. On the other hand, when it comes to GCC The operating system platform and hardware platform support can be summarized as one sentence: ubiquitous.
two Simple compilation
An example program is as follows:
//test. c
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
The one-step compilation instructions for this program are :
gcc test.c -o test
In essence, the above compilation process is divided into four stages, namely preprocessing ( Also known as precompiling, Preprocessing) , Compile (Compilation) , Assembly (Assembly) And connections (Linking) 。
two point one Pretreatment
gcc -E test.c -o test.i or gcc -E test.c
Can output test.i In the file test.c Pre processed code. open test.i Take a look at the document and you will understand. The latter instruction is to directly output the preprocessed code in the command line window .
gcc Of -E Option allows the compiler to stop after preprocessing and output the preprocessing results. In this example, the preprocessing result is stdio.h The contents of the file are inserted into test.c Yes.
two point two Compile as assembly code (Compilation)
After pretreatment, the generated test.i File compilation to generate assembly code:
gcc -S test.i -o test.s
gcc Of -S Option to stop after generating assembly code during program compilation, -o Output assembly code file.
two point three assembly (Assembly)
For the assembly code file generated in the previous section test.s , gas The assembler is responsible for compiling them into target files, as follows:
gcc -c test.s -o test.o
two point four connect (Linking)
gcc Connector is gas It is responsible for connecting the object file of the program with all the required additional object files, and finally generating the executable file. Additional object files include static link libraries and dynamic link libraries.
For the generated test.o , connect it with the C standard I/O library, and finally generate the program test
gcc test.o -o test
In the command line window, execute ./test, Let it say HelloWorld Come on!
three Compilation of multiple program files
Generally, the whole program is composed of multiple source files, which correspondingly form multiple compilation units GCC These compilation units can be well managed. Suppose there is a test1.c and test2.c A program composed of two source files. In order to compile them and finally generate an 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 The process of preprocessing, compiling, and linking will still be followed. If you dig deeper, the above command is roughly equivalent to executing the following three commands in sequence:
gcc -c test1.c -o test1.o
gcc -c test2.c -o test2.o
gcc test1.o test2.o -o test
four Error detection
gcc -pedantic illcode.c -o illcode
-pedantic Compilation options do not guarantee that the compiled program ANSI/ISO C Fully compatible with the standard, it can only be used to help Linux Programmers are getting closer to this goal. Or in other words, -pedantic Options can help programmers find some nonconformities ANSI/ISO C Standard code, but not all. In fact, only ANSI/ISO C Only those cases that require compiler diagnostics in language standards can be GCC Find and warn.
except -pedantic outside, GCC There are other compilation options that can also produce useful warnings. Most of these options are -W First, the most valuable number -Wall , using it can make GCC Generate as many warnings as possible.
gcc -Wall illcode.c -o illcode
GCC Although the warning information given cannot be regarded as an error in a strict sense, it is likely to become a shelter for errors. An excellent Linux Programmers should try their best to avoid generating warning messages and keep their code standard and robust. Therefore, it is a commendable behavior to treat warning messages as coding errors! So, when compiling the program -Werror Option, then GCC Compilation will be stopped at all warning places, forcing programmers to modify their code, as follows:
gcc -Werror test.c -o test
five Library File Connection
It is rare to develop software without using third-party function libraries at all. Generally speaking, it requires the support of many function libraries to complete the corresponding functions. From the programmer's perspective, function libraries are actually header files( .h )And library files( so , or lib 、 dll )Collection of.. although Linux Most of the functions under the /usr/include/ Directory, and the library files are placed in /usr/lib/ Directory; Windows The library files used are mainly stored in Visual Stido Under the directory of include and lib , and under the system folder. But sometimes, the library we want to use is not under these directories, so GCC When compiling, you must use your own method to find the required header file and library file.
For example, our program test.c Is on linux Use on c connect mysql , we need to go mysql Download on official website MySQL Connectors Of C Library. After downloading and unzipping, there is a include Folder, which contains mysql connectors The header file of lib Folder containing binary so file libmysqlclient.so
among inclulde The path of the folder is /usr/dev/mysql/include,lib Folder is /usr/dev/mysql/lib
five point one Compile to executable file
First, we need to compile test.c It is the target file, which needs to be executed at this time
gcc –c –I /usr/dev/mysql/include test.c –o test.o
five point two link
Finally, we link all the target files into executable files :
gcc –L /usr/dev/mysql/lib –lmysqlclient test.o –o test
Linux The library files under are divided into two categories: dynamic link libraries (usually .so End) and static link libraries (usually .a The only difference between the two is whether the code required for program execution is dynamically loaded at runtime or statically loaded at compile time.
five point three Use static link libraries when forcing links
By default, GCC The dynamic link library is preferred when linking. The static link library is considered only when the dynamic link library does not exist. If necessary, it can be added at compile time -static Option to force the use of static link libraries.
stay /usr/dev/mysql/lib Library files required when there are links in the directory libmysqlclient.so and libmysqlclient.a , in order to GCC When linking, only the static link library is used. You can use the following command :
gcc –L /usr/dev/mysql/lib –static –lmysqlclient test.o –o test
Search path order when linking static libraries:
1. ld I will find GCC Parameters in the command -L
two Look again gcc Environment variables for LIBRARY_PATH
three Find the internal directory again /lib /usr/lib /usr/local/lib This is the beginning compile gcc When written in the program
Search path order during dynamic link and execution :
one Dynamic library search path specified when compiling object code
two environment variable LD_LIBRARY_PATH Specified dynamic library search path
three configuration file /etc/ld.so.conf Dynamic library search path specified in
four Default dynamic library search path /lib
five Default dynamic library search path /usr/lib
For environment variables:
LIBRARY_PATH Environment variable: specify the program static link library file search path
LD_LIBRARY_PATH Environment variable: specify the program dynamic link library file search path