GCC compilation common commands

GCC compile command

                                       ---------------- After joining the new company, it is basically a one-click packaging script. I almost forgot about GCC and picked it up again.
GCC commands provide a lot of command options, but not all of them need to be familiar. It is enough to master a few commonly used ones at the beginning, and then slowly learn other options later, so as not to damage the confidence of learning due to too many options.

1. Common compile command options

Suppose the source program file is named test.c.

1. Compile and link without option
Usage: #gcc test.c
Function: Preprocess, assemble, compile and link test.c to form an executable file . No output file is specified here, the default output is a.out .

2. Option -o
usage: #gcc test.c -o test
Function: preprocess, assemble, compile and link test.c to form the executable file test. The -o option is used to specify the filename of the output file.

3. Option -E
usage: #gcc -E test.c -o test.i
Function: preprocess test.c to output test.i file.

4. Option -S
usage: #gcc -S test.i
Function: Assemble the preprocessing output file test.i into a test.s file.

5. Option -c
usage: #gcc -c test.s
Function: Compile the assembly output file test.s to output the test.o file.

6. Link without option
Usage: #gcc test.o -o test
Function: Link the compiled output file test.o into the final executable file test.

7. Option -O
usage: #gcc -O1 test.c -o test
Function: Compile the program with compilation optimization level 1. The level is 1~3. The higher the level, the better the optimization effect , but the longer the compilation time.

2. Compilation method of multi-source files

If there are multiple source files, there are basically two ways to compile:
[Assume there are two source files test.c and testfun.c]

1. Compile multiple files together
Usage: #gcc testfun.c test.c -o test
Function: Compile testfun.c and test.c respectively and link them into a test executable file.

2. Compile each source file separately, and then link the compiled output object file.
Usage:
#gcc -c testfun.c //compile testfun.c into testfun.o
#gcc -c test.c //compile test.c into test.o
#gcc -o testfun.o test.o -o test //Link testfun.o and test.o into test

Comparing the above two methods, the first method requires all files to be recompiled when compiling, while the second method can only recompile the modified files, and the unmodified files do not need to be recompiled.

3Library file connection

开发软件时,完全不使用第三方函数库的情况是比较少见的,通常来讲都需要借助许多函数库的支持才能够完成相应的功能。从程序员的角度看,函数库实际上就是一些头文件(.h)和库文件(so、或lib、dll)的集合。。虽然Linux下的大多数函数都默认将头文件放到/usr/include/目录下,而库文件则放到/usr/lib/目录下;Windows所使用的库文件主要放在Visual Stido的目录下的include和lib,以及系统文件夹下。但也有的时候,我们要用的库不再这些目录下,所以GCC在编译时必须用自己的办法来查找所需要的头文件和库文件。

例如我们的程序test.c是在linux上使用c连接mysql,这个时候我们需要去mysql官网下载MySQL Connectors的C库,下载下来解压之后,有一个include文件夹,里面包含mysql connectors的头文件,还有一个lib文件夹,里面包含二进制so文件libmysqlclient.so

其中inclulde文件夹的路径是/usr/dev/mysql/include,lib文件夹是/usr/dev/mysql/lib

 

3.1编译成可执行文件

首先我们要进行编译test.c为目标文件,这个时候需要执行

gcc –c –I /usr/dev/mysql/include test.c –o test.o
3.2链接

最后我们把所有目标文件链接成可执行文件:

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

Linux下的库文件分为两大类分别是动态链接库(通常以.so结尾)和静态链接库(通常以.a结尾),二者的区别仅在于程序执行时所需的代码是在运行时动态加载的,还是在编译时静态加载的。

3.3强制链接时使用静态链接库

默认情况下, GCC在链接时优先使用动态链接库,只有当动态链接库不存在时才考虑使用静态链接库,如果需要的话可以在编译时加上-static选项,强制使用静态链接库。

在/usr/dev/mysql/lib目录下有链接时所需要的库文件libmysqlclient.so和libmysqlclient.a,为了让GCC在链接时只用到静态链接库,可以使用下面的命令:

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

 

静态库链接时搜索路径顺序:

1. ld会去找GCC命令中的参数-L
2. 再找gcc的环境变量LIBRARY_PATH
3. 再找内定目录 /lib /usr/lib /usr/local/lib 这是当初compile gcc时写在程序内的

动态链接时、执行时搜索路径顺序:

1. 编译目标代码时指定的动态库搜索路径
2. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径
3. 配置文件/etc/ld.so.conf中指定的动态库搜索路径
4. 默认的动态库搜索路径/lib
5. 默认的动态库搜索路径/usr/lib

有关环境变量:
LIBRARY_PATH环境变量:指定程序静态链接库文件搜索路径
LD_LIBRARY_PATH环境变量:指定程序动态链接库文件搜索路径

---摘自:https://www.cnblogs.com/ibyte/p/5828445.html

更详细的介绍请参考:

https://blog.csdn.net/junmuzi/article/details/50924233

https://blog.csdn.net/qq_29350001/article/details/53339861

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732265&siteId=291194637