In article <11**********************@g47g2000cwa.googlegroups .com>,
st*********@gmail.com <st*********@gmail.com> wrote:
Usually, people write the program like this:
// a very easy example:
#include <cstring>
int main() {
size_t length = strlen("Hello, world!\n");
return 0;
}
today, I open <cstring> file, and I found there are only declarations
in it.
My question is this: Without definitions, How can I use function
"strlen(...)"?
Did the compiler do something I don't know? Strange!!!
This puzzled me a lot, Would you please tell me the reason?
Right. If a function is not a likely candidate for inline'ing
it is probably not provided in a header file (and furthermore
usually should not be). Where is it then? Well, somebody
(in this case your vendor) might have another source file
such as:
// strlen.c
#include ... whatever
size_t strlen(const char *string_arg)
{
// strlen innards
}
This is then compiled by itself, as you would do with
some of your own routines. And usually the object file,
and similar object files are placed into an "archive"
of one sort of another. Traditionally this would be a
..a or .lib file (in the case of UNIX and Windows, respectively)
but often also finds their way into shared libs (.so's) or
DLL's. Therefore, the routines line strlen() either gets
linked into a static program image of your executable
when you link it, often automatically if it's the standard
library, which it is in this case, or it gets "loaded with"
your program when it needs it during runtime. Most of
this is implementation defined meaning the standard does
not mention details such as linkers and loading, just
what it means to have a complete program, so you may find
differences across platforms. For instance, a common
think that used to be done (and still is in some cases)
is to not automatically link in all floating point routines.
In this case you would get a linker error saying say a
routine from math.h was not found, whereupon you'd need
to purposely tell it what that file is in order to
get your program imagine. Lots of issues here, but
this is one aspect of these details.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==>
http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?