 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
iceman80231@yahoo.com Guest
|
Posted: Mon Mar 28, 2005 11:46 pm Post subject: Very Newbie Question |
|
|
Hello all,
I had a question regarding header files in C. If you include a
standard header file, such as <stdio.h>, in your code, the conventional
wisdom says that the function prototypes contained within stdio.h will
be placed into the .c source code file referencing the .h file by the
preprocessor. My question is, whether one is dealing with a standard
..h file such as stdio.h or a user-defined .h file, how does the
compiler find the source code file containing the code for the
functions whose prototypes are defined in the .h file that the coder is
using?
An example to better explain my question:
#include <stdio.h>
int main(){
printf("Hello world!n);
return 0;
}
The preprocessor will replace the "#include <stdio.h>" directive will
all of the function protypes within the stdio.h file. But where does
it get the source code for the function prototypes defined in stdio.h
so the program knows what to do when a function like "printf" is
called?
Many thanks.
iceman
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Tue Mar 29, 2005 9:28 am Post subject: Re: Very Newbie Question |
|
|
* [email]iceman80231 (AT) yahoo (DOT) com[/email]:
| Quote: | The preprocessor will replace the "#include <stdio.h>" directive will
all of the function protypes within the stdio.h file. But where does
it get the source code for the function prototypes defined in stdio.h
so the program knows what to do when a function like "printf" is
called?
|
printf is part of the standard C library (and also part of the standard
C++ library).
The practice: that library has been compiled separately and is usually linked
into your program by default, without having to do anything special. See e.g.
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_01_01.html>.
When you create a larger program or a library to be reused, it may become
important to _tell_ the linker where to get the definition from, e.g. one
compiled with debug information versus one without debug information, or one
that's copied into your final executable versus one that's just referenced and
resides in a physically separate file (called a dynamically linked library).
The theory: the function definition could come from anywhere, e.g. built-
in in the compiler, from a database, whatever, because the standard does
not require an actual compiler and linker, just that your source code
gives the defined effect when it's executed in some unspecified way.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
R.F. Pels Guest
|
Posted: Tue Mar 29, 2005 9:29 am Post subject: Re: Very Newbie Question |
|
|
[email]iceman80231 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | preprocessor. My question is, whether one is dealing with a standard
.h file such as stdio.h or a user-defined .h file, how does the
compiler find the source code file containing the code for the
functions whose prototypes are defined in the .h file that the coder is
using?
|
It does not. Actually, the whole process is divided in three parts:
1. preprocessing
2. compiling
3. linking
What happens is that the compiler leaves instructions for the linker to fill
in in step 3. Those are 'external references'. This can also be done with
data, it is not constrained to functions.
--
Ruurd
..o.
...o
ooo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Mar 29, 2005 9:30 am Post subject: Re: Very Newbie Question |
|
|
[email]iceman80231 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | [...]
#include <stdio.h
int main(){
printf("Hello world!n);
return 0;
}
The preprocessor will replace the "#include
all of the function protypes within the stdio.h file. But where does
it get the source code for the function prototypes defined in stdio.h
so the program knows what to do when a function like "printf" is
called?
|
It doesn't, usually. The machine code for those function is usually
supplied instead of the source code. That machine code is collected
in what is known as "library" in the "linker" sense. The "library"
is then "linked" during "linking" and the functions (or rather their
machine code) is added to your final executable code and all instances
of calling those functions are thus resolved. Sometimes the machine
code is not added during "linking", but instead another mechanism is
used: dynamically linked libraries ("shared objects"). The code is
not added to your executable until the last moment: actual running of
the program.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Tue Mar 29, 2005 9:33 am Post subject: Re: Very Newbie Question |
|
|
On 28 Mar 2005 18:46:42 -0500, <iceman80231 (AT) yahoo (DOT) com> wrote:
| Quote: | I had a question regarding header files in C. If you include a
standard header file, such as <stdio.h>, in your code, the conventional
wisdom says that the function prototypes contained within stdio.h will
be placed into the .c source code file referencing the .h file by the
preprocessor. My question is, whether one is dealing with a standard
.h file such as stdio.h or a user-defined .h file, how does the
compiler find the source code file containing the code for the
functions whose prototypes are defined in the .h file that the coder is
using?
|
Compiler does not do that. This is what linkers are for. You might want
taking a look at http://www.iecc.com/linker/
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Mar 29, 2005 9:34 am Post subject: Re: Very Newbie Question |
|
|
[email]iceman80231 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | I had a question regarding header files in C. If you include
a standard header file, such as <stdio.h>, in your code, the
conventional wisdom says that the function prototypes
contained within stdio.h will be placed into the .c source
code file referencing the .h file by the preprocessor. My
question is, whether one is dealing with a standard .h file
such as stdio.h or a user-defined .h file, how does the
compiler find the source code file containing the code for the
functions whose prototypes are defined in the .h file that the
coder is using?
|
It all depends on the implementation, and the compiler doesn't
normally need to find the source code; only the object file that
was generated when compiling it. (Usually, such object files
are collected into library files.)
If you look at your compiler documentation, you will see a
number of options which can be used to indicate where the
compiler should look for library files -- normally, the same as
for include files, the compiler will have a few places
"built-in", where it will always look (and where it will find
the library files with the implementation of the standard
library.)
| Quote: | An example to better explain my question:
#include <stdio.h
int main(){
printf("Hello world!n);
return 0;
}
The preprocessor will replace the "#include
directive will all of the function protypes within the stdio.h
file. But where does it get the source code for the function
prototypes defined in stdio.h so the program knows what to do
when a function like "printf" is called?
|
I have the impression that you are not really familiar with how
a compiler works. Usually, a compiler will translate source
files into object files, one object file per source file. After
that, it is necessary to link all of the object files together
in order to create an executable -- most "compilers" today are
really driver programs, which invoke the compiler and the
linker, as necessary. Since the number of object files for even
the simplest program can be enormous (each function in the
standard library will typically be in a separate object file),
it is usual to collect a number of separate object files into a
library file. This is normally done by a separate tool, LIB
under Windows, ar under Unix, but in some cases (e.g. Sun CC),
the compiler driver can (or must) be used for this as well.
The compiler is normally furnished with the necessary system
libraries -- the standard library, but also a number of
libraries necessary to access non-standard system functions
(Windows or Posix API, for example). Note that it is not usual
for the compiler to be supplied with the library sources; all it
needs is the already compiled object files present in the
library.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gareth Guest
|
Posted: Tue Mar 29, 2005 9:36 am Post subject: Re: Very Newbie Question |
|
|
[email]iceman80231 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | The preprocessor will replace the "#include <stdio.h>" directive will
all of the function protypes within the stdio.h file. But where does
it get the source code for the function prototypes defined in stdio.h
so the program knows what to do when a function like "printf" is
called?
|
Iceman,
printf is part of the standard C library. Where exactly this is
depends on your system; on Unix systems, it is often found at
/usr/lib/libc.a or /usr/lib/libc.so. It should be pointed out that the
*source* code for the standard library is only used once - namely when
compiling and installing the library itself. When you build one of
your own programs, the linker looks in libc to find *object* code for
functions like printf, not the source code.
On most systems, libc is included in the link command for C/C++
programs by default (along with the math library, libm, and various
other system libraries). In addition to these, C++ programs are
automatically linked to the C++ standard library, libstdc++.
Gareth
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
skol Guest
|
Posted: Tue Mar 29, 2005 10:07 pm Post subject: Re: Very Newbie Question |
|
|
| Quote: | But where does it get the source code for the function prototypes
defined in stdio.h so the program knows what to do when a function
like "printf" is called?
|
No source gets inserted for printf()! Linker takes .obj code
for the function printf() from the corresponding .lib file.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
skol Guest
|
Posted: Wed Mar 30, 2005 12:52 am Post subject: Re: Very Newbie Question |
|
|
Roast Leg of Amputee
By all means, substitute lamb or a good beef roast if the haunch
it is in any way diseased. But sometimes surgeons make mistakes,
and if a healthy young limb is at hand, then don?t hesitate to cook
it to perfection!
1 high quality limb, rack, or roast
Potatoes, carrot
Oil
celery
onions
green onions
parsley
garlic
salt, pepper, etc
2 cups beef stock
Marinate meat (optional, not necessary with better cuts).
Season liberally and lace with garlic cloves by making incisions,
and placing whole cloves deep into the meat.
Grease a baking pan, and fill with a thick bed of onions,
celery, green onions, and parsley.
Place roast on top with fat side up.
Place uncovered in 500° oven for 20 minutes, reduce oven to 325°.
Bake till medium rare (150°) and let roast rest.
Pour stock over onions and drippings, carve the meat and
place the slices in the au jus.
Bisque à l?Enfant
Honor the memory of Grandma with this dish by utilizing her good
silver soup tureen and her great grandchildren (crawfish, crab or
lobster will work just as well, however this dish is classically
made with crawfish).
Stuffed infant heads, stuffed crawfish heads, stuffed crab or lobster shells;
make patties if shell or head is not available
(such as with packaged crawfish, crab, or headless baby).
Flour
oil
onions
bell peppers
garlic salt, pepper, etc.
3 cups chicken stock
2 sticks butter
3 tablespoons oil
First stuff the heads, or make the patties (see index)
then fry or bake.
Set aside to drain on paper towels.
Make a roux with butter, oil and flour,
brown vegetables in the roux, then add chicken stock and
allow to simmer for 20 minutes.
Add the patties or stuffed heads, and some loose crawfish,
lobster, lon
|
|
| Back to top |
|
 |
annamalai.gurusami@gmail. Guest
|
Posted: Wed Mar 30, 2005 11:34 pm Post subject: Re: Very Newbie Question |
|
|
Gareth wrote:
| Quote: |
On most systems, libc is included in the link command for C/C++
programs by default (along with the math library, libm, and various
other system libraries). In addition to these, C++ programs are
automatically linked to the C++ standard library, libstdc++.
|
I doubt whether the math library is included by default. One needs to
explicitly provide the linker option -lm. Atleast this is the case on
GNU/Linux platform.
Rgds,
anna
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|