[An Introduction to GCC 学习笔记] 04 Verbose选项、-c、链接次序

1 Verbose Compilation

  • The ‘-V’ option can be used to display detailed information about the exact sequence of commands used to compile and link a program.
    在这里插入图片描述
  • The output produced by ‘-v’ can be useful whenever there is a problem with the compilation process itself. It displays the full directory paths used to search for header files and libraries, the predefined preprocessor symbols, and the object files and libraries used for linking.

2 Compiling Files Independently(独立编译文件)

  • If a program is stored in a single file then any change to an individual function requires the whole program to be recompiled to produce a new executable. The recompilation of large source files can be very time-consuming.
  • When programs are stored in independent source files, only the files which have changed need to be recompiled after the source code has been modified. In this approach, the source files are compiled separately and then linked together - a two stage process.
  • In the first stage, a file is complied without creating an executable. The result is referred to as an object file, and has the extension ‘,o’ when using GCC.
  • In the second stage, the object files are merged together by a separate program called the linker. The linker combines all the object files together to create a single executable.
  • An object file contains machine code where any references to the memory addresses of functions(or variables) in other files are left undefined. This allows source files to be compiled without direct reference to each other. The linker fills in these missing address when it produces the executable.

3 Creating Object Files From Source Files

  • The command-line option ‘-c’ is used to compile a source file to an object file.
$ gcc -Wall -c main
  • There is no need to use the option ‘-o’ to specify the name of the output file in this case. When compiling with ‘-c’ the compiler automatically creates an objct file whose name the same as the source file, with ‘.o’ instead of the original extension.
  • There is no need to put the header file on the command line, since it is automatically included by the #include statements in the souce files.

4 Creating Executables From Object Files

  • The final step in creating an executable file is to use gcc to link the object files together and fill the missing addresses of external functions. To link object files together, they are simply listed on the command line:
$ gcc main.o hello_fn.o -o hello
  • To perform the linking step, gcc uses the link ld, which is a separate program.

5 Link Order of Object Files

  • On Unix-like systems, the traditional behavior of compilers and linkers is to search for external functions from left to right in the object files specified on the command line. This means that the object file which contains the definition of a function should appear after any files which call that function.
  • Most current compilers and linkers will search all object files, regardless of order, but since not all compilers do this it is best to follow the convention of ordering object files from left to right.
  • This is worth keeping in mind if you ever encounter unexpected problems with undefined references, and all the necessary object files appear to be present on the command line.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值