Hello here is a simple but interesting and useful topic .
Do you want to know how to create object files and link them together to create an executable files??
Well , your in the right place !!!!!
In this post we will be making use of the gcc compiler .
We will create two files
1 } wetechies.c - does not contain the main function but has the implementation of the add function .
2 } wetechiesmain.c - has the main function and calls the add function present in the wetechies.c file .
Here is the wetechies.c file
#include<stdio.h>
int add(int c , int d)
{
int result;
result = c + d;
printf("\n\nwetechies.c file has been called\n");
printf("\n\nThe result after addition is %d\n",result);
return result;
}
And here is the wetechiesmain.c file
#include<stdio.h>
int main()
{
int a , b ;
printf("\n\nwetechiesmain.c file has been called\n\n\n");
printf("Enter the two numbers to be added\n");
scanf("%d %d",&a,&b);
add(a,b);
}
Now we need to convert the above c files into object files .
We do this by using the gcc compiler using the -c option which compiles or assembles the source files, but does not link them. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
Now lets see the steps to create the object files and link them to create the final executable file> .
Step 1 : We create the object file (wetechies.o) file using the following command
Step 2 : We create the object file (wetechiesmain.o) file using the following command .
This is done by using the following command .
The -o option used along with the gcc command will place output in file wetechies_executable . This applies regardless to whatever sort of output is being produced, whether it be an executable file,an object file, an assembler file or preprocessed C code. When you link a .o file, its contents are copied into the program, whether you call the routines in it or not.
The above command will create an executable file with the name wetechies_executable .
We execute the executable file as below
Congrats you have obtained the correct output even though you have called the function in one file and got the implementation of it in another file !!!!!!!
It all happened because you created the executable file by linking two objects files .
Thats it !!!!
Leave your doubts and suggestions in the comments section .
Do you want to know how to create object files and link them together to create an executable files??
Well , your in the right place !!!!!
In this post we will be making use of the gcc compiler .
We will create two files
1 } wetechies.c - does not contain the main function but has the implementation of the add function .
2 } wetechiesmain.c - has the main function and calls the add function present in the wetechies.c file .
Here is the wetechies.c file
int add(int c , int d)
{
int result;
result = c + d;
printf("\n\nwetechies.c file has been called\n");
printf("\n\nThe result after addition is %d\n",result);
return result;
}
And here is the wetechiesmain.c file
int main()
{
int a , b ;
printf("\n\nwetechiesmain.c file has been called\n\n\n");
printf("Enter the two numbers to be added\n");
scanf("%d %d",&a,&b);
add(a,b);
}
Now we need to convert the above c files into object files .
We do this by using the gcc compiler using the -c option which compiles or assembles the source files, but does not link them. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
Now lets see the steps to create the object files and link them to create the final executable file> .
Step 1 : We create the object file (wetechies.o) file using the following command
gcc -c wetechies.c
The above command will create the wetechies.o object file .
The ls command can be used to check the contents of a directory .
Step 2 : We create the object file (wetechiesmain.o) file using the following command .
gcc -c wetechiesmain.c
The above command will create the wetechiesmain.o object file .
Step 3 : In this step we will link the two object files that we created in the above two steps in order to create the final executable file .
This is done by using the following command .
gcc -o wetechies_executable wetechies.o wetechiesmain.o
The -o option used along with the gcc command will place output in file wetechies_executable . This applies regardless to whatever sort of output is being produced, whether it be an executable file,an object file, an assembler file or preprocessed C code. When you link a .o file, its contents are copied into the program, whether you call the routines in it or not.
The above command will create an executable file with the name wetechies_executable .
We execute the executable file as below
./wetechies_executable
The below figure shows the whole procedure .
Congrats you have obtained the correct output even though you have called the function in one file and got the implementation of it in another file !!!!!!!
It all happened because you created the executable file by linking two objects files .
Thats it !!!!
Leave your doubts and suggestions in the comments section .
No comments:
Post a Comment