Home  >  Article  >  Operation and Maintenance  >  What is gcc in linux

What is gcc in linux

青灯夜游
青灯夜游Original
2023-04-18 13:41:464330browse

In Linux, the full name of gcc is "GNU Compiler Collection", which means "GNU Compiler Suite" in Chinese. It is a programming language compiler developed by GNU and is a compiler that can compile multiple languages. The gcc suite includes C, C, Objective-C, Fortran, Java, Ada and Go language front ends, as well as libraries for these languages.

What is gcc in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

1. What is gcc

GCC (GNU Compiler Collection, GNU compiler suite) is a programming language compiler developed by GNU. It is a compiler capable of compiling multiple languages. The GNU compiler suite includes C, C, Objective-C, Fortran, Java, Ada and Go language front ends, as well as libraries for these languages ​​(such as libstdc, libgcj, etc.)

Initially gcc was used as C The language compiler (GNU C Compiler) now supports C, Java, Pascal and other languages ​​in addition to the C language. gcc supports multiple hardware platforms.

2. Features of gcc

  • gcc is a portable compiler that supports multiple hardware platforms. For example, ARM, X86, etc.
  • gcc is not only a local compiler, it can also cross-compile across platforms. The so-called local compiler means that the compiled program can only be run in the local environment. Programs compiled by gcc can be run on other platforms. For example, embedded programs can be compiled on x86 and then run on arm.
  • gcc has multiple language front ends for parsing different languages.
  • gcc is designed to be modular and can add support for new languages ​​and new CPU architectures.
  • gcc is free software. Anyone can use or change this software.

3. The process of gcc compilation program

gcc compilation program mainly goes through four processes:

  • Pre-Processing
  • Compiling
  • Assembling
  • Linking
What is gcc in linux


Preprocessing actually expands header files and macros. During the compilation phase, gcc calls compilers of different languages, for example, c language calls the compiler ccl. Gcc is actually a tool chain that calls different tools during the process of compiling a program. During the assembly phase, gcc calls the assembler for assembly. The linking process will link the object files required by the program into an executable file. The assembler generates a relocatable object file. After studying the operating system, we know that the address in the source program starts from 0. This is a relative address, and the address when the program is actually running in the memory is definitely not from It starts with 0, and the absolute address of the program cannot be known when writing the source code, so Relocation can locate the source code codes, variables, etc. as specific memory addresses.

The following is a picture to represent this process. Pay attention to the suffix changes of the files during the process. The compilation options are related to these suffixes.

These are the four steps of GCC compilation.

4. Common gcc options

Let’s take a look at the common gcc options

What is gcc in linux

Now we have the source file hello.c, here are some gcc usage examples:

gcc -E hello.c -o hello.i   对hello.c文件进行预处理,生成了hello.i 文件
gcc -S hello.i -o hello.s    对预处理文件进行编译,生成了汇编文件
gcc -c hello.s -o hello.o  对汇编文件进行编译,生成了目标文件
gcc hello.o -o hello 对目标文件进行链接,生成可执行文件
gcc hello.c -o hello 直接编译链接成可执行目标文件
gcc -c hello.c 或 gcc -c hello.c -o hello.o 编译生成可重定位目标文件

使用gcc时可以加上-Wall选项。下面这个例子如果不加上-Wall选项,编译器不会报出任何错误或警告,但是程序的结果却不是预期的:

//bad.c
#includeade979de5fc0e1ca0540f360a64c230b
int main()
{
    printf("the number is %f ",5);  //程序输出了the number is 0.000000,结果错误
    return 0;
 }

使用-Wall选项:

gcc -Wall bad.c -o bad

gcc将输出警告信息:

warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
printf("the number is %f\n",5);

5、gcc编译多个文件

