How to compile , assemble and generate executable ?

Home Forums C-programming.. How to compile , assemble and generate executable ?

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2090 Reply
    Humble
    Keymaster

    Lets start with basic process of C programming and its different step to make it useful as a binary/executable…

    Lets assume we have a program called compile.c and it has below contents.

    [hchiramm@humbles-lap C-Pgm]$ cat compile.c 
    # include <stdio.h>
    
    int main ()
    {
    
    	printf ("\n I was told to compile ");
    }
    
    
    
    The compiler turns the C source code into an object code file, which is a file ending in ".o" and it contains the binary version of the source code.
    
    To execute object code you need to make it as an executable with the help of linker..
    
    
    #gcc -c compile.c
    
    above tells the compiler that, do  preprocessing and compilation and give me an "object file".
    
    
    [hchiramm@humbles-lap C-Pgm]$gcc -c compile.c 
    [hchiramm@humbles-lap C-Pgm]$ ls
    compile.c  compile.o
    [hchiramm@humbles-lap C-Pgm]$ file compile.o 
    compile.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
    
    
    
    
    
    But if you do this:
    
    
    [hchiramm@humbles-lap C-Pgm]$gcc compile.c -o compile
    
     [hchiramm@humbles-lap C-Pgm]$  file compile
    compile: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x19c38aaa4784da0ab4f7fc0db4fb62bfd82fd63a, not stripped
    
    
    
    In above case you are asking the compiler to invoke the "linker" as well.. That said , preprocessing + compiling + linking has been done and you have an executable called "compile"..
    
    In linux, the linker program is "ld".. Its job is linking object files and creating an executable..
    
    The GNU compiler collection do different stages ( preprocessing, compiling, linking..etc) as per the options provided to it.. 
    
    finally you have an executable : Execute it :
    
    
     [hchiramm@humbles-lap C-Pgm]$ ./compile 
    
     I was told to compile [hchiramm@humbles-lap C-Pgm]$ 
    
Viewing 1 post (of 1 total)
Reply To: How to compile , assemble and generate executable ?
Your information: