005_wz_bbk_-v详细信息,-c生成中间文件,链接次序

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.
-v显示详细信息
  • The output produced by '-v' can be useful whenever there is a problem with the compliation process ifself.It displays the full directory paths used to search for header files and libraries,the predefined preprocessor symbols,and the object filesand libraries used for linking.

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 recomplied 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 file 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 funtions(or variables)in other files are left undefined.This allows source files to be compiled without direct reference to eath other.The linker fills in these missing address when it produces the executable.

Creating Object Files From Source Files

  • The command-line option '-c' is used to compile a souce file to an object file.
gcc -Wall -c main.c
  • 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 object file whose name is 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 automactically included bu the #include statements in the souce files.
-c产生中间文件.o
  • The final step in creating an executable file is to use gcc to link the object files together and fill the missing addresses of wxternal functions.To link object files together,they are simply listed on the command line:
gcc main.o hello.o -o hello
  • To perform the linking step,gcc uses the linker ld,which is a separate program.
-o产生可执行文件

Link Order of Object Files

  • On Unix-like systems,the traditional behavior of compliers and linkers is to search for external functions froms 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 od 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.

2020.11.5

你可能感兴趣的:(005_wz_bbk_-v详细信息,-c生成中间文件,链接次序)