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.
#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
3. The process of gcc compilation program
gcc compilation program mainly goes through four processes:
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
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 #include<stdio.h> 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 #include<stdio.h> #include"hello.h" void printHello() { printf("hello world!\n"); }
//main.c #include<stdio.h> #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语言和其他语言进行程序设计的时候,我们需要头文件来提供对常数的定义和对系统及库函数调用的声明。库文件是一些预先编译好的函数集合,那些函数都是按照可重用原则编写的。它们通常由一组互相关联的可重用原则编写的,它们通常由一组互相关联的用来完成某项常见工作的函数构成。使用库的优点在于:
库又可以分为静态库与动态库:
一般头文件或库文件的位置在:
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!