新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > gcc的几个妙用

gcc的几个妙用

作者: 时间:2016-12-01 来源:网络 收藏
gcc的学习在C接触到linux以后就开始不断的学习,也知道了一些基本的用法,但是关于gcc的使用还是有很多值得我们加深的地方。gcc 只是一个编译工具而已。也就相当于我们在windows环境下的visual c++等一样,区别是visual c++是基于IDE的,而gcc是这些IDE的基础。学习linux程序设计必然会学习gcc。
gcc实质是完成程序的编译和链接,程序的编译是指从一种文件类型转换到另一种文件类型的过程。一个C语言程序转换为可执行程序的基本步骤如下:
1、编写程序(vi,emacs等软件)
2、程序预编译(cpp)
3、编译成汇编程序(cc)
4、汇编程序(as)
5、链接程序(ld)
其中的这些过程都已经被gcc包含,我们在实际的编译过程中采用了gcc main.c -o main.exe即可实现一个程序的编译和链接。并不需要一步一步的实现,但是我们在分析的过程中又必须注意一个C语言文件的处理过程以及相应的处理程序。
关于gcc的基本含义用法就不再详细的说明了,我觉得最简单的使用方法是通过软件的help学习软件。
[gong@Gong-Computer test]$ gcc --help
Usage: gcc [options] file...
Options:
-pass-exit-codes Exit with highest error code from a phase
--help Display this information
--target-help Display target specific command line options
--help={target|optimizers|warnings|params|[^]{joined|separate|undocumented}}[,...]
Display specific types of command line options
(Use -v --help to display command line options of sub-processes)
--version Display compiler version information
-dumpspecs Display all of the built in spec strings
-dumpversion Display the version of the compiler
-dumpmachine Display the compilers target processor
-print-search-dirs Display the directories in the compilers search path
-print-libgcc-file-name Display the name of the compilers companion library
-print-file-name= Display the full path to library
-print-prog-name= Display the full path to compiler component
-print-multi-directory Display the root directory for versions of libgcc
-print-multi-lib Display the mapping between command line options and
multiple library search directories
-print-multi-os-directory Display the relative path to OS libraries
-print-sysroot Display the target libraries directory
-print-sysroot-headers-suffix Display the sysroot suffix used to find headers
-Wa, Pass comma-separated on to the assembler
-Wp, Pass comma-separated on to the preprocessor
-Wl, Pass comma-separated on to the linker
-Xassembler Pass on to the assembler
-Xpreprocessor Pass on to the preprocessor
-Xlinker Pass on to the linker
-combine Pass multiple source files to compiler at once
-save-temps Do not delete intermediate files
-save-temps= Do not delete intermediate files
-no-canonical-prefixes Do not canonicalize paths when building relative
prefixes to other gcc components
-pipe Use pipes rather than intermediate files
-time Time the execution of each subprocess
-specs= Override built-in specs with the contents of
-std= Assume that the input sources are for
--sysroot= Use as the root directory for headers
and libraries
-B Add to the compilers search paths
-b Run gcc for target , if installed
-V Run gcc version number , if installed
-v Display the programs invoked by the compiler
-### Like -v but options quoted and commands not executed
-E Preprocess only; do not compile, assemble or link
-S Compile only; do not assemble or link
-c Compile and assemble, but do not link
-o Place the output into
-x Specify the language of the following input files
Permissible languages include: c c++ assembler none
none means revert to the default behavior of
guessing the language based on the files extension
Options starting with -g, -f, -m, -O, -W, or --param are automatically
passed on to the various sub-processes invoked by gcc. In order to pass
other options on to these processes the -W options must be used.
For bug reporting instructions, please see:
.
从上面的结果可以知道基本的用法。
但是还是有几个需要注意的地方,这也是我们学习gcc时不经常使用,但又非常有用的几个用法。
1、采用gcc实现预编译,预编译可以实现代码的检查,特别是宏定义的检查,通过预编译检查实际的代码是否出错,这是非常有用的检查方式。
由于预编译以后宏定义被扩展了,这时对源码的分析就能找出代码宏定义等是否存在错误,特别时一些不容易发现的错误。
基本的实现形式为:gcc -E file.c > file.pre.c
[gong@Gong-Computer Example]$ vi main.c
采用重定向的方式改变输出流,便于检查错误所在。
[gong@Gong-Computer Example]$ gcc -E main.c > main.pre.c
[gong@Gong-Computer Example]$ vi main.pre.c
从上面的结果可以发现我们的宏已经实现了扩展。通过分析宏的扩展可以分析代码是否正确。
上一页 1 2 3 下一页

关键词: gcclinux编译工

评论


技术专区

关闭