本文阅读用时较长,主张渐渐食用。
神马是gcc/g++
简介:
gcc的全称是GNU Compiler Collection,它是一个能够编译多种言语的编译器。最开端gcc是作为C言语的编译器(GNU C Compiler),现在除了c言语,还支撑C++、java、Pascal等言语。gcc支撑多种硬件渠道。**而g++是GNU的c++编译器。**
gcc/g++的特色
- 是一个可移植的编译器,支撑多种硬件渠道。例如ARM、X86等等。
- 不仅是个本地编译器,它还能跨渠道交叉编译。所谓的本地编译器,是指编译出来的程序只能够在本地环境进行运转。而gcc编译出来的程序能够在其他渠道进行运转。例如嵌入式程序可在x86上编译,然后在arm上运转。
- 有多种言语前端,用于解析不同的言语。
- 是按模块化规划的,能够参加新言语和新CPU架构的支撑。
- 是自由软件。任何人都能够运用或更改这个软件。
gcc/g++编译程序的进程
gcc编译程序主要经过四个进程:
- 预处理(Pre-Processing)
- 编译 (Compiling)
- 汇编 (Assembling)
- 链接 (Linking)
如图所示:
预处理实际上是将头文件、宏进行翻开。编译阶段,gcc调用不同言语的编译器,例如c言语调用编译器ccl。gcc实际上是个东西链,在编译程序的进程中调用不同的东西。汇编阶gcc/g++调用汇编器进行汇编。链接进程会将程序所需要的方针文件进行链接成可履行文件。汇编器生成的是可重定位的方针文件,学过操作系统,咱们知道,在源程序中地址是从0开端的,这是一个相对地址,而程序真实在内存中运转时的地址必定不是从0开端的,而且在编写源代码的时分也不能知道程序的绝对地址,所以**重定位**能够将源代码的代码、变量等定位为内存具体地址。下面以一张图来表明这个进程,留意进程中文件的后缀改变,编译选项和这些后缀有关。
gcc/g++の运用
装置gcc/g++
在对gcc/g++有必定了解后咱们就要装置他们了!
在CentOS下装置gcc/g++
翻开终端,输入以下指令行:
yum install gcc
yum install gcc-c++ libstdc++-devel
yum install glibc-static
装置完成后输入: which g++,就看到g++现已装置完成(一般是在 /usr/bin 目录下)
留意: 在linux下,C++的编译器不是g++这个名称,而是gcc-c++。
在Ubuntu下装置gcc/g++
装置g++编译器的指令:
sudo apt-get install build-essential
履行完后,就完成了gcc,g++,make的装置。build-essential是一整套东西,gcc,libc等等。
输入g++ -v 查看是否成功装置。
gcc指令选项
悉数选项:
翻开终端输入:gcc –help
root@DESKTOP-S8VRDOE:~# gcc --help
Usage: gcc [options] file...
Options:
-pass-exit-codes Exit with highest error code from a phase.
--help Display this information.
--target-help Display target specific command line options.
--help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...].
Display specific types of command line options.
(Use '-v --help' to display command line options of sub-processes).
--version Display compiler version information.
-dumpspecs Display all of the built in spec strings.
-dumpversion Display the version of the compiler.
-dumpmachine Display the compiler's target processor.
-print-search-dirs Display the directories in the compiler's search path.
-print-libgcc-file-name Display the name of the compiler's companion library.
-print-file-name=<lib> Display the full path to library <lib>.
-print-prog-name=<prog> Display the full path to compiler component <prog>.
-print-multiarch Display the target's normalized GNU triplet, used as
a component in the library path.
-print-multi-directory Display the root directory for versions of libgcc.
-print-multi-lib Display the mapping between command line options and
multiple library search directories.
-print-multi-os-directory Display the relative path to OS libraries.
-print-sysroot Display the target libraries directory.
-print-sysroot-headers-suffix Display the sysroot suffix used to find headers.
-Wa,<options> Pass comma-separated <options> on to the assembler.
-Wp,<options> Pass comma-separated <options> on to the preprocessor.
-Wl,<options> Pass comma-separated <options> on to the linker.
-Xassembler <arg> Pass <arg> on to the assembler.
-Xpreprocessor <arg> Pass <arg> on to the preprocessor.
-Xlinker <arg> Pass <arg> on to the linker.
-save-temps Do not delete intermediate files.
-save-temps=<arg> Do not delete intermediate files.
-no-canonical-prefixes Do not canonicalize paths when building relative
prefixes to other gcc components.
-pipe Use pipes rather than intermediate files.
-time Time the execution of each subprocess.
-specs=<file> Override built-in specs with the contents of <file>.
-std=<standard> Assume that the input sources are for <standard>.
--sysroot=<directory> Use <directory> as the root directory for headers
and libraries.
-B <directory> Add <directory> to the compiler's search paths.
-v Display the programs invoked by the compiler.
-### Like -v but options quoted and commands not executed.
-E Preprocess only; do not compile, assemble or link.
-S Compile only; do not assemble or link.
-c Compile and assemble, but do not link.
-o <file> Place the output into <file>.
-pie Create a dynamically linked position independent
executable.
-shared Create a shared library.
-x <language> Specify the language of the following input files.
Permissible languages include: c c++ assembler none
'none' means revert to the default behavior of
guessing the language based on the file's extension.
Options starting with -g, -f, -m, -O, -W, or --param are automatically
passed on to the various sub-processes invoked by gcc. In order to pass
other options on to these processes the -W<letter> options must be used.
For bug reporting instructions, please see:
<file:///usr/share/doc/gcc-11/README.Bugs>.
这个比较复杂,假如英语特别好,且比较闲的人能够看看
常用选项:
选项名 | 效果 |
---|---|
-o | 发生方针(.i、.s、.o、可履行文件等) |
-E | 只运转C预编译器 |
-S | 告知编译器发生汇编程序文件后中止编译,发生的汇编言语文件拓宽名为.s |
-c | 通知gcc取消衔接过程,即编译源码,并在最后生成方针文件 |
-Wall | 使gcc对源文件的代码有问题的地方宣布正告 |
-Idir | 将dir目录参加搜索头文件的目录途径 |
-Ldir | 将dir目录参加搜索库的目录途径 |
-llib | 衔接lib库 |
-g | 在方针文件中嵌入调试信息,以便gdb之类的调试程序调试 |
g++指令选项
悉数选项
root@DESKTOP-S8VRDOE:~# g++ --help
Usage: g++ [options] file...
Options:
-pass-exit-codes Exit with highest error code from a phase.
--help Display this information.
--target-help Display target specific command line options.
--help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...].
Display specific types of command line options.
(Use '-v --help' to display command line options of sub-processes).
--version Display compiler version information.
-dumpspecs Display all of the built in spec strings.
-dumpversion Display the version of the compiler.
-dumpmachine Display the compiler's target processor.
-print-search-dirs Display the directories in the compiler's search path.
-print-libgcc-file-name Display the name of the compiler's companion library.
-print-file-name=<lib> Display the full path to library <lib>.
-print-prog-name=<prog> Display the full path to compiler component <prog>.
-print-multiarch Display the target's normalized GNU triplet, used as
a component in the library path.
-print-multi-directory Display the root directory for versions of libgcc.
-print-multi-lib Display the mapping between command line options and
multiple library search directories.
-print-multi-os-directory Display the relative path to OS libraries.
-print-sysroot Display the target libraries directory.
-print-sysroot-headers-suffix Display the sysroot suffix used to find headers.
-Wa,<options> Pass comma-separated <options> on to the assembler.
-Wp,<options> Pass comma-separated <options> on to the preprocessor.
-Wl,<options> Pass comma-separated <options> on to the linker.
-Xassembler <arg> Pass <arg> on to the assembler.
-Xpreprocessor <arg> Pass <arg> on to the preprocessor.
-Xlinker <arg> Pass <arg> on to the linker.
-save-temps Do not delete intermediate files.
-save-temps=<arg> Do not delete intermediate files.
-no-canonical-prefixes Do not canonicalize paths when building relative
prefixes to other gcc components.
-pipe Use pipes rather than intermediate files.
-time Time the execution of each subprocess.
-specs=<file> Override built-in specs with the contents of <file>.
-std=<standard> Assume that the input sources are for <standard>.
--sysroot=<directory> Use <directory> as the root directory for headers
and libraries.
-B <directory> Add <directory> to the compiler's search paths.
-v Display the programs invoked by the compiler.
-### Like -v but options quoted and commands not executed.
-E Preprocess only; do not compile, assemble or link.
-S Compile only; do not assemble or link.
-c Compile and assemble, but do not link.
-o <file> Place the output into <file>.
-pie Create a dynamically linked position independent
executable.
-shared Create a shared library.
-x <language> Specify the language of the following input files.
Permissible languages include: c c++ assembler none
'none' means revert to the default behavior of
guessing the language based on the file's extension.
Options starting with -g, -f, -m, -O, -W, or --param are automatically
passed on to the various sub-processes invoked by g++. In order to pass
other options on to these processes the -W<letter> options must be used.
For bug reporting instructions, please see:
<file:///usr/share/doc/gcc-11/README.Bugs>.
常用选项
选项名 | 效果 |
---|---|
-B<directory> | 将<director>增加到编译器的搜索途径 |
-v | 显示编译器调用的程序 |
-### | 类似于-v,但选项被引证,指令未被履行 |
-E | 仅预处理;不要编译、汇编或链接 |
-S | 仅编译;不要组装或衔接 |
-c | 编译和汇编,但不链接 |
-o<file> | 将输出放入<file>。 |
-shared | 创建共享库 |
-x | 指定以下输入文件的言语。答应的言语包括:c++汇编程序无“none”表明恢复到默许行为 根据文件的扩展名猜想言语。 |
gcc/g++实战
由于g++和gcc的指令差不多所以下以g++为主
直接生成可履行文件并运转
1:生成可履行文件
首先用vim(能够用其他的编辑器)自己建一个.cpp文件:
root@DESKTOP-S8VRDOE:~/c++# vim hello_world.cpp
写上代码:
退出vim,运用g++编译.cpp
g++ hello_world.cpp
此刻就会发现目录下有一个新文件:a.out ,这个文件便是刚刚生成的可履行文件
root@DESKTOP-S8VRDOE:~/c++# g++ hello_world.cpp
root@DESKTOP-S8VRDOE:~/c++# ll
total 16
drwxr-xr-x 1 root root 512 Oct 22 08:00 ./
drwx------ 1 root root 512 Oct 22 08:00 ../
-rwxr-xr-x 1 root root 16384 Oct 22 08:00 a.out*
-rw-r--r-- 1 root root 91 Oct 22 08:00 hello_world.cpp
(2)运转可履行文件
运转可履行文件:
./a.out
现在就能够看到可履行文件的输出了
root@DESKTOP-S8VRDOE:~/c++# ./a.out
hello world
分进程生成可履行文件并运转
先同上方一致,创立.cpp文件:hello_world.cpp
(1)预处理
g++ -E hello_world.cPP //假如咱们直接履行该指令,预处理的成果会打印在屏幕上。
g++ -E hello_world.cpp -o hello_world.i //咱们能够将预处理的成果放到test.i文件中
留意,有必要放入test.i中,由于鄙人一步编译的进程中,指令只能处理.i的文件。
–* E指令的意义是:翻译进行到预处理之后中止。*
这时就会生成一个hello_world.i文件
root@DESKTOP-S8VRDOE:~/c++# ll
total 848
drwxr-xr-x 1 root root 512 Oct 22 08:12 ./
drwx------ 1 root root 512 Oct 22 08:00 ../
-rwxr-xr-x 1 root root 16384 Oct 22 08:00 a.out*
-rw-r--r-- 1 root root 91 Oct 22 08:00 hello_world.cpp
-rw-r--r-- 1 root root 778705 Oct 22 08:12 hello_world.i
此刻查看下hello_world.i文件
此刻能够发现,头文件现已翻开
(2)编译
g++ -S hello_world.i
这儿就不必指定文件了,它会主动生成一个.s文件
编译时将源代码转换成汇编言语,生成.s文件:
root@DESKTOP-S8VRDOE:~/c++# g++ -S hello_world.i
root@DESKTOP-S8VRDOE:~/c++# ll
total 852
drwxr-xr-x 1 root root 512 Oct 22 08:23 ./
drwx------ 1 root root 512 Oct 22 08:00 ../
-rwxr-xr-x 1 root root 16384 Oct 22 08:00 a.out*
-rw-r--r-- 1 root root 91 Oct 22 08:00 hello_world.cpp
-rw-r--r-- 1 root root 778705 Oct 22 08:12 hello_world.i
-rw-r--r-- 1 root root 2108 Oct 22 08:23 hello_world.s
咱们能够翻开.s文件查看一下汇编代码:
(3)汇编
名为汇编,实际上便是将汇编代码转换为计算机能够读懂的机器码:
g++ -c hello_world.s
此刻会构成一个二进制文件hello_world.o,可是当翻开它时咱们看到的是一个乱码状态,咱们需
要运用二进制的形式来进行翻开:
od hello_world.o
此刻咱们就能够看到对应的机器码了。 留意即使计算机能够看懂这些二进制编码,可是咱们仍然不能通过./来履行这个二进制文件。该文件被称为:可重定向方针文件。
(4)链接
g++ hello_world.o
此刻生成了可履行文件 a.out
root@DESKTOP-S8VRDOE:~/c++# g++ hello_world.o
root@DESKTOP-S8VRDOE:~/c++# ll
total 872
drwxr-xr-x 1 root root 512 Oct 22 08:31 ./
drwx------ 1 root root 512 Oct 22 08:00 ../
-rwxr-xr-x 1 root root 16384 Oct 22 08:31 a.out*
-rw-r--r-- 1 root root 91 Oct 22 08:00 hello_world.cpp
-rw-r--r-- 1 root root 778705 Oct 22 08:12 hello_world.i
-rw-r--r-- 1 root root 2560 Oct 22 08:28 hello_world.o
-rw-r--r-- 1 root root 2108 Oct 22 08:23 hello_world.s
(5)运转可履行文件
运转可履行文件:
./a.out
现在就能够看到可履行文件的输出了!
好啦,文章到这儿就完毕了,还有一些其他的内容之后会渐渐增加
假如喜爱,请给个赞再走呗!(暗示三连)
作者:玄予(xuanyu)
最后更新:2022年12月5日
链接:www.xuanyu.info/p/294.html
本文支撑转载,但请将此块内容一同转载,以保证文章内容的时效性。