Gcc
1. https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/
https://gcc.gnu.org/onlinedocs/
http://www.bnikolic.co.uk/blog/
http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/book1.html
2. 动态库
a) -shared, 指定编译动态库
b) -fPIC, -fpic: 生成位置无关的代码(position-independentcode). 在有些目标机器上-fpic会失败,这时改用-fPIC. -fpic为了节约内存,在GOT里面预留了“短”长度(跳转长度)。而-fPIC则采用了更大的跳转项。
c) –Werror
d) –Wall
e) 程序运行时动态库搜索路径(优先级也是按照下面的顺序)
i. ELF可执行文件中动态段中DT_RPATH所指定的路径。即在编译目标代码时, 对gcc加入链接参数“-Wl,-rpath”指定动态库搜索路径(指的是用-wl,rpath或-R选项)
example:
gcc-Wl,-rpath,/home/arc/test,-rpath,/lib/,-rpath,/usr/lib/,-rpath,/usr/local/libtest.c
ii. DT_RUNPATH指定路径
iii. Man ld.so
1. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径
2. 环境变量LD_PRELOAD指定先加载的动态库
3. 环境变量LD_DEBUG作debug
a) #LD_DEBUG=libs ls
b) #LD_DEBUG=all ls
Valid options for the LD_DEBUG environment variable are:
libs display library search paths
reloc display relocation processing
files display progress for input file
symbols display symbol table processing
bindings display information about symbol binding
versions display version dependencies
all all previous options combined
statistics display relocation statistics
unused determined unused DSOs
help display this help message and exit
|
iv. /etc/ld.so.cache中所缓存的动态库路径,这个可以通过先修改配置文件/etc/ld.so.conf中指定的动态库搜索路径,然后执行ldconfig命令来改变。
v. 系统默认的库目录 /lib
vi. 系统默认的库目录 /usr/lib
3. 静态库
a) 创建静态库的命令: ar, ranlib
b) After an archive is created, ormodified, there is a need to index it. This index is later used by the compilerto speed up symbol-lookup inside the library, and to make sure that the orderof the symbols in the library won't matter during compilation (this will bebetter understood when we take a deeper look at the link process at the end ofthis tutorial). The command used to create or update the index is called 'ranlib'.On some systems, the archiver (which is not always ar) already takes careof the index, so ranlib is not needed. (Usage: ranlib libz.a)
c) ar -csr
4. 连接的顺序
a) The linker searches andprocesses libraries and object files in the order they are specified. Thus,`foo.o -lz bar.o' searches library `z' after file foo.o butbefore bar.o. If bar.o refers to functions in `z', thosefunctions may not be loaded.
5. Gcc编译选项
a) 输出头文件包含关系(-H): gcc –H
b) 不链接标准的libc: -nostdlib
c) 打开所有的warnings: -Wall
d) 优化选项: -Ox, x为级别, 可以通过命令查看每个级别优化的内容
e) -isystem: Search dir for headerfiles, after all directories specified by -I but before the standard systemdirectories.
6. Some other useful commands:
a) strings: print the strings ofprintable characters in files.
b) nm: list symbols from objectfiles
7. 链接器版本控制脚本
#gcc –g –shared –o vis.so vis_comm.o vis_f1.o vis_f2.o -W1,--version-script,vis.map
a) 使用版本脚步控制符号的可见性
#cat vis.map
VER_1 {
global:
vis_f1;
vis_f2;
local:
*;
};
vis_f1, vis_f2对库外可见;
vis_common和其它函数对库外不可见;
b) 符号版本化
符号版本化允许一个共享库提供一个库的多个版本。
Gdb
快捷键
1. ctrl+x a
2. ctrl+x 2
core文件的产生
1. 使用’ulimit –c’控制是否产生core文件
2. /proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展
3. /proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名
调试多线程
a) info threand: 查看线程数
b) thread <thread-id>:指定gdb当前所在哪个线程
c) set scheduler-locking on/off:暂停/启动某个线程的运行
d) break in one thread:
break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]
调试多进程
e) Attach 某个进程
f) set follow-fork-mode
指定GDB收到信号后的处理
call 直接调用和执行一个函数
Strace
ltrace
Makefile
Oprofile
Valgrind
内存泄露检查,选项–tool=memcheck, 其它选项--leak-check=full
Valgrind包括如下一些工具:
Memcheck。这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。这也是本文将重点介绍的部分。Callgrind。它主要用来检查程序中函数调用过程中出现的问题。Cachegrind。它主要用来检查程序中缓存使用出现的问题。Helgrind。它主要用来检查多线程程序中出现的竞争问题。Massif。它主要用来检查程序中堆栈使用中出现的问题。Extension。可以利用core提供的功能,自己编写特定的内存调试工具。
Memwatch
Mtrace
Gcov
Gcov是进行代码运行的覆盖率统计的工具,它随着gcc的发布一起发布的,它的使用也很简单,需要在编译和链接的时候加上-fprofile-arcs -ftest-coverage生成二进制文件,gcov主要使用.gcno和.gcda两个文件,.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息。gcda文件的生成需要先执行可执行文件才能生成。生成gcda文件之后执行命令gcov *.cpp就会在屏幕上打印出测试的覆盖率,并同时生成文件“*cpp.gcov”,然后用vi打开就可以看见哪行被覆盖掉了。
Gprof
GNU gprof是一款linux平台上的程序分析软件(unix也有prof)。借助gprof可以获得C程序运行期间的统计数据,例如每个函数耗费的时间,函数被调用的次数以及各个函数相互之间的调用关系。gprof可以帮助我们找到程序运行的瓶颈,对占据大量CPU时间的函数进行调优(gprof统计的只是CPU的占用时间,对I/O瓶颈貌似无能为力,耗时甚久的I/O操作很可能只占据极少的CPU时间).
gprof的使用非常简单,在编译链接的时候加上"-pg"选项,然后按照正常方式运行程序,如果程序正常退出,一个名为gmon.out将会产生。使用gprof可查看gmon.out中的统计结果.
http://blog.sina.com.cn/s/blog_6608391701013phr.html
http://blog.youkuaiyun.com/linquidx/article/details/5916701