Visit Our UNIX and Linux User Community


Need more info on internals of c compilers


 
Thread Tools Search this Thread
Top Forums Programming Need more info on internals of c compilers
# 1  
Old 10-13-2009
Need more info on internals of c compilers

Hello Gurus,

i am ok with the concepts of c language but i would like to know
more about the internals of c with respect to the compilers

what happens when we say
gcc filename.c

the a.out will get created(what actaully compiler does to the code inaddition to generating object code)

gcc -c filename.c ( )

creates only .o files we need to link them to get a.out(i need extra)

gcc -o fn filename.c ( )

request you to provide me the more info on following
symbol table( how the linker actually uses this info),virtual address(what is this why its needed), physical address(what is this why its needed),linker,loading,locating.
expecting the above with repect to the gcc compilers

thanks in advance
either links or some descrption i am expecting
# 2  
Old 10-13-2009
Homework?
# 3  
Old 10-13-2009
this is not a home work?
i am not even a student, i recently joined a company but struggling to understand the internals of the compilers.
# 4  
Old 10-13-2009
Everything you asked is outside the work the compiler does.

The compiler's main objective is to take (preprocessed) high-level code, and translate it to assembly code. Example:
Code:
$ cat hello.c
#include <stdio.h>

int main(int argc, char **argv)
{
        printf("Hello, World!\n");
        return 0;
}
$ gcc -S -o hello.s hello.c
$ cat hello.s
        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "Hello, World!"
        .text
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $4, %esp
        movl    $.LC0, (%esp)
        call    puts
        movl    $0, %eax
        addl    $4, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.2.1 (SUSE Linux)"
        .section        .note.GNU-stack,"",@progbits

The assembler then uses that to create an object file (the output of 'gcc -c').
The linker takes the object file and connects it with various libraries to create an executable.
The loader reads the executable, and loads the referenced libraries, if available, and maps them into virtual memory.

The 'gcc' command, by default, hides these steps from the user, and invokes the complete toolchain (for a C program: cpp [C Pre-Processor], cc [C Compiler], as [Assembler], and ld [Linker])

Further reading: Compiler, Assembler, Linker, Virtual memory, and a series of blog posts on Linkers

---------- Post updated at 11:37 ---------- Previous update was at 11:18 ----------

Addendum: if you compile the program above (hello.c) using
Code:
gcc -v -save-temps -o hello hello.c

you'll see all the "compilation" steps, and the intermediate steps will be saved to hello.i (preprocessed), hello.s (compiled), hello.o (assembled), and hello (linked).
# 5  
Old 10-13-2009
for your understanding at a higher, non-programmer level -

Code:
cc myfile.c -o myfile -lm
# or maybe gcc
gcc myfile.c -o myfile -lm

The above statement does this
1. preprocesses - meaning it invokes a program to honor the #include directives, #define directives in myfile.c. The name of the program that does this is usually cpp.

2. compile - transform the preprocessed code into assembly language - see pludi's
nice description above

3. link edit - this is done by the ld program. The -lm brings in a library (precompiled shared library of code) called libm. The ld program looks for all of the external symbols in your code (like printf() ) and find the external symbol in a library. Each external function you call has to be tracked down then evaluated by ld. It also finds external variables for you. It packs all of this into a specially formatted file - in "a.out" format or ELF format - or whatever file format your machine uses. There are others.

The final product is compiled or binary code, which can be run directly by the operating system.

The stement aboves means "compile myfile.c, write it out to a binary file called myfile and link against libm". BTW, ld by default, also links against the standard C library, libc, so you do not have to "ask" for it.
# 6  
Old 10-14-2009
thank you for your support.

soon i will come up with some other queries.

Previous Thread | Next Thread
Test Your Knowledge in Computers #248
Difficulty: Easy
ARPANET was designed to reliably transmit 911 messages, and to inform the host computer when it loses a message.
True or False?

9 More Discussions You Might Find Interesting

1. Solaris

How to combine c11 and c99 compilers?

I have a SPARC server with Solaris SPARC 10 OS installed I am trying to compile source code from VLC media player and PHP 7 tarballs However the stumbling block I encounter is that when I type in ./configure into my PUTTY terminal the GCC compiler tells me halfway through configuration that... (3 Replies)
Discussion started by: alphatron150
3 Replies

2. Programming

Makefile for two compilers

I have g++-4.6 and g++-4.8.2 installed on my system and want to build a suite of programs using each one. I am thinking putting the option on what to build by having a different target, one for g++-4.6 and another target for g++-4.8.2. What do you think? (1 Reply)
Discussion started by: kristinu
1 Replies

3. AIX

C Compilers Available on AIX

Hi Guys, I want to know what are the different C compilers are available on AIX like cc, gcc etc. Appreciate your help. Regards, Srikanth:) (1 Reply)
Discussion started by: ysrikanth
1 Replies

4. What is on Your Mind?

How to switch from SVR4/BSD internals to Linux internals?

Hello, Long-time Unix hacker here - I've worked on four variants of the kernel prior to the introduction of Linux. In my spare time, I've written Linux (Ubuntu) device drivers, kernel modules, cross-compiled, and built the kernel. I'd like to do Linux internals/device drivers as a day job,... (1 Reply)
Discussion started by: OriginalVersion
1 Replies

5. Homework & Coursework Questions

Phases of Compilers

Hello I have a questions and m stuck on it. Theoritcally the phases of compiler. 1. Lexical analyis -- genrates tokes NOW MY QUESTION IS: where i can see these token if i make a simple C program. Please guys tell me step to see it. and other phases too. your help will be appreciated... (1 Reply)
Discussion started by: aditya08
1 Replies

6. Programming

Help with C compilers

i have windows 7 64bit, and i want to program C in it, i have the eclipse for C, but it wont work because i have no C compilers , where do i get a C compiler, so eclipse will work? I usually transfer to a unix server and use a command gcc -Wall -g -o ___ ___ where can i get the gcc C... (5 Replies)
Discussion started by: omega666
5 Replies

7. AIX

Error: could not find any of these C compilers

Hi, While trying to configure Apache Web Server on P630 Server i am getting the below message "Creating Makefile in src + configured for AIX 5.2 platform Error: could not find any of these C compilers anywhere in your PATH: gcc cc acc c89 Configure terminated" when i did a "lslpp -l... (2 Replies)
Discussion started by: cherryven
2 Replies

8. UNIX for Dummies Questions & Answers

Finding out available C++ compilers on my system

How can I find out what C++ compilers are available on my system? Thanks in advance (7 Replies)
Discussion started by: HelpMeIAmLost
7 Replies

9. Windows & DOS: Issues & Discussions

MS DOS Basic to .EXE compilers?

Hi, Does anyone know any compilers that will do Microsoft Basic files and make them .exe files. Thanks :D (1 Reply)
Discussion started by: jacx2
1 Replies

Featured Tech Videos