命令行大全

cc

小猪老师 发表于 2020-07-18 03:14浏览次数:

cc命令运行系统的C 编译器。

查看英文版

目录:

1 cc 运行系统环境

2 cc 描述

3 cc 语法

4 cc 选项

5 cc 环境变量

6 cc 示例

cc 运行系统环境

Unix&Linux

cc 描述

编译器的基本职责是将计算机程序从一种语言翻译成另一种语言,并且通常在一次编译中执行一次以上。

编译用C编写的程序时,编译器将通过几个步骤将其转换为可在目标体系结构平台上执行的机器代码

通常,编译器对您的代码执行以下功能:

  1. 预处理。C预处理器(“ cpp”)通过删除注释,扩展并执行其他一些重要步骤来准备将要发送到实际编译器的代码
  2. 语法检查。您的程序是否包含任何语法错误?如果是这样,编译器会告知您,并且您必须先修复它们,然后再继续。如果怀疑程序的某些部分不正确,但是不确定它们是否确实是错误的,则它可能会发出警告,但仍会编译程序。
  3. 转换为汇编语言。这是一个中间步骤,其中您的代码被翻译为目标系统的汇编语言。这几乎是人类仍然可以阅读您的代码的最后一步。
  4. 部件。您的程序被组装为适合您目标平台的目标代码字节
  5. 连结中。目标代码程序和命令行选项指定的任何库函数的目标代码链接在一起,并创建了一个可执行文件

The compiler's essential duty is to translate a computer program from one language to another, and it usually does this more than once in the a single compilation.

When compiling a program written in C, the compiler goes through several steps to translate it into machine code that can be executed on your target architecture or platform.

In general, the compiler performs the following functions on your code:

  1. Pre-processing. The C preprocessor ("cpp") prepares your code to be sent to the actual compiler by removing comments, expanding macros, and performing a few other important steps.
  2. Syntax checking. Does your program contain any syntactical errors? If so, the compiler will let you know, and you will have to fix them before you proceed. If it suspects some parts of your program are not correct, but it's not sure that they are actually errors, it may issue warnings, but still compile the program.
  3. Conversion to assembly language. This is an intermediary step where your code is translated into the language of your target system's assembly language. This is pretty much the last step where your code is still readable by a human.
  4. Assembly. Your program is assembled into the bits and bytes of object code suitable for your target platform.
  5. Linking. The object code is linked together with the object code of any library functions specified by your program and command line options, and an executable file is created.

查看英文版

查看中文版

cc 语法

cc [ options ]
cc [ options ]

查看英文版

查看中文版

cc 选项

实际上,您可以将数百个选项传递给编译器,以微调程序的执行方式。

在这里,我们选择了一些最有用且最常用的选项,您可能会需要这些选项来创建可执行文件。

-C

只需编译代码即可;不要尝试链接源文件。

将创建目标文件,可以选择稍后在一个单独的步骤中链接目标文件以创建可执行文件。

-O [level]

创建优化的可执行文件。

编译器将分析您的代码,如果知道任何巧妙的技巧可以提高性能,它将在字节码中实现它们。

水平是一个可选的数字,字母或文字可以指定多少的优化应该做的。

例如,GNU编译器集合允许选项-O0指定最小优化(默认值),

- O1和-O2指定中间优化级别,而-O3指定最大优化。

-Ofast可以用作-O3的别名,并且-Og将执行与调试器以后的分析兼容的优化。

优化需要更多的计算和内存,并且需要更长的时间才能完成。

x86系统的常见优化技巧包括减小指令大小以减少执行的计算量;

为值零实现专用寄存器(因为在大多数程序中通常使用零),

以及利用某些优势来优化某些乘法和数组索引缩放操作所述的LEA(“加载有效地址”)指令。

-Wall

编译时启用“所有”(除了最深奥的)警告。

这是经常使用的好选择,因为它可以提示您可能不是错误的任何情况,

但是您可能希望在编译之前解决这些情况。

