473,383 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

What did the compiler do with my program?

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.

Nov 22 '05 #1
7 1430
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.
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.

Nov 22 '05 #2
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?
Nov 22 '05 #3
<st*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.
Nov 22 '05 #4
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

Nov 22 '05 #5

"technator" <te*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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


Nov 23 '05 #6
ldd is for Solaris.

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

~Technator

Nov 24 '05 #7
technator wrote:
ldd is for Solaris.

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


Windows doesn't come with Visual Studio, however. Try:
http://www.dependencywalker.com/

Nov 24 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
12
by: Ralf Dieholt | last post by:
I still don´t get it - what is the advantage of C ? I can program anything in BASIC V2 on a C64, does C have any advantages over BASIC V2 ? Seems to be more cryptic than BASIC to me.
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
2
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to...
40
by: vishnu | last post by:
Hi friend, i have a problem in my program what is the use of static function in C lang? plz help me
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
by: dolphin | last post by:
What is the different between c++ call convention and c call convention?Can some give some examples?
46
by: Kenny O'Clock | last post by:
This came up in a job interview, what is the output of the program below? I tried to compile and run it myself, but my compiler (lcc-win32) aborts with this errors.... Warning test2.c: 3 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.