当前位置: 首页 >> 程序设计 >> An Introduction to GCC 学习笔记:
 

An Introduction to GCC 学习笔记:

作者:吴学军 2004-8      来源:本站原创     发表时间:2006-03-11     浏览次数:      字号:    

内容摘要 注:转载时请注明出处和作者(吴学军 wu_xuejun@hotmail.com) 来源于GNU文档学习手册

vides maximum optimization
without increasing the executable size. It is the default
optimization level for releases of GNU packages.

‘-O3’ This option turns on more expensive optimizations, such as function
inlining, in addition to all the optimizations of the lower levels
‘-O2’ and ‘-O1’. The ‘-O3’ optimization level may increase the speed
of the resulting executable, but can also increase its size. Under
some circumstances where these optimizations are not favorable,
this option might actually make a program slower.
‘-funroll-loops’
This option turns on loop-unrolling, and is independent of the other
optimization options. It will increase the size of an executable.
Whether or not this option produces a beneficial result has to be
examined on a case-by-case basis.
‘-Os’ This option selects optimizations which reduce the size of an executable.
The aim of this option is to produce the smallest possible
executable, for systems constrained by memory or disk space. In
some cases a smaller executable will also run faster, due to better
cache usage.
 
注:For most purposes it is satisfactory to use ‘-O0’ for debugging, and
 ‘-O2’ for development and deployment.
 
“optimizations may not necessarily make a program faster in every case.”
 
第七章:编译C++程序
GCC是一个真正的C++编译器,其直接将C++代码编译为汇编代码。
 
1。利用g++而不是gcc命令;
(注:C++ source code should be given one of the valid C++ file extensions ‘.cc’,
‘.cpp’, ‘.cxx’ or ‘.C’ rather than the ‘.c’ extension used for C programs.)
 
2。C++标准库模板
The C++ standard library ‘libstdc++’ supplied with GCC provides a wide
range of generic container classes such as lists and queues, in addition to
generic algorithms such as sorting。
 
3。Explicit template instantiation
To achieve complete control over the compilation of templates with g++
it is possible to require explicit instantiation of each occurrence of a template,
using the option ‘-fno-implicit-templates’.
 
第八章:平台相关的选项
利用 -m 选项来确定不同的开发平台
 
 
获得GCC的帮助文档:
$ gcc -v --help
$ gcc -v --help 2>&1 | more
获得GCC的版本号:
$ gcc --version
(注:The ‘-v’ option can also be used to display detailed information about the
exact sequence of commands used to compile and link a program)
 
第十章:编译器相关的工具
 
1。 ar工具 Creating a library with the GNU archiver
The GNU archiver ar combines a collection of object files into a single
archive file, also known as a library.
ar工具可以将要发布的多个.o文件组装成一个.a的库文件
例如:将hello_fn.o和bye_fn.o生成 libhello.a的命令行:
$ ar cr libhello.a hello_fn.o bye_fn.o
其中The option ‘cr’ stands for “create and replace”.
 
利用 选项 t 可以查看.a文件中所包含的所有.o的信息
$ ar t libhello.a
hello_fn.o
bye_fn.o
 
2。程序性能测试工具-gprof
To use profiling, the program must be compiled and linked with the ‘-pg’
profiling option:
$ gcc -Wall -c -pg collatz.c
$ gcc -Wall -pg collatz.o
编译和链接的时候添加选项 -pg ,才能使用gprof测试程序性能
 
然后运行编译通过的程序,才能产生gprof需要的文件 gmon.out(在当前目录下)
然后执行:
$ gprof a.out (假设可执行文件是缺省生成的)
 
3。程序覆盖测试工具- gcov
The GNU coverage testing tool gcov analyses the number of times each
line of a program is executed during a run. This makes it possible to
find areas of the code which are not used, or which are not exercised
in testing.
When combined with profiling information from gprof the
information from coverage testing allows efforts to speed up a program to
be concentrated on specific lines of the source code.
 
编译和链接必须使用相关选项,才可以使用gcov工具。
例如:
$ gcc -Wall -fprofile-arcs -ftest-coverage cov.c
其中
‘-ftest-coverage’ adds instructions for counting the number of times
                    individual lines are executed,
