g++命令总结

一、g++简介

g++是c++编译器的一种,主要编译.cpp,对于c用gcc。

在终端输入g++ --help,得到以下代码

 1 Usage: g++ [options] file...
 2 Options:
 3   -pass-exit-codes         Exit with highest error code from a phase
 4   --help                   Display this information
 5   --target-help            Display target specific command line options
 6   --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
 7                            Display specific types of command line options
 8   (Use '-v --help' to display command line options of sub-processes)
 9   --version                Display compiler version information
10   -dumpspecs               Display all of the built in spec strings
11   -dumpversion             Display the version of the compiler
12   -dumpmachine             Display the compiler's target processor
13   -print-search-dirs       Display the directories in the compiler's search path
14   -print-libgcc-file-name  Display the name of the compiler's companion library
15   -print-file-name=<lib>   Display the full path to library <lib>
16   -print-prog-name=<prog>  Display the full path to compiler component <prog>
17   -print-multiarch         Display the target's normalized GNU triplet, used as
18                            a component in the library path
19   -print-multi-directory   Display the root directory for versions of libgcc
20   -print-multi-lib         Display the mapping between command line options and
21                            multiple library search directories
22   -print-multi-os-directory Display the relative path to OS libraries
23   -print-sysroot           Display the target libraries directory
24   -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
25   -Wa,<options>            Pass comma-separated <options> on to the assembler
26   -Wp,<options>            Pass comma-separated <options> on to the preprocessor
27   -Wl,<options>            Pass comma-separated <options> on to the linker
28   -Xassembler <arg>        Pass <arg> on to the assembler
29   -Xpreprocessor <arg>     Pass <arg> on to the preprocessor
30   -Xlinker <arg>           Pass <arg> on to the linker
31   -save-temps              Do not delete intermediate files
32   -save-temps=<arg>        Do not delete intermediate files
33   -no-canonical-prefixes   Do not canonicalize paths when building relative
34                            prefixes to other gcc components
35   -pipe                    Use pipes rather than intermediate files
36   -time                    Time the execution of each subprocess
37   -specs=<file>            Override built-in specs with the contents of <file>
38   -std=<standard>          Assume that the input sources are for <standard>
39   --sysroot=<directory>    Use <directory> as the root directory for headers
40                            and libraries
41   -B <directory>           Add <directory> to the compiler's search paths
42   -v                       Display the programs invoked by the compiler
43   -###                     Like -v but options quoted and commands not executed
44   -E                       Preprocess only; do not compile, assemble or link
45   -S                       Compile only; do not assemble or link
46   -c                       Compile and assemble, but do not link
47   -o <file>                Place the output into <file>
48   -pie                     Create a position independent executable
49   -shared                  Create a shared library
50   -x <language>            Specify the language of the following input files
51                            Permissible languages include: c c++ assembler none
52                            'none' means revert to the default behavior of
53                            guessing the language based on the file's extension
54 
55 Options starting with -g, -f, -m, -O, -W, or --param are automatically
56  passed on to the various sub-processes invoked by g++.  In order to pass
57  other options on to these processes the -W<letter> options must be used.

以1.cpp为例:

    命令:g++ 1.cpp

    功能:生成默认为a.exe的文件,包含了编译链接。 

二、常用命令

1.    g++   -E    1.cpp  > 1.i                  Preprocess only; do not compile, assemble or link 

   只预处理,不生成文件。这一步主要进行宏的替换、注释消除、找到库文件。1.i 中会有很多代码。

2.   g++   -S   1.cpp                           Compile only; do not assemble or link

  只编译,不汇编、不连接 ,生成1.s.,里面是汇编指令

3.   g++    -c 1.cpp                            Compile and assemble, but do not link

  从汇编生成目标代码(机器码).   生成1.o文件。

4.   g++ 1.o    -L  <PATH>           

  链接目标代码,生成可执行程序

5.  g++    xxx.x    -o   yyy.x

  输出自己想要的名字。

三、程序举例

1.    g++   -E    1.cpp  > 1.i                  Preprocess only; do not compile, assemble or link 

   只预处理,不生成文件。这一步主要进行宏的替换、注释消除、找到库文件。1.i 中会有很多代码。

 

 生成了一万八千多行代码。

 

2.   g++   -S   1.cpp                           Compile only; do not assemble or link

  只编译,不汇编、不连接 ,生成1.s.,里面是汇编指令

 

3.   g++    -c 1.cpp                            Compile and assemble, but do not link

  从汇编生成目标代码(机器码).   生成1.o文件。

  $   gedit 1.o

4.   g++ 1.o    -L  <PATH>           

  链接目标代码,生成可执行程序

  g++ hello.o -o hello.out -L ./

5.  g++    xxx.x    -o   yyy.x

  输出自己想要的名字。

posted on 2019-09-04 17:50  现在种树也不晚  阅读(13564)  评论(0编辑  收藏  举报

导航