-ansi

禁用cc提供的大多数(但不是全部)非ANSI C兼容功能。

要遵守严格的ANSI标准,请启用此选项。

这样做将确保您的代码具有完全可移植性,可以在任何其他系统上进行编译。

-G

创建已编译的可执行文件的调试版本。

编译器将在编译的可执行文件中包含有关哪个源文件的哪一行对应于哪个函数调用的信息。

当您逐步执行程序时,调试器以后可以使用此信息来显示源代码。

请注意,这将创建一个更大的可执行文件。

-D -ansi

通过将指定的分配给符号名称来定义编译时的宏。

运行C预处理程序时,它将把name的任何实例扩展为value的文本。

-Idir

搜索目录所包含的文件,其名称以斜线(不要开始/)之前搜索照常目录。

按照指定的顺序搜索多个-I选项的目录。

预处理程序首先在包含源文件的目录中搜索#include文件,然后在以-I选项命名的目录(如果有)中搜索/ usr / ucbinclude,然后在/ usr / include中搜索。

-Ldir

dir添加到编译器将在其中查找链接库的目录列表。

此选项传递给ld和/ usr / lib。

-llibrary

如果链接,则此选项将指示的库添加到要链接的库列表中。

例如,如果您使用的是C数学库(libm)中的函数,则可以在编译时使用-lm选项将其链接。

请注意您的库的相互依赖关系,并确保在命令行上以正确的顺序链接它们;

首先指定其他库将依赖的任何库。

例如,“ -lGL -lGLU ”将首先搜索libGL,然后搜索libGLU。

libGLU依赖于libGL,因此,如果您颠倒了这些-l选项的顺序,该程序将无法编译。

-M

如果链接,请创建一个与输出可执行文件具有相同基本名称的映射文件,但后缀为.map。

该映射文件包含符号列表及其地址。

-mmodel

指定编译器和链接器使用的内存模型。

model的值包括s代表“小”存储模型,m代表“中”,l代表“大”,f代表“ flat”(32位),c代表“ compact”,h代表“ huge”。

除非您确定需要此选项,否则可以忽略它。

-o file name

编译后的可执行文件的输出名称。

如果未指定文件名,则编译后的可执行文件将被命名为a.out。

-S

产生汇编程序源代码,但在实际汇编之前停止。

-Uname

“取消定义”任何先前定义的名为name的宏,无论是在源代码中还是在命令行中使用-D指定。

There are literally hundreds of options you can pass to the compiler to fine-tune how your program executes. Here we have selected some of the most useful and commonly-used options which you may need to create an executable file.

-c Just compile the code; do not attempt to link source files. Object files will be created, which can optionally be linked later in a separate step to create an executable file.
-O[level] Create an optimized executable. The compiler will analyze your code, and if it knows any clever tricks to speed up performance, it will implement them in the byte code. The level is an optional number, letter, or word which can specify how much optimization should be done. For instance, the GNU compiler collection allows for the options -O0 to specify minimum optimization (the default), -O1 and -O2 for intermediate levels of optimization, and -O3 for maximum optimization. -Ofast can be used as an alias for -O3, and -Og will perform optimizations that are compatible with later analysis by a debugger.Optimization requires more computation and memory, and will take longer to complete. Common optimization tricks for x86 systems include reducing instruction size to make execution less computationally expensive, implementing a dedicated register for the value zero (because zero is used so commonly in most programs), and optimizing certain multiplication and array index-scaling operations by taking advantage of the LEA ("load effective address") instruction.
-Wall Enable "all" (all but the most esoteric) warnings when compiling. This is a good option to use regularly, as it can cue you in to any conditions which may not be errors, but which you may nonetheless want to resolve before compiling.
-ansi Disable most (but not all) of the non-ANSI C compliant features provided by cc. To adhere to strict ANSI standards, enable this option. Doing so will ensure that your code is fully portable to be compiled on any other system.
-g Create a debugging version of the compiled executable file. The compiler will include information in the compiled executable about which line of which source file corresponds to which function call. A debugger can later use this information to show the source code as you step through the program. Note that this creates a much larger executable file.
-D name[=value] Defines a macro at compile time by assigning the specified value to the symbol name. When the C preprocessor is run, it will expand any instances of name to the text of value.
-Idir Search dir for included files whose names do not begin with a slash (/) prior to searching the usual directories. The directories for multiple -I options are searched in the order specified. The preprocessor first searches for #include files in the directory containing sourcefile, and then in directories named with -I options (if any), then /usr/ucbinclude, and finally, in /usr/include.
-Ldir Add dir to the list of directories where the compiler will look for linked libraries. This option is passed to ld and /usr/lib.
-llibrary If linking, this options adds the indicated library to the list of libraries to be linked. For instance, if you are using functions from the C math library (libm), you would link it at compile time using the option -lm.

