delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/24/09:31:17

From: Koen Deforche <deforche AT cs DOT kuleuven DOT ac DOT be>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: 2 include || !2include
Date: Thu, 24 Apr 1997 13:50:18 +0200
Organization: K.U.Leuven
Lines: 57
Message-ID: <335F48FA.1566@cs.kuleuven.ac.be>
References: <Pine DOT VMS DOT 3 DOT 91-b11-vms DOT 970424125917 DOT 867B-100000 AT sat111 DOT physnet DOT uni-hamburg DOT de>
NNTP-Posting-Host: snoopy.cs.kuleuven.ac.be
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Michael Flegel wrote:
> 
> Hi Y'all!
> 
> I've just been wondering: I use printf and exit and all those fancy
> commands :-), which I used to have to include stdlib.h and conio.h and
> stuff in my old BC3.1, Now that I use DJGPP, I compile and link my little
> "hello world" proggy using printf, getch and uhhh, nothing else, but I
> don'T have to include any header files.
> I can live with that, but it makes me a little edgy, not to know where my
> stuff comes from. So I'd appreciate an explanation from anyone who knows.
> (Maybe it had something to do wit the  fact that I link it as a C and not
> C++ program... don'T know)
> 
>   MLF/SLi

In a header file there are just statements that tell the compiler :
hey, there does exist a function printf, which is declared as
int printf(...);, and its scope is external.

What actually happens, is that the C  preprocessor replaces

#include <stdlib.h>

with the whole file, which is then given to the compiler.
(the c preprocessor just creates a new file where no #-directives
 are in anymore)

If you do not include the headerfile, then the compiler doesn't know
there is a symbol printf declared externally, and when it comes to
reading printf, it wil implicitly declare a symbol
printf, according to sintax you give it in your program, and assume
it is some external symbol.

(compiling with -Wall gives you this warning)

Finally the program (consisting of some .o files and eventually some
.a files) will be linked with the standard c libraries, and eventuall
other libraries you specify, to make you executable.
It is in this part that the external symbols will be "resolved", the
linker will connect statements (like your printf in hello world),
to the definitions of the symbols (printf is defined in the standard
c library). At this step, "duplicate symbol"-conflicts (symbols that
are defined twice), and "unsatisfied symbol"-problems (symbols that
arent defined in no .o or .a (including std libraries)-file, are
reported.

That is why you are not really, however it is neater, obligated
to #include stdlib.h in this case (with gcc (djgpp)). This is certainly
true for C (gcc), and i guess it is the same story for C++, however
i am not sure. Why it doesn't work with BC i dont know, i never used it.

However when you start using datatypes that are declared in the
headerfile, you must include it, as there is no way else, the compiler
can know how the datatype looks like.

koen.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019