Specific use of Linux gcc command

  • 2021-07-26 09:07:50
  • OfStack

01. Command Overview

gcc command uses the compiler based on C/C + + introduced by GNU, which is the most widely used compiler in the open source field. It has the characteristics of powerful functions and compiling code supporting performance optimization.

gcc is the GNU compiler suite (GNU Compiler Collection), which includes the front end of C, C + +, Objective-C, Fortran, Java, Ada, Go and D languages, as well as libraries of these languages (such as libstdc + +, libgcj, etc.). The original intention of GCC is a compiler specially written for GNU operating system. The GNU system is completely free software. Here, "freedom" means that it respects the freedom of users.

02. Command format

Usage: gcc [Options] file...

03. Common Options


 -pass-exit-codes     在某1阶段退出时返回最高的错误码
 --help          显示此帮助说明
 --target-help      显示目标机器特定的命令行选项
 --help={common|optimizers|params|target|warnings|[^]
 {joined|separate|undocumented}}[,...]
              显示特定类型的命令行选项
 (使用‘-v --help'显示子进程的命令行参数)
 --version        显示编译器版本信息
 -dumpspecs        显示所有内建 spec 字符串
 -dumpversion       显示编译器的版本号
 -dumpmachine       显示编译器的目标处理器
 -print-search-dirs    显示编译器的搜索路径
 -print-libgcc-file-name 显示编译器伴随库的名称
 -print-file-name=<库>  显示 <库> 的完整路径
 -print-prog-name=<程序> 显示编译器组件 <程序> 的完整路径
 -print-multiarch     Display the target's normalized GNU triplet, used as
              a component in the library path
 -print-multi-directory  显示不同版本 libgcc 的根目录
 -print-multi-lib     显示命令行选项和多个版本库搜索路径间的映射
 -print-multi-os-directory 显示操作系统库的相对路径
 -print-sysroot      显示目标库目录
 -print-sysroot-headers-suffix 显示用于寻找头文件的 sysroot 后缀
 -Wa,<选项>        将逗号分隔的 <选项> 传递给汇编器
 -Wp,<选项>        将逗号分隔的 <选项> 传递给预处理器
 -Wl,<选项>        将逗号分隔的 <选项> 传递给链接器
 -Xassembler <参数>    将 <参数> 传递给汇编器
 -Xpreprocessor <参数>  将 <参数> 传递给预处理器
 -Xlinker <参数>     将 <参数> 传递给链接器
 -save-temps       不删除中间文件
 -save-temps=<arg>    不删除中间文件
 -no-canonical-prefixes  生成其他 gcc 组件的相对路径时不生成规范化的
              前缀
 -pipe          使用管道代替临时文件
 -time          为每个子进程计时
 -specs=<文件>      用 <文件> 的内容覆盖内建的 specs 文件
 -std=<标准>       指定输入源文件遵循的标准
 --sysroot=<目录>     将 <目录> 作为头文件和库文件的根目录
 -B <目录>        将 <目录> 添加到编译器的搜索路径中
 -v            显示编译器调用的程序
 -###           与 -v 类似,但选项被引号括住,并且不执行命令
 -E            仅作预处理,不进行编译、汇编和链接
 -S            编译到汇编语言,不进行汇编和链接
 -c            编译、汇编到目标代码,不进行链接
 -o <文件>        输出到 <文件>
 -pie           Create a position independent executable
 -shared         Create a shared library
 -x <语言>        指定其后输入文件的语言
              允许的语言包括:c c++ assembler none
              ‘none'意味着恢复默认行为,即根据文件的扩展名猜测
              源文件的语言

04. Reference example

4.1 Generate the default executable


[deng@localhost bak]$ gcc test.c 
[deng@localhost bak]$

Preprocess, assemble, compile and link test. c to form an executable file. No output file is specified here, and the default output is a. out.

4.2 Specifying Output Files


[deng@localhost bak]$ gcc test.c -o test
[deng@localhost bak]$ ls
5th 6th 7th 8th 9th test test.c
[deng@localhost bak]$ 
 

test. c is preprocessed, assembled, compiled and linked to form an executable file test. The-o option specifies the file name of the output file.

4.3 Preprocessing only, no compilation, assembly, and linking


[deng@localhost bak]$ gcc -E test.c -o test.i 
[deng@localhost bak]$ 

Preprocess test. c and output test. i file.

4.4 Compile to assembly language without assembly and linking


[deng@localhost bak]$ gcc -S test.c -o test.s
[deng@localhost bak]$ ls
5th 6th 7th 8th 9th test test.c test.i test.s
[deng@localhost bak]$ 

The preprocessing output file test. i is assembled into an test. s file.

4.5 Compile, assemble to object code without linking


[deng@localhost bak]$ gcc -c test.c -o test.o
[deng@localhost bak]$ ls
5th 6th 7th 8th 9th test test.c test.i test.o test.s
[deng@localhost bak]$ 

4.6 Generate the object code into an executable file


[deng@localhost bak]$ gcc test.o -o test
[deng@localhost bak]$ 

Link the compiled output file test. o to the final executable file test.

4.7 Specifying optimization levels at compile time


[deng@localhost bak]$ gcc -O1 test.c -o test
[deng@localhost bak]$ 

Compile the program using Compilation Optimization Level 1. Level 1 ~ 3, the higher the level, the better the optimization effect, but the longer the compilation time.

4.8 Multi-file Compilation


[deng@localhost bak]$ gcc testfun.c test.c -o test
[deng@localhost bak]$ 

testfun. c and test. c are compiled and linked into test executable files.

4.9 Multiple File Compilation Method 2


[deng@localhost bak]$ gcc -c test.c  
[deng@localhost bak]$ gcc -c testfun.c  
[deng@localhost bak]$ gcc test.o testfun.o -o test
[deng@localhost bak]$ 

Related articles: