gcc -o 将构建输出写入输出文件。
gcc -O 设置编译器的优化级别。
将构建输出写入输出文件。
$ gcc [options] [source files] [object files] -o output file
我的文件.c:
// myfile.c
#include <stdio.h>
void main()
{
printf("Program run\n");
}
在终端上构建myfile.c并运行输出文件myfile:
$ gcc myfile.c -o myfile
$ ./myfile
Program run
$
设置编译器的优化级别。
选项 | 优化级别 | 执行时间处理时间 | 代码大小 | 内存使用情况 | 编译时间 |
---|---|---|---|---|---|
-O0 | 编译时间优化(默认) | + | + | - | - |
-O1 或 -O | 优化代码大小和执行时间 | - | - | + | + |
-O2 | 针对代码大小和执行时间进行更多优化 | -- | + | ++ | |
-O3 | 针对代码大小和执行时间进行更多优化 | --- | + | +++ | |
-操作系统 | 优化代码大小 | -- | ++ | ||
-Ofast | O3 快速但不准确的数学计算 | --- | + | +++ |
+增加 ++增加更多 +++增加更多 -减少 --减少更多 ---减少更多
$ gcc -Olevel [options] [source files] [object files] [-o output file]
我的文件.c:
// myfile.c
#include <stdio.h>
void main()
{
printf("Program run\n");
}
在终端上构建myfile.c并运行输出文件myfile:
$ gcc -O myfile.c -o myfile
$ ./myfile
Program run
$