Common Linux GCC Commands

(Click the public account above to quickly follow)

Source: ggjucheng

Link: http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html

1 Introduction

The meaning of GCC is just GNU C Compiler. After so many years of development, GCC now supports not only C language; it also supports Ada, C++, Java, Objective C, Pascal, COBOL, and even functional and logic programming languages like Mercury, etc. GCC has evolved from being just a GNU C language compiler to becoming the GNU Compiler Collection, which means the GNU compiler family. On the other hand, when it comes to the support of operating system platforms and hardware platforms by GCC, it can be summed up in one sentence: it is 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 compilation process above is divided into four stages: preprocessing, compilation, assembly, and linking.

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

This outputs the code after preprocessing into the test.i file. Open the test.i file to take a look, and you’ll understand. The latter command 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 results. In this case, the preprocessing result is that the contents of the stdio.h file are inserted into test.c.

2.2 Compile to Assembly Code (Compilation)

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

gcc -S test.i -o test.s

The -S option of gcc indicates that during the compilation, it stops after generating the assembly code, and -o specifies the output 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 an object file, as follows:

gcc -c test.s -o test.o
2.4 Linking

The gcc linker provided by gas is responsible for linking the program’s object files with all required additional object files to finally generate an executable file. The additional object files include static and dynamic link libraries.

For the test.o generated in the previous section, it is linked with the C standard input output library to finally generate the program test:

gcc test.o -o test

In the command line window, execute ./test to let it say Hello World!

3 Compiling Multiple Program Files

Typically, an entire program consists of multiple source files, which correspondingly form multiple compilation units. Using GCC can effectively manage these compilation units. Suppose there is a program composed of two source files, test1.c and test2.c. To compile them and finally generate the executable program test, you can use the following command:

gcc test1.c test2.c -o test

If multiple files are processed at the same time, GCC will still proceed sequentially through preprocessing, compilation, and linking. If you delve deeper, the above command is roughly equivalent to executing the following three commands sequentially:

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

4 Error Checking

gcc -pedantic illcode.c -o illcode

The -pedantic compilation option does not guarantee that the compiled program is fully compliant with the ANSI/ISO C standard; it can only help Linux programmers get closer to this goal. 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 situations required by the ANSI/ISO C language standard for compiler diagnostics may be detected and warned by GCC.

In addition to -pedantic, GCC has some other compilation options that can also produce useful warning messages. Most of these options start with -W, with -Wall being the most valuable. Using it can make GCC produce as many warning messages as possible.

gcc -Wall illcode.c -o illcode

Although the warning messages given by GCC cannot strictly be considered errors, they can very well become the breeding ground for errors. A good Linux programmer should try to avoid generating warning messages and keep their code standard and robust. Therefore, treating warning messages as coding errors is commendable! So, when compiling programs, adding the -Werror option will make GCC stop compiling at all places that generate warnings, forcing the programmer to modify their code, as shown below:

gcc -Werror test.c -o test

5 Linking Library Files

In software development, it is quite rare to completely avoid using third-party function libraries; generally speaking, many function libraries are needed to complete the corresponding functions. From the programmer’s perspective, a function library is actually a collection of header files (.h) and library files (so, or lib, dll). Although most functions in Linux default to placing header files in the /usr/include/ directory and library files in the /usr/lib/ directory, the library files used in Windows are mainly placed in the include and lib directories under Visual Studio and the system folder. However, sometimes, the libraries we need are not in these directories, so GCC must find the required header files and library files in its own way during compilation.

For example, if our program test.c connects to MySQL on Linux, we need to download the MySQL Connectors C library from the MySQL website, unzip it, and there is an include folder containing the MySQL connectors’ header files, and a lib folder containing the binary so file libmysqlclient.so.

The path of the include folder is /usr/dev/mysql/include, and the lib folder is /usr/dev/mysql/lib.

5.1 Compile to Executable File

First, we need to compile test.c into an object file, at which point we need to execute:

gcc -c -I /usr/dev/mysql/include test.c -o test.o
5.2 Linking

Finally, we link all object files into an executable file:

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

In Linux, library files are divided into two categories: dynamic link libraries (usually ending with .so) and static link libraries (usually ending with .a). The difference between the two lies in whether the code required for the program is dynamically loaded at runtime or statically loaded at compile time.

5.3 Force Linking to Use Static Link Libraries

By default, GCC prefers to use dynamic link libraries when linking, and only considers using static link libraries when dynamic link libraries do not exist. If necessary, the -static option can be added during compilation to force the use of static link libraries.

In the /usr/dev/mysql/lib directory, there are the library files libmysqlclient.so and libmysqlclient.a needed for linking. To make GCC use only static link libraries during linking, you can use the following command:

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

The search order for static library linking is:

1. ld will look for the -L parameter in the GCC command. 2. Then look for the gcc environment variable LIBRARY_PATH. 3. Then look for the default directories /lib /usr/lib /usr/local/lib that were written into the program when compiling gcc.

The search order for dynamic linking at runtime is:

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. The dynamic library search path specified in the configuration file /etc/ld.so.conf. 4. The default dynamic library search path /lib. 5. The default dynamic library search path /usr/lib.

About environment variables:

The LIBRARY_PATH environment variable: specifies the search path for program static link library files. The LD_LIBRARY_PATH environment variable: specifies the search path for program dynamic link library files.

[Today’s WeChat public account recommendation↓]

Common Linux GCC Commands

For more recommendations, please seeTechnical and Design Public Accounts Worth Following

Among them, recommendations include popular public accounts related to technology, design, geeks, and IT matchmaking. Technology covers: Python, Web front-end, Java, Android, iOS, PHP, C/C++, .NET, Linux, databases, operations, big data, algorithms, IT workplace, etc. Click “Technical and Design Public Accounts Worth Following” to discover exciting content!

Common Linux GCC Commands

Leave a Comment