Doubts about the compilation and linking phase of a C Program

0

I have some questions about the steps used to compile a C program.

I have as a source of study the book by Luis Damas (I know that it is not advisable to have only one ...), where he says that the compilation phase consists of the syntactic verification of the structure of the code to be compiled. If you do not have any errors, it creates an object file referring to the file .c . However, it does not say what it has inside the object code.

After all, what compose the object file?

And when we include a #include in our code, does the preprocessor insert the library into our program, or just a "link" that tells linker which library to use and where it is located?

    
asked by anonymous 01.10.2016 / 17:28

2 answers

2

The internal format of the object files varies from compiler to compiler, they are not required to follow a pattern about it, but basically what they have there is the list of functions and global variables of your .c file, already in instructions. The labels for jumps and function calls are still temporary, the linker will fix them when the final executable is created.

And about includes, always think of them as giving you a ctrl + c ctrl + v of the file you're including in the source file. The compiler needs this to know the name of the functions, their parameters, and the layout of the data structures. So when calling a function that receives two ints the compiler knows that it has to generate instructions to put those parameters on the stack before making the call. So yes, it's like a link itself, an empty space that the linker then replaces with the "real" address of that function in the executable.

    
01.10.2016 / 17:43
1

The #include is something that only serves the compiler. You already have ask about it .

I've broadly answered how a compiler works .

The object has essentially the binary code that it should execute. This code is bytes that make sense to instruct the processor.

Of course, it has some information that helps linker mount the executable, public symbol table, relocation information, static data generated, reference definitions, debug information (when appropriate) , etc.

You can use objdump to see what's inside it. Or dumpbin if you use Microsoft.

Each one can have a format COFF is one of the formats .

To understand a bit about binary code operation already has a question .

The object is generated as a single build unit. The compiler assembles as it is informed, there is no correlation of a .c file and an object.

01.10.2016 / 17:52