What did the compiler do with my program?

S

storyGerald

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?

Thank you!



Gerald

At last, I don't know how to thank the friends who helped me. I pointed
out my problem, and you solve it without any pay ^_^. I do appreciate
what you have done. And I like to communicate with you all.
 
R

Rolf Magnus

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.

That's what headers are for.
My question is this: Without definitions, How can I use function
"strlen(...)"?
Did the compiler do something I don't know? Strange!!!

Maybe the compiler, maybe the linker. What happened is that the C++ standard
library was automatically linked to your program.
 
G

Greg Comeau

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.
 
J

Jim Langston

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?

Thank you!

The actual function for strlen is sitting in some library somewhere that
gets pulled into your program when you link it. The header just lets the
compiler know how to access this library function.

If you look at the linking of your program you should see some libraries
included in the link.
 
T

technator

Use the command "ldd" to find the .so files loaded with your program.

For example, If I use it on one the executables, it shows the
following:

libm.so.1 => /usr/lib/libm.so.1
libnsl.so.1 => /usr/lib/libnsl.so.1
libresolv.so.2 => /usr/lib/libresolv.so.2
libc.so.1 => /usr/lib/libc.so.1
libgen.so.1 => /usr/lib/libgen.so.1
libdl.so.1 => /usr/lib/libdl.so.1
librt.so.1 => /usr/lib/librt.so.1
libpthread.so.1 => /usr/lib/libpthread.so.1
libthread.so.1 => /usr/lib/libthread.so.1
libsocket.so.1 => /usr/lib/libsocket.so.1

~techantor
 
H

Howard

technator said:
Use the command "ldd" to find the .so files loaded with your program.

For example, If I use it on one the executables, it shows the
following:

libm.so.1 => /usr/lib/libm.so.1
libnsl.so.1 => /usr/lib/libnsl.so.1
libresolv.so.2 => /usr/lib/libresolv.so.2
libc.so.1 => /usr/lib/libc.so.1
libgen.so.1 => /usr/lib/libgen.so.1
libdl.so.1 => /usr/lib/libdl.so.1
librt.so.1 => /usr/lib/librt.so.1
libpthread.so.1 => /usr/lib/libpthread.so.1
libthread.so.1 => /usr/lib/libthread.so.1
libsocket.so.1 => /usr/lib/libsocket.so.1

~techantor

That's nice, but doesn't do much on my Windows machine. :)

-Howard
 
T

technator

ldd is for Solaris.

For Windows, use Dependecy Walker which comes with Microsoft Visual
Studio.

~Technator
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top