C++: Lesson Seven introduces us to the idea that not all the code of an applicat
ID: 3592485 • Letter: C
Question
C++:Lesson Seven introduces us to the idea that not all the code of an application is written at the same time by a single person. Nearly all serious applications are written by teams of people. And major pieces of the application come from standard code that already exists.
But do we really understand the mechanics of this? One thing that is lacking in this course (mostly due to lack of time - we have too many topics as it is) is a look at how compiled code is linked together to produce executable programs.
So let's talk about that. Many of you know a lot of this, so please share with your classmates. Focus on the following questions and contribute to our understanding. Some of you could probably answer all of these, but try to "share" and only hit a point or two at at time.
Here are my questions:
When I use a function like the stream function open, where is the code for open? Where is its source code? Where is its compiled object code? How does a call to open in my compiled code know where the open function resides in memory?
Explanation / Answer
Basically the code for the fucntion open is present in the header files which is included to our current program using the preprocessor directive #include.
There are basically 5 operations involved when we compile and execute the code.
First of all the code is written using any text editor and is saved in the form of the file with .c extension on the computer. When the code is sent for the compilation purpose the first operation performed on the code is preprocessing. The source code written by us is compact. It involves many header files also which are added by us in the file. At the time of preprocessing the code from the header files in included into our present code. Not only the code is included into the current file, but also all the operations specified by the mentioned preprocessor directives like #define, #include, #ifdef, etc are also applied on the code and the expanded version of the source code is saved in a file with .I extension.
The next step after preprocessing is Compilation. The expanded source code is passed to the compiler which checks for the syntax errors in the code and if there are any syntactical errors in that case returns the output on the screen mentioning the necessary errors and the warnings generated by the compiler. If the expanded code is error free in that case the compiler generates the equivalent assembly level code and saves the assembly level code in a file with the .asm extension.
As the assembly code is generated by the compiler then this assembly code is given as an input to the assembler. The work of the assembler is to convert the assembly code into the relocatable object code. Here the term relocatable is used because all the memory addresses are relative to each other and are not physically allocated.
The next task is of linking. This is performed by linker. The work of the linker is to
After linking we get an executable file with a file extension of .exe .
Now the code is completely compiled and is ready for execution. We just need to load program into the RAM. This is the work of the operating system program known as the program loader. It assigns the memory to the program. As we know that the memory addresses are relative to each other in the program, hence the memory is allocated to program. And finally the code is excuted and we are able to get the output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.