一、安裝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/