15个常用GCC命令

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

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

编辑main.c如下.

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

int main(void)
{
   printf("
 The Geek Stuff
");
   return 0;
}

GCC编译选项

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

使用最基本的gcc编译格式

  1. gcc main.c  
gcc main.c

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

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

  1. gcc main.c -o main  
gcc main.c -o main


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

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

http://www.thegeekstuff.com/2011/10/c-program-to-an-executable/

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

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

  1. <span style="font-size:14px;">#include<stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.    int i;  
  6.    printf("  The Geek Stuff [%d] ", i);  
  7.    return 0;  
  8. }</span>  
#include<stdio.h>

int main(void)
{
   int i;
   printf("
 The Geek Stuff [%d]
", i);
   return 0;
}
  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 main
main.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编译选项,使得只输出预编译结果

  1. $ gcc -E main.c > main.i  
$ gcc -E main.c > main.i


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

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

  1. gcc -S main.c > main.s  
gcc -S main.c > main.s


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

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

  1. gcc -C main.c  
gcc -C main.c


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

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

  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

$ ls
a.out  main.c  main.i  main.o  main.s

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

  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这个选项作用很大,能快速准确定位错误地址。

下面是一个例子,

  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选项会显示所有编译步骤的调试信息。

例子:

  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 main
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Target: i686-linux-gnu
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
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
...
...
...


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

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

看如下代码:

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

int main(void)
{
  // Print the string
   printf("
 The Geek Stuff
");
   return 0;
}


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

  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 main
main.c: In function ‘main’:
main.c:5:3: error: expected expression before ‘/’ token

gcc 会抛出上面错误信息。

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

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

例子:

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

int main(void)
{
  char c = -10;
  // Print the string
   printf("
 The Geek Stuff [%d]
", c);
   return 0;
}


执行上面代码输出:

  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类型。

  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选项 开启编译时的宏

例子如下:

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

int main(void)
{
#ifdef MY_MACRO
  printf("
 Macro defined 
");
#endif
  char c = -10;
  // Print the string
   printf("
 The Geek Stuff [%d]
", c);
   return 0;
}


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

  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:

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

int main(void)
{
  char c;
  // Print the string
   printf("
 The Geek Stuff [%d]
", c);
   return 0;
}
  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 main
main.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:

  1. $ cat opt_file  
  2. -Wall -omain  
$ cat opt_file
-Wall -omain
    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