Be aware of your libraries' inter-dependencies and make sure to link them in the correct order on the command line; specify first any libraries which other libraries will depend on. For instance, "-lGL -lGLU" will search libGL first and then libGLU. libGLU depends on libGL, so if you reversed the order of these -l options, the program would not compile.
-M If linking, creates a map file with the same base name as the output executable, but with the suffix .map. This map file contains a list of symbols with their addresses.
-mmodel Specifies the memory model that the compiler and linker use. Values of model include s for "small" memory model, m for "medium", l for "large", and f for "flat" (32-bit), c for "compact", or h for "huge". Unless you're sure you require this option, you may omit it.
-o file name The output name of the compiled executable file. If file name is not specified, the compiled executable will be named a.out.
-S Produces assembler source code, but stops before actually assembling.
-Uname "Undefine" any previously-defined macro named name, either in the source code or specified on the command line with -D.

查看英文版

查看中文版

cc 环境变量

以下环境变量会影响编译过程:

LANG
LC_CTYPE
LC_MESSAGES
LC_ALL

这些环境变量控制编译器使用本地化信息与不同的国家惯例一起工作的方式。

对于以UTF-8 编码的英语,通用值为“ en_GB.UTF-8 ” 。

如果设置,LC_ALL变量将覆盖所有其他语言环境设置。

要查看您当前的语言环境设置,请使用locale命令。

TMPDIR

如果设置了TMPDIR,它将指定用于临时文件的目录。

编译器使用临时文件保存一个编译阶段的输出,该输出将用作下一阶段的输入。

例如,预处理程序的输出(即实际编译器的输入)在编译过程中存储在临时文件中。

COMPILER_PATH

用冒号分隔的目录列表,非常类似于PATH变量。

如果编译器使用GCC_EXEC_PREFIX的值找不到子程序,则会在此目录列表中搜索子程序。

LIBRARY_PATH

用冒号分隔的目录列表,类似于PATH。

链接时,编译器将在此目录列表中搜索链接的库,并使用GCC_EXEC_PREFIX的值找不到特殊的链接器文件。

CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH

这些变量中的每一个都可能包含由特殊字符分隔的目录列表,就像PATH一样,编译器将在其中查找头文件。

特殊字符PATH_SEPARATOR取决于目标,并在构建时确定。对于大多数Linux目标,PATH_SEPARATOR是冒号。

CPATH指定要搜索仿佛用指定的目录列表-I选项,但给定任何路径后-I选项。

C_INCLUDE_PATH,CPLUS_INCLUDE_PATH和OBJC_INCLUDE_PATH仅在预处理指定的特定语言时适用。

每个命令都指定要搜索的目录列表,就像使用-isystem指定的一样,但是在命令行上使用-isystem选项指定的任何路径之后。

在所有这些变量中,一个空元素指示编译器搜索其当前工作目录。

空元素可以出现在路径的开头或结尾。例如,如果CPATH的值为:/ special / include,则其效果与“ -I。-I / special / include ”相同。

DEPENDENCIES_OUTPUT

