美文网首页gcc编译器入门
005_wz_bbk_-v详细信息,-c生成中间文件,链接次序

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

作者: 王泽0106 | 来源:发表于2020-11-05 17:03 被阅读0次

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生成中间文件,链接次序

    Verbose(冗长的) Compilation The '-v' option can be used to d...

  • 从零开始学习Linux(四):链接命令

    1、ln 命令作用:生成链接文件。 1.1、创建软链接 查看生成的软链接文件的详细信息,如下图所示: 软链接的特点...

  • C语言学习教程

    编译文件:cc -c one.c two.c 生成.o目标文件 链接文件:cc one.o two.o 默认生成...

  • OC 编译过程

    Objective-C文件的编译过程主要包括clang前端的预处理、编译、后端优化中间表示、生成汇编指令、链接、生...

  • C语言生成可执行文件的编译过程

    C语言源文件要经过编译、链接才能生成可执行程序: 1、 编译(Compile)会将源文件(.c文件)转换为目标文件...

  • C 语言的编译

    如图,每个 .c 结尾的C源文件通过 gcc -c *.c 生成一个以 .o 结尾的目标文件,最后通过链接器(实现...

  • 2017-06-20

    c编译过程先生成二进制文件 .o文件 gcc -c。里面调用的函数是一个符号。 链接器之后才真正链接。$ ld -...

  • makefile-动态链接库(*.so)

    目录文件 hello.c hello.h 生成动态链接库libhello.so main.c 内容 使用动态链接库...

  • Linux C 编程基础

    1、gcc编译过程 基本命令: 2、编译文件 3、静态链接库 (1) 首先用-c编译各个单源文件.c生成.o文件 ...

  • C语言运行过程

    编译:cc -c xxx.c执行预编译指令检查语法生成 xxx.o 目标文件 链接:cc xxx.o为目标文件增加...

网友评论

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

    本文链接:https://www.haomeiwen.com/subject/inlnvktx.html