What did the compiler do with my program?

Discussion in 'C++' started by storyGerald@gmail.com, Nov 20, 2005.

  1. Guest

    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 20, 2005
    #1
    1. Advertising

  2. Rolf Magnus Guest

    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.
     
    Rolf Magnus, Nov 20, 2005
    #2
    1. Advertising

  3. Greg Comeau Guest

    In article <>,
    <> 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?
     
    Greg Comeau, Nov 20, 2005
    #3
  4. Jim Langston Guest

    <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Nov 21, 2005
    #4
  5. technator Guest

    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
     
    technator, Nov 21, 2005
    #5
  6. Howard Guest

    "technator" <> wrote in message
    news:...
    > 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


    >
     
    Howard, Nov 23, 2005
    #6
  7. technator Guest

    ldd is for Solaris.

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

    ~Technator
     
    technator, Nov 24, 2005
    #7
  8. Old Wolf Guest

    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/
     
    Old Wolf, Nov 24, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan
    Replies:
    0
    Views:
    1,139
  2. Jack Wright
    Replies:
    5
    Views:
    628
    Shiv Kumar
    Jan 19, 2004
  3. Ram
    Replies:
    0
    Views:
    2,847
  4. Andrey Batyuck

    Compiler compiler with C++ as output

    Andrey Batyuck, May 11, 2004, in forum: C++
    Replies:
    3
    Views:
    443
    Frederik Hertzum
    May 17, 2004
  5. Daniel Waite
    Replies:
    2
    Views:
    228
    Daniel Waite
    May 2, 2008
Loading...

Share This Page