博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
15个常用GCC命令
阅读量:4481 次
发布时间:2019-06-08

本文共 7034 字,大约阅读时间需要 23 分钟。

GCC编译器非常强大 ,在各个发行的系统中都非常流行,本文介绍的是一些常用的gcc编译选项

 

下面这段代码将回围绕整个文章:

编辑main.c如下.

 

[cpp]
 
  1. #include<stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.    printf("\n The Geek Stuff\n");  
  6.    return 0;  
  7. }  
#include
int main(void){ printf("\n The Geek Stuff\n"); return 0;}

GCC编译选项

1.指定输出可执行文件的名字

使用最基本的gcc编译格式

 

[cpp]
 
  1. gcc main.c  
gcc main.c

执行完上面这句命令,会在当前目录下输出一个名为a.out的可执行文件。

 

使用 -o选项可以指定输出的可执行文件名称。

 

[cpp]
 
  1. gcc main.c -o main  
gcc main.c -o main

执行完上面语句会在当前目录下输出一个名为main的可执行文件。

 

要想知道gcc编译器编译执行的过程请参考下面这个链接 

2.让所有编译警告都显示出来

编辑一段带警告的代码如下:

 

[cpp]
 
  1. <span style="font-size:14px;">#include<stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.    int i;  
  6.    printf("\n The Geek Stuff [%d]\n", i);  
  7.    return 0;  
  8. }</span>  
#include
int main(void){ int i; printf("\n The Geek Stuff [%d]\n", i); return 0;}
[cpp]
 
  1. $ gcc -Wall main.c -o main  
  2. main.c: In function â€˜main’:  
  3. main.c:6:10: warning: â€˜i’ is used uninitialized in this function [-Wuninitialized]  
$ gcc -Wall main.c -o mainmain.c: In function ‘main’:main.c:6:10: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]

执行gcc -Wall main.c -o main 会得到未初始化变量i的警告.

3.指定 -E编译选项,使得只输出预编译结果

 

[cpp]
 
  1. $ gcc -E main.c > main.i  
$ gcc -E main.c > main.i

上面这条gcc 编译命令会将输出重定向到输出文件当中。上面的例子中,main.i文件中的内容就是执行-E选项gcc命令的结果。

 

4.通过编译选项 -S 输出汇编代码

 

[cpp]
 
  1. gcc -S main.c > main.s  
gcc -S main.c > main.s

main.s 会包含main.c的汇编输出代码

 

5.指定-C 输出编译后的代码

 

[cpp]
 
  1. gcc -C main.c  
gcc -C main.c

执行上面这条命令会输出main.o文件,包含机器指令代码或者编译后的代码。

 

6.通过编译选项-save-temps 输出所有的中间代码。

[cpp]
 
  1. $ gcc -save-temps main.c  
  2.   
  3. $ ls  
  4. a.out  main.c  main.i  main.o  main.s  
$ gcc -save-temps main.c$ lsa.out  main.c  main.i  main.o  main.s

7.链接共享库(动态链接库)指定编译选项 -l

 

[cpp]
 
  1. gcc  -Wall main.c -o main -lCPPfile  
gcc  -Wall main.c -o main -lCPPfile

gcc命令指出再执行链接main.c 代码时,会链接上-lCPPfile.so动态链接库来完成生成main可执行文件。

 

8.指定编译选项-fPIC 创建独立的(无关联的)地址信息代码。

当创建动态链接库时,独立位置信息(position independent)代码也需要生成。这可以帮助动态链接库或者跟多的加载地址信息来替代其他相对的地址信息。所以-fPIC这个选项作用很大,能快速准确定位错误地址。

下面是一个例子,

 

[cpp]
 
  1. $ gcc -c -Wall -Werror -fPIC Cfile.c  
  2. $ gcc -shared -o libCfile.so Cfile.o  
$ gcc -c -Wall -Werror -fPIC Cfile.c$ gcc -shared -o libCfile.so Cfile.o

9.打印输出有个执行过程的信息 使用-V选项

 

当编译源文件时,-V选项会显示所有编译步骤的调试信息。

例子:

 

[cpp]
 
  1. $ gcc -Wall -v main.c -o main  
  2. Using built-in specs.  
  3. COLLECT_GCC=gcc  
  4. COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper  
  5. Target: i686-linux-gnu  
  6. Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu  
  7. Thread model: posix  
  8. gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)  
  9. ...  
  10. ...  
  11. ...  
$ gcc -Wall -v main.c -o mainUsing built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapperTarget: i686-linux-gnuConfigured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnuThread model: posixgcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5).........

10.指定编译选项-ansi,支持ISO C89 programs.

 

通过-ansi选项开启支ISO C89风格.

看如下代码:

 

[cpp]
 
  1. #include<stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.   // Print the string  
  6.    printf("\n The Geek Stuff\n");  
  7.    return 0;  
  8. }  
