重点解释
-E Preprocess only; do not compile, assemble or link.
预处理 处理头文件信息
-S Compile only; do not assemble or link.
生成汇编文件 可以和-o配合使用
-c Compile and assemble, but do not link.
编译.c文件,生成.o文件 可以和-o配合使用
-o <file> Place the output into <file>.
.o文件生成可执行文件 或者取别名i
-I (大写i) // 指定头文件目录,位置 相对路径/绝对路径都行
-L // 找库路径
-l (小写L) // 链接库
四步流程
1.预处理(cpp) 头文件展开,宏替换,注释去除 gcc -E 生成.i文件
2.编译(gcc) 编译生成汇编文件 gcc -S 生成.s文件
3.汇编(as) 汇编成二进制文件 gcc -c 生成.o文件
4.链接(ld) 链接各二进制文件生成可执行文件 gcc 生成可执行程序
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ gcc -E hello.c -o hello.i
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ la
hello.c hello.i
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ gcc -S hello.i -o hello.s
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ la
hello.c hello.i hello.s
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ gcc -c hello.s -o hello.o
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ la
hello.c hello.i hello.o hello.s
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ gcc hello.o -o hello
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ la
hello hello.c hello.i hello.o hello.s
nianxing_su@nianxingsu-virtual-machine:~/syh/MakeFile/hello$ ./hello
Hello World.
预处理(.i文件还是c文件主要时头文件展开,宏替换,注释去除)
编译(编译成汇编文件 .s)
汇编(生成二进制文件 .o)
链接(链接各二进制文件生成可执行文件)
汇总
gcc hello.c // 集合上面4步直接从源文件生成一个可执行程序,默认时a.exe或a.out -o 取别名
gcc hello.c -o hello
gcc常用选项
-o file 指定生成输出文件名为file
-E 只进行预处理
-S 只进行预处理和编译
-c 只进行预处理,编译和汇编
-v 查看gcc版本
-g 包含调试信息
-On n=0~3 编译优化,n越大优化得越多
-Wall 提示更多警告信息
-D 编译时定义宏
gcc -Wall -Werror test // 出现警告就当成错误信息处理
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("SIZE:%d\n",SIZE);
return 0;
}
#include <stdio.h>
#define SIZE 99
int main(int argc, char const *argv[])
{
printf("SIZE:%d\n",SIZE);
return 0;
}
gcc/g++指令信息
gcc --help或者g++ --help
C:\Users\Administrator>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:
<https://sourceforge.net/projects/mingw-w64>.