Build links (1)

Foreword

This article describes the brief time gcc to compile a program of what has been done

text

one example

********fn1.c************
#include<iostream>
void fn1(){        //定义了一个函数
    std::cout<<"this is fn1"<<std::endl;
}
********main.c************
void fn1();       //声明了一个函数

int main(){
    fn1();
}

First look at an example, there are two file content above, fn1.c defines a function fn1 () , but only declared in main.c fn1 () is a function, sometimes we usually use compiled as follows, and then directory will have one executable file a.out

g++ main.c fn1.c

But here's good results in the package hides many of the details, it actually works as follows, contains the compile and link phase

g++ -c fn1.c   //编译期,生成fn1.o
g++ -c main.c  //编译期,生成main.o
g++ -o a.out fn1.o main.o //链接期, 搜索fn1.o和main.o文件中的符号,维护一张符号表,为只有声明还未没有定义的符号找到其定义的地方,这步就叫做链接

About Links

When multiple .o files (also known as object files) for linking these documents carries its own symbolic message, the linker will first go through the symbol table maintenance information, and the maintenance process will often be some symbol redefinition of the problem, because different target file has the same name as a symbol of;
after the completion of maintenance of this linker symbol table, but undefined symbols can be linked by declaring its operation .o file, if not find the error will be reported can not find the symbol;

Guess you like

Origin www.cnblogs.com/ishen/p/12080162.html