Jump to content

Compiler Linker and Loader


krishg3

Recommended Posts

Consider three source code files main.c, f1.c, and f2.c, containing some C source codes. The function main is defined in the file main.c, function f1 is defined in the file f1.c and function f2 is defined in the file f2.c.

The function main calls the function f1, and function f1 in turn calls the function f2. Your object is to create an executable file named say project.

 

Qu.1. What are the steps to be followed, when finally you execute “project” starting from source code preparation?

 

Qu.2. Is it possible to compile the file main.c, without having files f1.c, f2.c? Mention an option of a compiler that you know which is used to accomplish this.

 

Qu.3. Will there be any difference in the final executable, if it is produced for a 32 bit system / 64 bit system? Name a command by which you can find out the type of the executable file.

 

Qu.4. Suppose now the file f1.c contains a function f1, which is completely defined in that file and file f2.c, which contains a function f2, which is also completely defined in that file. The file main.c can either call f1 or f2. How can you ensure that the executable file will only contain the code for that function and no redundant code?

 

Qu.5. Please see, what type of assembly code is created, corresponding to the function call statements in your system

Link to comment
Share on other sites

  • 1 month later...

Qu.1. What are the steps to be followed, when finally you execute “project” starting from source code preparation?

 

Answer.1. steps:

 

1. you have to use #include "f1.c" & #include "f2.c" in main.c

2. you compile using a C or C++ compiler which is in Windows dev-cpp or Microsoft Visual C++ express ..etc ,and in Linux gcc or g++ in the terminal

3. execute your program in windows it's a .exe file ,and in Linux it's a.out

 

Qu.2. Is it possible to compile the file main.c, without having files f1.c, f2.c? Mention an option of a compiler that you know which is used to accomplish this.

 

Answer.2. No, there is no possibility for doing that, because of two things:

 

1. if files are #included "...", you have to have them with the main.c

2. if you remove lines of #include "...", you will get errors for functions that have no definition,

 

Qu.3. Will there be any difference in the final executable, if it is produced for a 32 bit system / 64 bit system? Name a command by which you can find out the type of the executable file.

 

Answer.3. a 32-bit executable is totally different than 64-bit exe,

.. you can notice that problem when you try to run a 16-but exe program on windows xp !

and to find its type is possible from right-click => properties in Windows,

 

Qu.4. Suppose now the file f1.c contains a function f1, which is completely defined in that file and file f2.c, which contains a function f2, which is also completely defined in that file. The file main.c can either call f1 or f2. How can you ensure that the executable file will only contain the code for that function and no redundant code?

 

Answer.4. when you do a normal inclusion using #include "file.c" it's known as the static inclusion where the Linker links the included files simply, weather its functions were called or not,

 

the way you want is known as dynamic inclusion, #include <library.h>, where you have to do three steps:

1. create a library reference file file.lib, placed into /lib file

2. create a dynamically-linked-library file file.dll, placed into /system file

3. write the include library with only declarations, file.h

4. make the Linker know that there are dynamically-linked-libraries needed

.. the way to write .lib and .dll files is not easy

 

Qu.5. Please see, what type of assembly code is created, corresponding to the function call statements in your system

 

Answer.5. the assembly code is "another story" because it depends on the Compiler, the Compiler technology, and not only the Architecture of the machine, but also the operating system it's compiled for ...

 

.. but most of personal computers are on the 80/86 architecture, also calling functions requires special jumps after saving state in ASM

 

-----------------------------------

 

good luck

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.