Linux GCC common commands 1 Introduction 2 simple Compilation 2.1 preprocessing 2.2 Compilation into Assembly code (Compilation) 2.3 Assembly (Assembly) 2.4 connection (Linking) 3 Compilation of multiple program files 4 check errors www.2cto.com 5 Library file connection 5.1 compiled into executable file 5.2 link 5.3 force link when using static Link Library 1 Introduction GCC meaning only 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 example program: // test. c # include <stdio. h> int main (void) {printf ("Hello World! \ N "); return 0;} this program, one-step compilation command is: gcc test. c-o test, in essence, the above Compilation process is divided into four stages, namely Preprocessing (also called Preprocessing), Compilation (Compilation), Assembly (Assembly) and connection (Linking ). 2.1 pre-processing: www.2cto.com gcc-E test. c-o test. I or gcc-E test. c can output the pre-processed code of test. c In the test. I file. Open the test. I file and take a look. The subsequent command directly outputs the pre-processing code in the command line window. The-E Option of gcc allows the compiler to stop the pre-processing and output the pre-processing result. In this example, the pre-processing result is to insert the content in the stdio. h file to test. c. 2.2 After Compilation as Compilation preprocessing, you can directly pair the generated test. compile the I file to generate the assembly code: gcc-S test. i-o test. the-S option of sgcc indicates that, after the assembly code is generated during the program compilation, it is stopped and-o outputs the assembly code file. 2.3 Assembly for the Assembly code file generated in the previous section test. s, the gas compiler is responsible for compiling it into the target file, as follows: gcc-c test. s-o test. the o2.4 Linking 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 the test. o, connect it to the C standard input/output library, and finally generate the program testgcc test. o-o test is executed in the command line window. /test, let it say HelloWorld! 3. Compilation of multiple program files generally consists 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: if gcc test1.c test2.c-o test processes more than one file simultaneously, GCC will continue to process, compile, and link the file in sequence. 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 to check gcc-pedantic illcode. the c-o illcode-pedantic compilation option does not guarantee full compatibility between the compiled program and 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. Although the warning information given by gcc-Wall illcode. c-o illcodeGCC 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 the programmer to modify his code as follows: gcc-Werror test. when c-o test www.2cto.com 5 library files are connected to the development software, it is rare that no third-party function libraries are used at all, generally, functions can be completed only with the support of many function libraries. 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. in so, the path of the inclulde folder is/usr/dev/mysql/include, and the lib folder is/usr/dev/mysql/lib 5.1 compiled into an executable file. First, we need to compile test. c is the target file. In this case, run gcc-c-I/usr/dev/mysql/include test. c-o test. at the end of o5.2, we link all the target files to an executable file: gcc-L/usr/dev/mysql/lib-lmysqlclient test. library files in o-o testLinux It can be divided into two categories: dynamic link library (usually. 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 When a static link library is used for force link, by default, dynamic link library is preferentially used for GCC connection. The static Link Library is considered only when the dynamic link library does not exist, if necessary, you can add the-static option during compilation to force the static link library to be used. 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. search Path order for o-o test www.2cto.com static Library Link: 1. ld will go to the parameter-L2. in the GCC command and find the gcc environment variable LIBRARY_PATH3. find the default directory/lib/usr/local/lib. This is the search path sequence for Dynamic Links written in the program during compile gcc and execution: 1. the dynamic library search path specified when compiling the target Code 2. the dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH 3. configuration file/etc/ld. so. the dynamic library search path specified in conf 4. default dynamic library search path/lib5. default dynamic library search path/usr/lib environment variable: LIBRARY_PATH environment variable: Specify the LD_LIBRARY_PATH environment variable of the static link library file search path: specify the path for searching files in the dynamic link library of the program