程序编译[c++ vs java] && 调试 2012-07-12 08:22:12| 分类: 默认分类 | 标签:编译 c++ java gdb |字号 订阅c++的编译过程c++的编译以cc或者cpp为单位1. 预处理gcc -E test.cc预处理阶段输入是代码的文本文件,.h 或者cc 或者 cpp预处理阶段输出是后缀为ii的文件预处理阶段进行 头文件的展开,宏的替换等2. 编译gcc -S test.cc编译阶段输入是后缀为ii的文件编译输出是.s文件编译阶段做的事情就是将 文本形式的程序 转化为 文本形式的汇编代码3. 汇编gcc -c test.cc汇编阶段输入是.s文件汇编阶段输出是.o文件汇编阶段做的事情就是将 文本形式的 汇编代码 转化为二进制形式4. 连接连接阶段输入是.o文件链接阶段输出时.so或者.a,或者是可执行程序链接阶段做的事情就是重新定位 编译单元中的 全局变量 和 函数编译示例test.cc#include int main(){ printf("hello,world\n"); return 0;}[liud@db-sf-rd00.db01.baidu.com myapp]$ g++ test.cc -o test -save-temps[liud@db-sf-rd00.db01.baidu.com myapp]$ ls -al总用量 52drwxrwxr-x 2 liud liud 4096 7月 12 22:18 .drwxrwxr-x 4 liud liud 4096 7月 12 22:09 ..-rwxrwxr-x 1 liud liud 7093 7月 12 22:18 test-rw-rw-r-- 1 liud liud 72 7月 12 22:10 test.cc-rw-rw-r-- 1 liud liud 20704 7月 12 22:18 test.ii-rw-rw-r-- 1 liud liud 1592 7月 12 22:18 test.o-rw-rw-r-- 1 liud liud 930 7月 12 22:18 test.s处理逻辑如下:test.cc---->预处理---->test.ii---->编译----->test.s----->汇编----->test.o(目标文件)----->链接----->test相关工具g++实质是个空壳,其调用了其他预处理,汇编,链接程序,如下:cc1plus: 预处理,编译as: 汇编collect2: 链接ar : 打包目标文件为静态链接库ld: 多个目标文件被链接为动态链接库 nm 功能:列出object目标文件的符号(symbole)For each symbol, nm shows:The symbol value,hexadecimal by default.he symbol type:"A" The symbol's value is absolute, and will not be changed by further linking."B" The symbol is in the uninitialized data section (known as BSS)."C" The symbol is common. Common symbols are uninitialized data. When linking, multiple common symbols may appear with the same name. If the symbol is defined anywhere, the common symbols are treated as undefined references."D" The symbol is in the initialized data section."G" The symbol is in an initialized data section for small objects. Some object file formats permit more efficient access to small data objects, such as a global int variable as opposed to a large global array. "I" The symbol is an indirect reference to another symbol. This is a GNU extension to the a.out object file format which is rarely used. "N" The symbol is a debugging symbol. "R" The symbol is in a read only data section. "S" The symbol is in an uninitialized data section for small objects."T" The symbol is in the text (code) section."U" The symbol is undefined. "V" The symbol is a weak object. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes zero with no error.makemake命令读取makefile的依赖进行编译http://www.chinaunix.net/old_jh/23/408225.htmlmake -C dir执行动作:首先cd到dir,读取makefile,执行编译动作,然后回到当前目录make规则target : prerequisitescommand如果一行太长,可以用\进行分割make支持三种通配符:* 和 ? 以及 [...]target : 【可以是label,可以是可执行文件,也可以是object文件等】prerequisites: 【产生target所需要的文件】command: 【任意的shell命令,必须以tab键开始】makefile中主要包含了五个东西:1. 显式规则2. 隐晦规则3. 变量定义4. 文件指示和注释5. 注释,makefile只有行注释,和unix的shell脚本一样,其注释用#字符自动变量:$@: 代表规则中的目标文件名$^ : 规则的所有依赖文件$<: 规则的第一个依赖文件$?: 依赖文件中比目标文件更新的文件列表如果我们要定义标签作为目标,因为标签不是一个文件,我们必须设定它是一个伪目标.PHONY : cleanclean: rm *.o *.tmpCFLAGS : 需要传递给c编译器的一系列开关参数CXXFLAGS : 需要传递给c++编译器的一系列开关参数cc is for compiling C files, while cxx is for compiling C++ filesg++ 编译选项-c : option says not to run the linker. Then the output consists of object files output by the assembler.完成预处理,编译,汇编步骤,输出.o文件,而不进行链接-pipe Use pipes rather than temporary files for communication between the various stages of compilation.使用Linux的pipe功能,将预处理,编译,汇编,链接,每一步骤的输出指向下一个步骤的输入,而不输出中间 文件-o file Place output in file file. This applies regardless to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code. 将输出放入文件中,这个选项不管输出的类型是什么,不管是可执行文件,目标文件,汇编文件,或者预处理文件-shared Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options that were used to generate code (-fpic, -fPIC, or model suboptions) when you specify this option.[1] 产生一个可以shared的目标文件,这个目标文件可以和其他的目标文件进行连接以形成可执行文件 -static On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect. 对于支持动态连接的系统,这个选项阻止和shared libraries进行连接,在其他系统上,这个选项没有效果 -fPIC If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on the m68k and the SPARC. 这个选项产生位置无关代码,适合于动态连接-Wall Turns on all optional warnings which are desirable for normal code.. 打开所有可选的警告-Werror Make all warnings into errors. 让所有的warning成为错误-g Produce debugging information in the operating system<80><99>s native format (stabs, COFF, XCOFF, o r DWARF). GDB can work with this debugging information. 产生debug信息,GDB可以利用这些debug信息 -finline-functions Integrate all simple functions into their callers. The compiler heuristically decides which func- tions are simple enough to be worth integrating in this way. If all calls to a given function are integrated, and the function is declared "static", then the function is normally not output as assembler code in its own right. Enabled at level -O3. 把所有简单的函数集成到调用者内,编译器启发性的决定哪些函数可以进行集成 -D name Predefine name as a macro, with definition 1. -D name=definition Predefine name as a macro, with definition definition. The contents of definition are tokenized and processed as if they appeared during translation phase three in a #define directive. In particular, the definition will be truncated by embedded newline characters. If you are invoking the preprocessor from a shell or shell-like program you may need to use the shell<80><99>s quoting syntax to protect characters such as spaces that have a meaning in the shell syntax. 预定义name为宏,如同#define宏一样-W 编译器的警告设置参数,拥有众多的选项,下面举一些常用的例子: -Woption 让编译器给出option指定的编译警告,常用的一些如下: unused-function: 遇到仅声明过但尚未定义的静态函数时发出警告。 unused-parameter: 从未用过的函数参数的警告。 unused-variable: 在本地声明但从未用过的变量的警告。 unused-value: 经计算但从未用过的值得警告。 return-type: 对函数返回类型不当的警告。 uninitialized:在初始化之前就使用自动变量。 float-equal: 比较两个浮点数是否相等。-pg 在程序中加入剖分信息给gprof使用(profiler) -I dir Add the directory dir to the list of directories to be searched for header files 将目录添加到头文件目录里列表中 -L. Normally the files found this way are library files---archive files whose members are object files 指明library file的路径 库的概念:库是为了方便服用代码二产生的,一堆cc产生了一段.o目标文件,如果管理这些目标文件,方便复用,把几个.o的文件打包到一起也就有了库,本质上来说,一个库是.o的集合 二进制在链接库的时候,会自动的从库的.o中查找函数定义,完成链接过程 静态库通常为.a后缀:静态库在链接的时候,会将二进制程序使用到的.o都包含进去 动态库为.so后缀:动态库在链接的时候,只会将二进制程序使用到的符号信息导入到了程序中,需要程序在运行的时候去动态加载,就是你通过nm看到的-lobjc You need this special case of the -l option in order to link an Objective-C program. 选项指明要连接的库的名字,不如库名为libmysqlclient.a ,则-lmysqlclient -Wl,option Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. 将option传递给linker,如果option包含commas,它将被拆分成多个选项 大家经常可以看到:-Wl,-e,so_main,相应的具体含义可以man ld来进行查询,比如这里的-e,指的是设置so的 入口为so_main c++ 调试[gdb]1. 添加源代码目录-d: gdb -d ./src,默认采用PATH环境变量(gdb) dir ~/src2. 调用函数(gdb) call func(10)3. 打印变量表达式print v1 + 14. 传递参数set args --defaults-file=../etc/my.cnf就这样直接写参数就好5. stack tracebt打印栈信息,有助于查看函数调用情况6. 绑定进程gdb --pid xxx7. 设置断点 b file.c:2228. 跳到其他栈去查看变量等frame 29. 查看当前源文件list10. 重复上面的命令 enter11. whatis查看变量类型12. 调试coregdb -c core xxx13. 显示宏info macro N注意在编译的时候,需要gcc -gdwarf-2 -g3 -o test_macro test_macro.c-gdwarf-2: 指的是debug的信息形式以dwarf-2的形式 g3: debug信息的level, g3包括一些多余的信息,这些信息有宏信息等如果不用g3这个参数,将无法使用info macro N来查看宏定义java命令执行 执行一个class: java [-options] class [args...]options:-cp 待续:
程序编译[c++ vs java
最新推荐文章于 2024-03-29 07:29:15 发布