‘-fprofile-arcs’ incorporates instrumentation code for each branch of
                   the program. Branch instrumentation records how frequently
                   different paths are taken through‘if’ statements and
                   other conditionals.
运行通过编译的程序,产生供gcov使用的文件:
分别带有后缀:‘.bb’‘.bbg’ and ‘.da’在当前目录下
然后运行
$ gcov cov.c (注意:是源代码文件
这样会产生一个带有标注的源文件的副本文件,后缀为 .gcov
在该文件中,标注了每一行代码的执行次数,标注为‘######’的语句为
未被执行到的语句。
可以通过命令:
grep ’######’ *.gcov 查找到所有未被执行到的语句
 
第十一章:编译器工作过程
 
Compilation is a multi-stage process involving several
tools, including the GNU Compiler itself (through the gcc or g++ frontends),
the GNU Assembler as, and the GNU Linker ld. The complete
set of tools used in the compilation process is referred to as a toolchain.
 
toolchain的定义见此:)
 
调用GCC的内部过程如下:
• preprocessing (to expand macros)                    预处理
• compilation (from source code to assembly language) 编译
• assembly (from assembly language to machine code)   汇编
• linking (to create the final executable)            链接
 
详细描述如下:
1。preprocessing:
举例:第一步GCC会调用如下命令
 
$ cpp hello.c > hello.i
 
生成中间文件 hello.i(如果是C++文件的话,就是 hello.ii)
这种中间文件不会保存到磁盘上的,除非添加了编译选项-save-temps
 
2。compilation:
第二步:调用gcc(或者g++)生成汇编文件而不是.o文件(使用了-S选项)
 
$ gcc -Wall -S hello.i
 
这样就生成了基于x86 CPU的汇编文件 hello.s
 
3.assembly:
The purpose of the assembler is to convert assembly language into machine
code and generate an object file. When there are calls to external
functions in the assembly source file, the assembler leaves the addresses
of the external functions undefined, to be filled in later by the linker.
第三步调用
 
$ as hello.s -o hello.o
 
4。linking:
The final stage of compilation is the linking of object files to create an
executable. In practice, an executable requires many external functions
from system and C run-time (crt) libraries. Consequently, the actual link
commands used internally by GCC are complicated.
 
第四步调用
$ gcc hello.o
 
其实在GCC内部的这一步调用如下:
$ ld -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o
/usr/lib/crti.o /usr/lib/gcc-lib/i686/3.3.1/crtbegin.o
-L/usr/lib/gcc-lib/i686/3.3.1 hello.o -lgcc -lgcc_eh
-lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i686/3.3.1/crtend.o
/usr/lib/crtn.o(非常复杂)
 
第十二章:测试编译后文件的工具
 
1。Identifying files 鉴别文件 - file 命令
举例:
$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386,
version 1 (SYSV), dynamically linked (uses shared
libs), not stripped
 
最后一个not stripped表示该文件含符号表(可以利用命令strip去除符号表
 
2。查看符号表工具 - nm
举例:
$ nm a.out
08048334 t Letext
08049498 ? _DYNAMIC
08049570 ? _GLOBAL_OFFSET_TABLE_
........
080483f0 T main
08049590 b object.11
0804948c d p.3
U printf@GLIBC_2.0
 
其中: T表示该函数在此文件中有定义
       U表示未定义的函数(需要link的外部函数)
所以,nm最常用的地方在于,查看这个文件中是否包含某函数的定义
 
3。寻找所需的动态链接库工具 - ldd
 
例如:
$ gcc -Wall hello.c
$ ldd a.out
libc.so.6 => /lib/libc.so.6 (0x40020000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
 
给命令同样可以用于动态链接库本身,来查找其所需要的链接库
 
-- End --
 
(坚持不懈地往一个方向前进,不要浮躁的四处张望!)Go!!!

[1] [2] [3] [4]

编辑 webmaster

发表评论  打印本文  推荐本文  加入收藏  返回顶部  关闭窗口
 
 
 
评论更多>>
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •