Introduction to GCC
1. GCC is the abbreviation of GNU Compiler Collection. Originally, it was a C language compiler (gnu c compiler). The author was Richard Stallman and was the founder of the GNU project. Now it supports multiple languages, such as C, C ++, Java, Pascal, Ada, and Cobol.
2. Main features:
GCC is a portable compiler that supports multiple hardware platforms and even provides comprehensive support for uncommon computers such as Mmix designed by Don knuth.
GCC is not only a local compiler, but also cross-platform cross-compiling (locally compiled programs can run on other platforms ).
GCC has multiple language frontend for parsing different languages.
GCC is modular and can be added with support for new languages and new CPU architectures.
GCC is a free software
GCC program compilation process
1) pre-processing [-E] # expand the header file
2) Compile (Compiling) [-S]
3) Assembly [-C]
4) Link (linking) [no options]
Common options
Option |
Function |
-E |
Preprocessing to generate a. I file |
-S |
Compile and generate the. s Assembly File |
-C |
Compile and compile the source code to generate the. O target file. |
Null |
Link without any options |
-O |
Generate the target file, such as. I,. O,. s, and executable file. The default file is a. Out. |
-Wall |
Issue a warning to GCC when the source code is faulty. |
-I [dir] |
Add dir to the search path of the header file |
-L [dir] |
Add dir to the search path of the input file |
-L [lib] |
Link lib Library |
-G |
Embedded debugging information in the target file for GDB debugging |
-O |
Optimized compiled code |
-W |
Disable all warning information [not recommended] |
Example
//测试程序#include <stdio.h>#define HELLO "Hello World!!!\n"int main(int argc,char *argv[]){ printf(HELLO); return 0;}
Operation:
Gcc-e hello. C-O hello. I (preprocessing)
Gcc-s hello. I-O hello. S (compile and generate assembly code)
Gcc-C hello. S-O hello. O (assembly, generate binary code)
GCC hello. O-O hello (link: no additional options are required to generate an executable file)
GCC hello. C-O hello (directly compile the link into an executable target file)
Gcc-C hello. C or GCC-C hello. C-O hello. O (compile and generate relocated target files)
-Wall usage
We recommend that you add the-wall option for beginners. Some programs do not add the-wall option, and the compiler does not report any errors, but the result is not as expected. As follows:
//bad.c#include <stdio.h>int main(int argc,char *argv[]){ printf("2 + 2 = %f\n",4); return 0;}
GCC compiles multiple files
One-time Compilation
GCC [-wall] hello_fn.c main. C-o newhello
Independent Compilation
Gcc-wall-C main. C-o main. o
Gcc-wall-C hello_fn.c-O hello_fn.o
Gcc-Wall Main. O hello_fn.o-O newhello # Link generation Executable File
Advantage: if only one module is modified, you do not need to compile all modules again to save Compilation Time [recommended]
Appendix-Test procedure
//hello_fu.h#ifndef _HELLO_FUN_H#define _HELLO_FUN_Hvoid hello(const char *str);#endif
//hello_fu.c#include <stdio.h>#include "hello_func.h"void hello(const char *str){ printf("Hello %s\n",str); printf("Compiled: "__DATE__" at "__TIME__"\n"); printf("This is line %d of file %s\n",__LINE__,__FILE__); return;}
//main.c#include "hello_func.h"int main(){ hello("world"); return 0;}
Appendix-makefile
Cc = gcc
Cflags =-wall-G
Bin = Main
Sources = $ (wildcard *. c)
Objects = $ (sources:. c =. O)
. Phony: All clean
ALL: $ (BIN)
$ (BIN): $ (objects)
$ (CC) $ (cflags)-O [email protected] $ ^
%. O: %. c
$ (CC) $ (cflags)-C $ <-O [email protected]
Clean:
-Rm-RF $ (BIN) $ (objects)
GCC needs to recognize file extensions
Extension |
Description |
. C |
C source file |
. CPP/CC |
C ++ source file |
. O |
Target File |
. S |
Assembly language source file |
. A/. So |
Compiled library files |
Error detected using gcc
An error occurred during the link stage!
C/C ++ code after compilation is different
GCC (1) [version 2]