#include
int main(void){ // Print the string printf("\n The Geek Stuff\n"); return 0;}

执行上面代码附加-ansi编译选项,编译器会输出错误因为c++ 不支持ISO C89风格。

 

 

[cpp]
 
  1. $ gcc -Wall -ansi main.c -o main  
  2. main.c: In function â€˜main’:  
  3. main.c:5:3: error: expected expression before â€˜/’ token  
$ gcc -Wall -ansi main.c -o mainmain.c: In function ‘main’:main.c:5:3: error: expected expression before ‘/’ token

gcc 会抛出上面错误信息。

 

11.指定编译选项 -funsigned-char选项将char类型解释为unsigned char类型。

通过这个编译选项,char 类型会被认为是unsigned char.

例子:

 

[cpp]
 
  1. #include<stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.   char c = -10;  
  6.   // Print the string  
  7.    printf("\n The Geek Stuff [%d]\n", c);  
  8.    return 0;  
  9. }  
#include
int main(void){ char c = -10; // Print the string printf("\n The Geek Stuff [%d]\n", c); return 0;}

执行上面代码输出:

 

 

[cpp]
 
  1. $ gcc -Wall -funsigned-char main.c -o main  
  2. $ ./main  
  3.   
  4.  The Geek Stuff [246]  
$ gcc -Wall -funsigned-char main.c -o main$ ./main The Geek Stuff [246]

12.指定编译选项 -fsigned-char选项将unsigned char类型解释为 char类型。

[cpp]
 
  1. $ gcc -Wall -fsigned-char main.c -o main  
  2. $ ./main  
  3.   
  4.  The Geek Stuff [-10]  
$ gcc -Wall -fsigned-char main.c -o main$ ./main The Geek Stuff [-10]

13.指定-D选项 开启编译时的宏

例子如下:

 

[cpp]
 
  1. #include<stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5. #ifdef MY_MACRO  
  6.   printf("\n Macro defined \n");  
  7. #endif  
  8.   char c = -10;  
  9.   // Print the string  
  10.    printf("\n The Geek Stuff [%d]\n", c);  
  11.    return 0;  
  12. }  
#include
int main(void){#ifdef MY_MACRO printf("\n Macro defined \n");#endif char c = -10; // Print the string printf("\n The Geek Stuff [%d]\n", c); return 0;}

通过编译选项 可以直接定义宏

 

 

[cpp]
 
  1. $ gcc -Wall -DMY_MACRO main.c -o main  
  2. $ ./main  
  3.   
  4.  Macro defined   
  5.   
  6.  The Geek Stuff [-10]  
$ gcc -Wall -DMY_MACRO main.c -o main$ ./main Macro defined  The Geek Stuff [-10]

14.将编译警告转换成错误.

 

编译警告很多时候会被我们忽视,在特殊场合我们还是需要重视编译警告,如果能把编译警告变长直接输出错误,那我们的重视程度会提高很多并去解决。

example:

 

[cpp]
 
  1. #include<stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.   char c;  
  6.   // Print the string  
  7.    printf("\n The Geek Stuff [%d]\n", c);  
  8.    return 0;  
  9. }  
#include
int main(void){ char c; // Print the string printf("\n The Geek Stuff [%d]\n", c); return 0;}

 

[cpp]
 
  1. $ gcc -Wall -Werror main.c -o main  
  2. main.c: In function â€˜main’:  
  3. main.c:7:10: error: â€˜c’ is used uninitialized in this function [-Werror=uninitialized]  
  4. cc1: all warnings being treated as errors  
$ gcc -Wall -Werror main.c -o mainmain.c: In function ‘main’:main.c:7:10: error: ‘c’ is used uninitialized in this function [-Werror=uninitialized]cc1: all warnings being treated as errors

上述代码未初始化变量c,警告变成了错误提示.

 

15.通过文件指定编译选项,指定@编译选项

比较神奇的功能。可以使用@编译选项然后跟着文件名,一个以上的编译选项用空格 隔开。

example:

 

[cpp]
 
  1. $ cat opt_file  
  2. -Wall -omain  
$ cat opt_file-Wall -omain

 

[cpp]
 
    1. $ gcc main.c @opt_file  
    2. main.c: In function ‘main’:  
    3. main.c:6:11: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]  
    4.   
    5. $ ls main  
    6. main  

转载于:https://www.cnblogs.com/dpf-learn/p/6127662.html

你可能感兴趣的文章
Android无法删除项目+导入项目报错
查看>>
poj 2349(最小生成树应用)
查看>>
python接口自动化测试二十五:执行所有用例,并生成HTML测试报告
查看>>
c# 指定的存储区提供程序在配置中找不到,或者无效
查看>>
最简陋的python数据
查看>>
第一堂java web课
查看>>
操作系统简介
查看>>
第1周小组博客作业--1703班06组
查看>>
vue项目中icon图标的完美引入
查看>>
C语言指针
查看>>
Java的安装
查看>>
0920 JSON数据 蓝懿
查看>>
Azure Cosmos DB 使用费用参考
查看>>
【嵌入式开发】写入开发板Linux系统-模型S3C6410
查看>>
C# 子线程与主线程通讯方法一
查看>>
006——修改tomacat的编码
查看>>
《C程序设计语言》笔记 (八) UNIX系统接口
查看>>
git常用命令
查看>>
Android必知必会-获取视频文件的截图、缩略图
查看>>
(转)理解Bitblt、StretchBlt与SetDIBitsToDevice、StretchDibits
查看>>