Pages

This blog is under construction

Friday, November 16, 2018

How to compile a c program in Linux, written in multiple source files?


       
(Assuming that all the files which you created, are in the current directory)

Step 1:

Write functions and header files.
             

add.h
                            

                       add.c




mul.h


                                                                                                    mul.c





main.c
compiling different file of a program




                                                                         



Step 2: (Compiling multiple source files)

Now follow the below commands in given order to link the main program with your designed Functions.

 gcc -I. -c main.c -o main.o         //generate object code of main.c file
 gcc -I. -c add.c -o add.o             //generate object code of add.c file
 gcc -I. -c mul.c -o mul.o             // generate object code of mult.c
 gcc main.o add.o mul.o -o final_output

//link all object codes main.o, add.o and mul.o and create executable code final_output

 ./final_output        // run output file

First Command will generate the object code, -I option is used to tell the compiler where the header files are included, Dot(.) means current directory in Linux, since our header files are in the current directory.
The second will link the main function with other functions
 Finally, in 3rd you can run your executable code final_output  by ./final_output


No comments:

Post a Comment