一、安装gcc:
1、使用 yum list gcc*
查询 centos 官方gcc的所有包:
可安装的软件包
gcc.x86_64
gcc-c++.x86_64
gcc-gfortran.x86_64
gcc-gnat.x86_64
gcc-go.x86_64
gcc-objc.x86_64
gcc-objc++.x86_64
gcc-plugin-devel.x86_64
2、根据需要安装包,编辑c , c++ 需要安装 gcc.x86_64 和 gcc-c++.x86_64
yum -y install gcc.x86_64
yum -y install gcc-c++.x86_64
使用gcc:
示例程序如下:
//test.c
#include <stdio.h>
int main()
{
printf("Hello Centos 7!\n");
return 0;
}
二 通过gcc --help查看gcc命令参数
- 看不懂可以直接看下一节常用编译命令选项,会有详细讲解
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 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.
三 常用编译命令选项
假设源程序文件名为test.c
-
无选项编译链接
用法:#gcc test.c
作用:将test.c预处理、汇编、编译并链接形成可执行文件。这里未指定输出文件,默认输出为a.out。编译成功后可以看到生成了一个a.out的文件。在命令行输入./a.out 执行程序。./表示在当前目录,a.out为可执行程序文件名。 -
选项 -o
用法:#gcc test.c -o test
作用:将test.c预处理、汇编、编译并链接形成可执行文件test。-o选项用来指定输出文件的文件名。输入./test执行程序。 -
选项 -E
用法:#gcc -E test.c -o test.i
作用:将test.c预处理输出test.i文件。 -
选项 -S
用法:#gcc -S test.i
作用:将预处理输出文件test.i汇编成test.s文件。 -
选项 -c
用法:#gcc -c test.s
作用:将汇编输出文件test.s编译输出test.o文件。 -
无选项链接
用法:#gcc test.o -o test
作用:将编译输出文件test.o链接成最终可执行文件test。输入./test执行程序。
如果想直接输入test就运行,需要把test复制到目录/usr/bin下
- 选项-O
用法:#gcc -O1 test.c -o test
作用:使用编译优化级别1编译程序。级别为1~3,级别越大优化效果越好,但编译时间越长。输入./test执行程序。
8.编译使用C++ std库的程序
用法:#gcc test.cpp -o test -l std c++
作用:将test.cpp编译链接成test可执行文件。-l std c++指定链接std c++库。
四 多源文件的编译方法
如果有多个源文件,基本上有两种编译方法:
[假设有两个源文件为test.c和testfun.c]
-
多个文件一起编译
用法:#gcc testfun.c test.c -o test
作用:将testfun.c和test.c分别编译后链接成test可执行文件。 -
分别编译各个源文件,之后对编译后输出的目标文件链接。 用法:
#gcc -c testfun.c //将testfun.c编译成testfun.o #gcc -c test.c //将test.c编译成test.o
#gcc -o testfun.o test.o -o test //将testfun.o和test.o链接成test 以上两种方法相比较,第一中方法编译时需要所有文件重新编译,而第二种方法可以只重新编译修改的文件,未修改的文件不用重新编译。
参考:http://www.crs811.com/index.php/2016/03/01/linuxcentos-7-gcc/