// hello.c
#includeade979de5fc0e1ca0540f360a64c230b
#include"hello.h"
void printHello()
{
        printf("hello world!\n");
}
//main.c
#includeade979de5fc0e1ca0540f360a64c230b
#include"hello.h"
int main()
{
        printHello();
        return 0;
}
//hello.h
//仅包含函数声明
#ifndef _HELLO_
#define _HELLO_
void printHello();
#endif

编译这三个文件,可以一次编译:

gcc hello.c main.c -o main 生成可执行文件main

也可以独立编译:

gcc -Wall -c main.c -o main.o
gcc -Wall -c hello.c -o hello.o
gcc -Wall main.o hello.o -o main

独立编译的好处是,当其中某个模块发送改变时,只需要编译该模块就行,不必重新编译所有文件,这样可以节省编译时间。

6、使用外部库

在使用C语言和其他语言进行程序设计的时候,我们需要头文件来提供对常数的定义和对系统及库函数调用的声明。库文件是一些预先编译好的函数集合,那些函数都是按照可重用原则编写的。它们通常由一组互相关联的可重用原则编写的,它们通常由一组互相关联的用来完成某项常见工作的函数构成。使用库的优点在于:

  • 模块化的开发
  • 可重用性
  • 可维护性

库又可以分为静态库与动态库:

  • 静态库(.a):程序在编译链接的时候把库的代码链接到可执行文件中。程序运行的时候将不再需要静态库。静态库比较占用磁盘空间,而且程序不可以共享静态库。运行时也是比较占内存的,因为每个程序都包含了一份静态库。
  • 动态库(.so或.sa):程序在运行的时候才去链接共享库的代码,多个程序共享使用库的代码,这样就减少了程序的体积。

一般头文件或库文件的位置在:

  • /usr/include及其子目录底下的include文件夹
  • /usr/local/include及其子目录底下的include文件夹
  • /usr/lib
  • /usr/local/lib
  • /lib

7、生成静态库

为了生成.a文件,我们需要先生成.o文件。下面这行命令将我们的hello.o打包成静态库libhello.a:

ar rcs libhello.a hello.o

ar是gun归档工具,rcs表示replace and create,如果libhello之前存在,将创建新的libhello.a并将其替换。

然后就可以这样来使用静态库libhello.a

gcc -Wall main.c libhello.a -o main

还有另外一种使用方式:

gcc -Wall -L. main.c -o main -lhello 【lhello 是 libhello的缩写】

其中 -L.表示库文件的位置在当前目录下,由于libhello.a是我们自己生成的,并存放在当前录下下,所以需要加上-L.选项。默认库文件是在系统的目录下进行搜索。同样的,-I.选项用于头文件的搜索。

8、生成共享库

生成一个共享库,名称的规则是libxxx.so。将刚才hello.o生成libhello.so的命令为:

gcc -shared -fPIC hello.o -o libhello.so

生成了共享库之后,可以这样来使用共享库:

gcc -Wall main.o -o main -L. -lhello

该命令与使用静态库的命令相同,但是在共享库与静态库共存的情况下,优先使用共享库。

共享库有时候并不不在当前的目录下,为了让gcc能够找得到共享库,有下面几种方法:

  • 拷贝.so文件到系统共享库路径下,一般指/usr/lib

  • 在~/.bash_profile文件中,配置LD_LIBRARY_PATH变量

  • 配置/etc/ld.so.conf,配置完成后调用ldconfig更新ld.so.cache

其中,shared选项表示生成共享库格式。fPIC表示产生位置无关码(position independent code),位置无关码表示它的运行、加载与内存位置无关,可以在任何内存地址进行加载。

9、库的搜索路径

库的搜索路径遵循几个搜索原则:从左到右搜索-I -l指定的目录,如果在这些目录中找不到,那么gcc会从由环境 变量指定的目录进行查找。头文件的环境变量是C_INCLUDE_PATH,库的环境变量是LIBRARY_PATH.如果还是找不到,那么会从系统指定指定的目录进行搜索。

相关推荐:《Linux视频教程

The above is the detailed content of What is gcc in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is linux firmwareNext article:what is linux firmware