如果设置了此变量,则其值指定如何基于编译器处理的非系统头文件输出make的依赖项。

系统头文件在依赖项输出中被忽略。

DEPENDENCIES_OUTPUT的值可以只是一个文件名,在这种情况下,将Make规则写入该文件,

并从源文件名中猜测目标名称。

或者该值可以采用“ file target ” 的形式,在这种情况下,使用target作为目标名称将规则写入文件file

The following environment variables affect the compilation process:

LANG
LC_CTYPE
LC_MESSAGES
LC_ALL

These environment variables control the way the compiler uses localization information to work with different national conventions. A common value is "en_GB.UTF-8" for English language encoded in UTF-8. If set, the LC_ALL variable overrides all other locale settings.

To view your current locale settings, use the locale command.

TMPDIR

If TMPDIR is set, it specifies the directory to use for temporary files. The compiler uses temp files to hold the output of one stage of compilation that is to be used as input to the next stage. For example, the output of the preprocessor, which is the input to the actual compiler, is stored in a temporary file during the compilation process.

COMPILER_PATH

A colon-separated list of directories, much like the PATH variable. The compiler will search this list of directories for subprograms if it can't find them using the value of GCC_EXEC_PREFIX.

LIBRARY_PATH

A colon-separated list of directories, much like PATH. When linking, the compiler will search this list of directories for linked libraries, and for special linker files it can't find using the value of GCC_EXEC_PREFIX.

CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH

Each of these variables may hold a list of directories separated by a special character, much like PATH, in which the compiler will look for header files. The special character, PATH_SEPARATOR, is target-dependent and determined at build time. For most Linux targets, PATH_SEPARATOR is a colon.

CPATH specifies a list of directories to be searched as if specified with the -I option, but after any paths given with the -I option.

C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, and OBJC_INCLUDE_PATH apply only when preprocessing the particular language indicated. Each specifies a list of directories to be searched as if specified with -isystem, but after any paths given with -isystem options on the command line.

In all these variables, an empty element instructs the compiler to search its current working directory. Empty elements can appear at the beginning or end of a path. For instance, if the value of CPATH is :/special/include, that has the same effect as "-I. -I/special/include".

DEPENDENCIES_OUTPUT

If this variable is set, its value specifies how to output dependencies for make based on the non-system header files processed by the compiler. System header files are ignored in the dependency output.

The value of DEPENDENCIES_OUTPUT can be just a file name, in which case the Make rules are written to that file, guessing the target name from the source file name. Or the value can have the form "file target", in which case the rules are written to file file using target as the target name.

查看英文版

查看中文版

cc 示例

cc myfile.c

编译文件myfile.c。输出将被写入可执行文件a.out。

cc myfile.c -o myexe

编译文件myfile.c并将其命名为已编译的可执行输出文件myexe。

cc myfile.c -Wall -o myexe

编译文件myfile.c并将已编译的可执行文件输出为myexe,如果发生则在编译期间显示警告。

cc myfile.c -Wall -lX11 -o myexe

编译myfile.c文件的可执行myexe,连接的libx11库和编译过程中发出任何警告适用。

cc myfile.c -Wall -ansi -lX11 -o myexe

编译myfile.c文件的可执行myexe,连接的libx11库,严格遵守ANSI C标准,如果适用发出警告。

cc myfile.c

Compile the file myfile.c. Output will be written to the executable file a.out.

cc myfile.c -o myexe

Compile the file myfile.c and name the compiled executable output file myexe.

cc myfile.c -Wall -o myexe

Compile the file myfile.c and output the compiled executable as myexe, displaying warnings during compilation if they occur.

cc myfile.c -Wall -lX11 -o myexe

Compile myfile.c to the executable myexe, linking the libX11 library and issuing any applicable warnings during compilation.

cc myfile.c -Wall -ansi -lX11 -o myexe

Compile myfile.c to the executable myexe, linking the libX11 library, adhering strictly to ANSI C standards, and issuing warnings if applicable.

查看英文版

查看中文版