Doubts about the compilation and "linkage" phase of a C-Program

Asked

Viewed 507 times

-1

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

I have as source of study the book of Luis Damas (I know 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 there is no error, it creates an object file referring to the file .c. However, it does not say what is inside the object code.

After all, what makes up the object file?

And when we include a #include any in our code, the preprocessor inserts the library into our program, or only a "link" that informs the linker which library to use and where it is located?

2 answers

3

The #include is something that only serves the compiler. You already have question speaking of, there’s no need to repeat here.

I have already answered in general terms how a compiler works.

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

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

You can use the objdump to see what’s inside it. Or dumpbin use Microsoft.

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

To understand a little about the binary code operation already has a question.

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

The internal format does not depend on the compiler but on the linkeditor that he wants to conform to. It has compilers that have linkeditors together then end up giving to speak that is tool format as a whole.

  • Thank you very much, I will study these articles. :)

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 their file. c, already in machine instructions format. The Labels for jumps and function calls are still temporary, Linker will arrange them at the time of creation of the final executable.

And about includes, always think of them as if you gave a Ctrl+c Ctrl+v of the file you are 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. Thus, when calling a function that receives two ints the compiler knows that he has to generate instructions to place these parameters in the stack before making the call. So yes, it’s like a link itself, an empty space that Linker then replaces with the "real" address of that function in the executable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.