[LCP]stdio.h reference

Santosh.Kawade at eyield.com Santosh.Kawade at eyield.com
Tue Jan 8 17:57:36 UTC 2002


I have 2 questions  in reference to your answer to the #include  

>The preprocessor includes the files, expand the macros, 
> strips comments, and then send the output to the compiler. 

Does the preprocessor strips the comment or is it the Lexical Analyzer ( first
phase of compilation) which does that ?

my second question is that you said the compiler compiles the code and passes it
to assembler. Does the assembler really comes into picture or the compiler
handles everything and passes the obj code to linker to link into exe or
library.

SAN.

____________________Reply Separator____________________
Subject:    Re: [LCP]stdio.h reference
Author: linuxcprogramming at lists.linux.org.au
Date:       1/7/02 5:20 PM

Madhav M., pigiando tasti a caso sul citofono, ha scritto:

 > I want to know, how linker reolves the reference for stdio.h.  I
 > chekced /etc/fstab file and also checked LD_LIBRARY_PATH. But i
 > didnt get the path for stdio.h.  Am I missing something???

The linker has nothing to do with preprocessor directives.

``#include <stdio.h>'' is a directive telling the C preprocessor to
 substitute that directive with the content of a file named stdio.h.

The preprocessor looks for that file in a set of directories:

 - first, all the ones specified on the command line with the
   -I<directory> options

 - then a set of common directories like /usr/include and the compiler
   installation directory
   (i.e. /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include)


Also, if you specify the name of the file between double quotes
instead of angular parenthesis, as in

   #include "stdio.h"

the first directory searched by the preprocessor for that file is the
one containing the file being preprocessed.

The preprocessor includes the files, expand the macros, strips
comments, and then send the output to the compiler. 

The compiler in turns generates the assembly code for the target
platform, and that is passed to the assembler.

The assembler in turns generates an object file containing the
machine-excutable code, data and other structures needed by the
linker.

This has to be done for each C source file in your
application/library, so unless you have only one C source file, you
end with several object files.

These can be grouped in an archive library (.a) using the `ar' and
`ranlib' tools, so they can be used later for other programs (i.e. if
you want to make a library). 

Or they can be passed to the linker to produce either an executable,
or a dynamic library (.so, or .sl on some systems).

The linker takes one or more object files and zero or more libraries
(either archive ones or shared ones), solves the cross-references
between the various pieces of the code and then generates either an
executable or a shared library.

When you generate an executable, some options are implicit: i.e. you
don't have to explicitly specify to link the C standard library and
the startup and exit code for your program, because this is implicit. 

Instead, you may want to specify additional libraries, using one or
more options in the form of ``-L<directory>'', which tell the linker
where to search for libraries in addition to the standard directories
(like /usr/lib and the compiler installation directory), and one or
more options in the form of ``-l<library>'' which tell the linker
which libraries have to be linked in.

You can see the output of the various stages. Given you have your
program ``sample.c'':

1) you can see the preprocessor output with 

   gcc -E sample.c | less

2) you can see the assembly generated by the compiler with

   gcc -S sample.c; less sample.s

3) you can see the object file generated by the assembler with

   gcc -c sample.c -o sample.o

4) you can have your executable either with

   gcc sample.c -o sample

   or with the two lines

   gcc -c sample.c -o sample.o
   gcc sample.o -o sample

--
UNIX diapers by Pannolini USPTO 2039887  http://www.uspto.gov
Matteo Ianeselli      ianezz AT sodalia.it  (+39) 0461 316452
Visita il LinuxTrent:            http://www.linuxtrent.it
_______________________________________________
This is the Linux C Programming List
:  http://lists.linux.org.au/listinfo/linuxcprogramming List
Received: from ftoomsh ([138.25.6.1]) by mailserver.mail1.com with SMTP
  (IMA Internet Exchange 3.11) id 0005EECD; Mon, 7 Jan 2002 07:44:06 -0800
Received: from localhost ([127.0.0.1]:3577 "ehlo ftoomsh.progsoc.uts.edu.au")
    by ftoomsh.progsoc.uts.edu.au with ESMTP id <S154898AbSAGPnh>;
    Tue, 8 Jan 2002 02:43:37 +1100
Received: from kinsale.sodalia.it ([193.70.237.166]:2743 "EHLO
    kinsale.sodalia.it") by ftoomsh.progsoc.uts.edu.au with ESMTP
    id <S154897AbSAGP1j>; Tue, 8 Jan 2002 02:27:39 +1100
Received: (from ianezz at localhost)
    by kinsale.sodalia.it (8.8.6/8.8.6) id RAA02320;
    Mon, 7 Jan 2002 17:20:30 +0100 (MET)
From:   ianezz at sodalia.it
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <15417.51917.761606.160746 at kinsale.sodalia.it>
To: linuxcprogramming at lists.linux.org.au
Subject: Re: [LCP]stdio.h reference
In-Reply-To: <LIOJPHGAEIMNFAAA at mailcity.com>
References: <LIOJPHGAEIMNFAAA at mailcity.com>
X-Mailer: VM 6.95 under Emacs 21.1.1
Return-Path: <ianezz at kinsale.sodalia.it>
X-Orcpt: rfc822;linuxcprogramming at lists.linux.org.au
Sender: linuxcprogramming-admin at lists.linux.org.au
Errors-To: linuxcprogramming-admin at lists.linux.org.au
X-BeenThere: linuxcprogramming at lists.linux.org.au
X-Mailman-Version: 2.0.3
Precedence: bulk
Reply-To: linuxcprogramming at lists.linux.org.au
List-Help: <mailto:linuxcprogramming-request at lists.linux.org.au?subject=help>
List-Post: <mailto:linuxcprogramming at lists.linux.org.au>
List-Subscribe: <http://lists.linux.org.au/listinfo/linuxcprogramming>,
    <mailto:linuxcprogramming-request at lists.linux.org.au?subject=subscribe>
List-Id: Linux C Programming <linuxcprogramming.lists.linux.org.au>
List-Unsubscribe: <http://lists.linux.org.au/listinfo/linuxcprogramming>,
    <mailto:linuxcprogramming-request at lists.linux.org.au?subject=unsubscribe>
List-Archive: <http://lists.linux.org.au/archives/linuxcprogramming/>
X-Original-Date: Mon, 7 Jan 2002 17:20:29 +0100
Date:   Mon, 7 Jan 2002 17:20:29 +0100



More information about the linuxCprogramming mailing list