About -lm switch used for linking math.h

Discussion in 'C Programming' started by Avinash Sonawane, Jan 26, 2012.

  1. Sir,
    I am using gcc 4.6.1.
    I have few questions regarding -lm switch. Please kindly guide me for the same.

    1) Why I need to use -lm flag for math.h even if I had declared the
    #include <math.h> in the code?
    2) If it's all about linking the library then why I don't have to do
    the same thing with stdio.h too?
    3) Or we have to add the -l'library' switch for every library excluding stdio.h?
    4) In case if the -l'library' switch is used for some specific
    libraries only then please direct me to the complete list of such
    libraries.

    Thanks.
    Avinash Sonawane, Jan 26, 2012
    #1
    1. Advertising

  2. Avinash Sonawane <> writes:
    > I am using gcc 4.6.1.
    > I have few questions regarding -lm switch. Please kindly guide me for the same.
    >
    > 1) Why I need to use -lm flag for math.h even if I had declared the
    > #include <math.h> in the code?


    "include <math.h>" tells the compiler to, in effect, include the
    contents of "math.h" (declarations) in your source file. "-lm" tells
    the linker to use the math library, which contains the *implementations*
    of the functions declared in math.h.

    > the same thing with stdio.h too?


    No, because the functions declared in stdio.h are defined in a library
    that's linked by default.

    > 3) Or we have to add the -l'library' switch for every library excluding stdio.h?


    You need "-l" for *some* libraries. For gcc, most of the functions
    defined in standard header are in the standard library, which is linked
    by default (<math.h> and "-lm" are probably the one exception).
    Headers that aren't defined by the C standard may or may not require an
    explicit "-l" option.

    > 4) In case if the -l'library' switch is used for some specific
    > libraries only then please direct me to the complete list of such
    > libraries.


    There is no such complete list. The libraries that are installed will
    vary tremendously from one system to another. You'll just have to read
    the documentation for whatever function you want to use. For example,
    if you're on a Unix-like system, "man sqrt" will show you the
    declaration of the sqrt() function and tell you that you need
    "#include <math.h>" and "-lm".

    The need to use "-lm" is widely criticized, and there are
    implementations that don't require it (that put the math library into
    the standard library).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 26, 2012
    #2
    1. Advertising

  3. Avinash Sonawane

    James Kuyper Guest

    On 01/26/2012 04:59 PM, Avinash Sonawane wrote:
    > Sir,
    > I am using gcc 4.6.1.
    > I have few questions regarding -lm switch. Please kindly guide me for the same.
    >
    > 1) Why I need to use -lm flag for math.h even if I had declared the
    > #include <math.h> in the code?


    #include <math.h> tells the compiler which library header to insert into
    your program when compiling. -lm tells the linker which library to
    search when linking your compiled program. A compiler could connect the
    two, but most of the ones I'm familiar with don't.

    > 2) If it's all about linking the library then why I don't have to do
    > the same thing with stdio.h too?


    By convention dating back to the early days of C, most of the C standard
    library is often stored in one library file, except for the part
    described by <math.h>, which is stored in a different one. This approach
    became conventional because, especially at that time, most programs made
    no use of the math library, and adding all of those functions to the
    linker's search would significantly slowed down linkage. This is far
    less of an issue now than it used to be, but some habits, once started,
    are hard to change.

    > 3) Or we have to add the -l'library' switch for every library excluding stdio.h?


    No.

    > 4) In case if the -l'library' switch is used for some specific
    > libraries only then please direct me to the complete list of such
    > libraries.


    Any such list could, in principle, be different for different compilers,
    and there's no way it could be complete - there's millions of libraries
    out there. The best you can hope for is a list of C standard library
    parts. However, for most systems I've familiar with, <math.h> is the
    only part of the C standard library that has it's own separate library file.
    James Kuyper, Jan 26, 2012
    #3
  4. Avinash Sonawane

    jacob navia Guest

    Le 26/01/12 22:59, Avinash Sonawane a écrit :
    > Sir,
    > I am using gcc 4.6.1.
    > I have few questions regarding -lm switch. Please kindly guide me for the same.
    >
    > 1) Why I need to use -lm flag for math.h even if I had declared the
    > #include<math.h> in the code?


    Because you do not have a Macintosh. If you would have one, you
    wouldn't need that.

    > 2) If it's all about linking the library then why I don't have to do
    > the same thing with stdio.h too?


    Because of hysterical reasons.

    > 3) Or we have to add the -l'library' switch for every library excluding stdio.h?


    Yes. For each library you should add the corresponding name to the link
    command.

    > 4) In case if the -l'library' switch is used for some specific
    > libraries only then please direct me to the complete list of such
    > libraries.
    >


    The -l'library' switch is needed for all the libraries not included
    by default. Most linkers include some libraries by default, at least
    the C library (that is why you do not need to include the stdio
    library).

    In more advanced systems, there is no distinction between the stdio
    library and the mathematical library: they are all part of the C
    library and included by default. Those systems do NOT need any -lm
    switch. There are other systems that provide many more libraries
    included by default.

    There are some linkers/IDE combinations that have a small database
    of which functions are included in which library so that it isn't
    needed to remember the name of the library, the IDE just consults
    its database and figures out the correct switch for the linker.
    jacob navia, Jan 26, 2012
    #4
  5. Avinash Sonawane

    Eric Sosman Guest

    On 1/26/2012 5:17 PM, James Kuyper wrote:
    > On 01/26/2012 04:59 PM, Avinash Sonawane wrote:
    >> Sir,
    >> I am using gcc 4.6.1.
    >> I have few questions regarding -lm switch. Please kindly guide me for the same.
    >>
    >> 1) Why I need to use -lm flag for math.h even if I had declared the
    >> #include<math.h> in the code?

    >
    > #include<math.h> tells the compiler which library header to insert into
    > your program when compiling. -lm tells the linker which library to
    > search when linking your compiled program. A compiler could connect the
    > two, but most of the ones I'm familiar with don't.
    >
    >> 2) If it's all about linking the library then why I don't have to do
    >> the same thing with stdio.h too?

    >
    > By convention dating back to the early days of C, most of the C standard
    > library is often stored in one library file, except for the part
    > described by<math.h>, which is stored in a different one. This approach
    > became conventional because, especially at that time, most programs made
    > no use of the math library, and adding all of those functions to the
    > linker's search would significantly slowed down linkage. This is far
    > less of an issue now than it used to be, but some habits, once started,
    > are hard to change.


    In at least one C implementation, the math functions have in
    fact been combined with the rest of the library and all are linked
    automatically with no need for -lm. This implementation also provides
    a completely empty "libm" library, just to accommodate Makefiles and
    so on that use -lm anyhow!

    --
    Eric Sosman
    d
    Eric Sosman, Jan 27, 2012
    #5
  6. Avinash Sonawane

    Kaz Kylheku Guest

    On 2012-01-26, James Kuyper <> wrote:
    > By convention dating back to the early days of C, most of the C standard
    > library is often stored in one library file, except for the part
    > described by <math.h>, which is stored in a different one. This approach
    > became conventional because, especially at that time, most programs made
    > no use of the math library, and adding all of those functions to the
    > linker's search would significantly slowed down linkage. This is far
    > less of an issue now than it used to be, but some habits, once started,
    > are hard to change.


    I suspect there is one more reason and it is related to the reason why there
    are separate /bin and /usr/bin directories as well as /lib and /usr/lib.

    Disks used to be small, and so programs had to be separated into essential
    (for booting the system) and nonessential.

    If programs linked the math library by default, then the math library would
    have to have been placed in the root file system (so that the utilities which
    run early in the boot process could execute).

    (The issue of what executable material to include in a minimal filesystem for
    booting is still relevant.)
    Kaz Kylheku, Jan 27, 2012
    #6
  7. Avinash Sonawane

    Joe Pfeiffer Guest

    Avinash Sonawane <> writes:

    > Sir,
    > I am using gcc 4.6.1.
    > I have few questions regarding -lm switch. Please kindly guide me for the same.
    >
    > 1) Why I need to use -lm flag for math.h even if I had declared the
    > #include <math.h> in the code?


    There are two distinct phases here: #include <math.h> tells the
    compiler what the match functions look like, -lm actually links in the
    code for them.

    > 2) If it's all about linking the library then why I don't have to do
    > the same thing with stdio.h too?


    Because that's *so* standard it's just assumed. More specifically,
    there is a "standard C library" which is automatically linked with your
    code, and all of the functions from <stdio.h> are in that library.

    The better question would be why the C standard library function
    prototypes aren't all automatically available, without a bunch of
    #include's. To me it seems like they ought to be, but the people
    writing the standards and the people writing the compilers don't agree
    with me, so they aren't.

    > 3) Or we have to add the -l'library' switch for every library
    > excluding stdio.h?


    You've got a little bit of confusion going on here, which I've alluded
    to above but I'll say more specifically here: a .h file is not a
    library. A .h file contains function prototypes, which tell the
    compiler how to generate code to make calls to the functions. That has
    nothing at all to do with the code that's in the actual library --
    that's what the -l flag pulls in.

    OK, when I say it has "nothing to do with" that's overstating things a
    bit: assuming no bugs, the .h file is designed to make sure calls to
    the library are actually correct. But aside from that intention, they
    are unrelated.

    > 4) In case if the -l'library' switch is used for some specific
    > libraries only then please direct me to the complete list of such
    > libraries.


    There is no complete list. Any particular system will have its own set
    of libraries, depending on what's been installed on it. If you want,
    you can create a library of your own that exists only on your own
    system, and which won't be in any list.

    If you're on a Unix-based system (including Linux and BSD), the man
    page for a function will tell you both what .h file you need to #include
    to compile it, and what library you need to use to link it. So, for
    instance, the man page for the sin function says:

    #include <math.h>

    double sin(double x);
    float sinf(float x);
    long double sinl(long double x);

    Link with -lm.

    So, to be able to use sin(), sinf(), or sinl(), you need to
    #include <math.h> and link with -lm
    Joe Pfeiffer, Jan 27, 2012
    #7
  8. Avinash Sonawane

    jacob navia Guest

    Le 27/01/12 07:09, Joe Pfeiffer a écrit :
    >
    > The better question would be why the C standard library function
    > prototypes aren't all automatically available, without a bunch of
    > #include's. To me it seems like they ought to be, but the people
    > writing the standards and the people writing the compilers don't agree
    > with me, so they aren't.
    >


    The lcc-win compiler provides the "stdheaders.h" include file
    that includes all the standard headers sparing you the need to figure
    out what function is in what header.

    I proposed that in comp.std.c years ago, but it was ignored in the
    new standard.
    jacob navia, Jan 27, 2012
    #8
  9. In article <>,
    Joe Pfeiffer <> wrote:
    ....
    >If you're on a Unix-based system (including Linux and BSD), the man
    >page for a function will tell you both what .h file you need to #include
    >to compile it, and what library you need to use to link it. So, for
    >instance, the man page for the sin function says:


    This isn't necessarily true (and, in true CLC fashion, I'm going to go to
    great pains to explain why your statement isn't true in absolute
    generality). All versions of MS/PC DOS/Windows, since DOS 2.0, are
    Unix-based, yet, when I type "man {anything}" on my Windows Server 2008 R2
    Home Premium Super Deluxe (*), all I get is some dumb error message.

    (*) With wheels and a sandwich...

    --
    Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
    an 8 bit operating system based on a 4 bit architecture from a 2 bit company
    that can't stand 1 bit of competition.

    Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
    tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
    operating system based on a 4 bit architecture from a 2 bit company that
    can't stand 1 bit of competition.
    Kenny McCormack, Jan 27, 2012
    #9
  10. Avinash Sonawane

    Kleuske Guest

    OT:Re: About -lm switch used for linking math.h

    On Fri, 27 Jan 2012 09:48:22 +0000, Kenny McCormack saw fit to publish the
    following:

    > In article <>, Joe Pfeiffer
    > <> wrote: ...
    >>If you're on a Unix-based system (including Linux and BSD), the man page
    >>for a function will tell you both what .h file you need to #include to
    >>compile it, and what library you need to use to link it. So, for
    >>instance, the man page for the sin function says:

    >
    > This isn't necessarily true (and, in true CLC fashion, I'm going to go
    > to great pains to explain why your statement isn't true in absolute
    > generality). All versions of MS/PC DOS/Windows, since DOS 2.0, are
    > Unix-based, yet, when I type "man {anything}" on my Windows Server 2008
    > R2 Home Premium Super Deluxe (*), all I get is some dumb error message.
    >
    > (*) With wheels and a sandwich...


    In what universe is MS-DOS "unix-based?".

    --
    There's a way out of any cage.
    -- Captain Christopher Pike, "The Menagerie" ("The Cage"),
    stardate unknown.
    Kleuske, Jan 27, 2012
    #10
  11. On Thu, 26 Jan 2012 23:09:23 -0700
    Joe Pfeiffer <> wrote:

    > The better question would be why the C standard library function
    > prototypes aren't all automatically available, without a bunch of
    > #include's. To me it seems like they ought to be, but the people
    > writing the standards and the people writing the compilers don't agree
    > with me, so they aren't.


    Because you may be able to compile and link without the C standard
    library functions being available. Like when boostrapping the standard
    library or implementing a operating system kernel, where the standard
    library functions simply won't work. All compilers/linkers offer a
    option, not to link against the standard library.


    Wolfgang
    Wolfgang.Draxinger, Jan 27, 2012
    #11
  12. Avinash Sonawane

    BartC Guest

    "Wolfgang.Draxinger" <-muenchen.de> wrote in
    message news:-muenchen.de...
    > On Thu, 26 Jan 2012 23:09:23 -0700
    > Joe Pfeiffer <> wrote:
    >
    >> The better question would be why the C standard library function
    >> prototypes aren't all automatically available, without a bunch of
    >> #include's. To me it seems like they ought to be, but the people
    >> writing the standards and the people writing the compilers don't agree
    >> with me, so they aren't.


    I suppose it's simple enough to bundle all the standard headers into one big
    header, and to just include that.

    That's saves having the ridiculous situation of having to keep deleting or
    adding include files, everything time you edit some code and the number of
    references to something in a standard header, falls below 1, or rises above
    zero. How do you keep track of something like that anyway? So you just keep
    a whole bunch of headers in, whether they are essential or not.

    The only problem is that an all-inclusive header file is not standardised
    (or is it?) making it more difficult to share code.

    > Because you may be able to compile and link without the C standard
    > library functions being available. Like when boostrapping the standard
    > library or implementing a operating system kernel, where the standard
    > library functions simply won't work.


    Those are special cases, ordinary programmers shouldn't be burdened with
    maintaining lists of include files just so 1% of all programmers have the
    option of leaving them out!

    Actually it would be simplest for the compiler to just include everything
    necessary (to be able to write C code), and for a compiler switch to say
    headers are included manually.

    > All compilers/linkers offer a
    > option, not to link against the standard library.


    So why not a switch to *not* include the maths library, rather than the
    other way around? Is the maths library actually that big? And would it be
    statically linked in?

    --
    Bartc
    BartC, Jan 27, 2012
    #12
  13. "BartC" <> writes:

    > "Wolfgang.Draxinger" <-muenchen.de> wrote in
    > message news:-muenchen.de...
    >> On Thu, 26 Jan 2012 23:09:23 -0700
    >> Joe Pfeiffer <> wrote:
    >>
    >>> The better question would be why the C standard library function
    >>> prototypes aren't all automatically available, without a bunch of
    >>> #include's. To me it seems like they ought to be, but the people
    >>> writing the standards and the people writing the compilers don't agree
    >>> with me, so they aren't.

    >
    > I suppose it's simple enough to bundle all the standard headers into one big
    > header, and to just include that.

    <snip>
    > The only problem is that an all-inclusive header file is not standardised
    > (or is it?) making it more difficult to share code.


    But since the list of standard headers is standardised, you could
    distribute a .h file that includes them all along with the code you want
    to share.

    >> Because you may be able to compile and link without the C standard
    >> library functions being available. Like when boostrapping the standard
    >> library or implementing a operating system kernel, where the standard
    >> library functions simply won't work.

    >
    > Those are special cases, ordinary programmers shouldn't be burdened
    > with maintaining lists of include files just so 1% of all programmers
    > have the option of leaving them out!
    >
    > Actually it would be simplest for the compiler to just include everything
    > necessary (to be able to write C code), and for a compiler switch to say
    > headers are included manually.


    Some environments let you make such a compiler in a few minutes. gcc
    has a -include command-line option (and you can read options from a file
    with @file) so an alias like "gcc -std=c99 -pedantic @std-includes" does
    what you want.

    <snip>
    --
    Ben.
    Ben Bacarisse, Jan 27, 2012
    #13
  14. Re: OT:Re: About -lm switch used for linking math.h

    In article <jfttgn$s37$>, Kleuske <> wrote:
    >On Fri, 27 Jan 2012 09:48:22 +0000, Kenny McCormack saw fit to publish the
    >following:
    >
    >> In article <>, Joe Pfeiffer
    >> <> wrote: ...
    >>>If you're on a Unix-based system (including Linux and BSD), the man page
    >>>for a function will tell you both what .h file you need to #include to
    >>>compile it, and what library you need to use to link it. So, for
    >>>instance, the man page for the sin function says:

    >>
    >> This isn't necessarily true (and, in true CLC fashion, I'm going to go
    >> to great pains to explain why your statement isn't true in absolute
    >> generality). All versions of MS/PC DOS/Windows, since DOS 2.0, are
    >> Unix-based, yet, when I type "man {anything}" on my Windows Server 2008
    >> R2 Home Premium Super Deluxe (*), all I get is some dumb error message.
    >>
    >> (*) With wheels and a sandwich...

    >
    >In what universe is MS-DOS "unix-based?".


    This one. Re-read what I wrote. It is well understood that as of
    version 2 of DOS - i.e., DOS 2.0 - DOS was based on Unix - the hierarchical
    file system, the general compilation model, etc. If necessary, re-read your
    history books. And, of course, all versions of DOS/Windows since then share
    the legacy.

    Note: Obviously, I'm playing games with the word "based" - as in
    "Unix-based". The previous poster was trying to be politically correct by
    avoiding saying that Linux is Unix - weaseling out by saying that Linux is
    merely "Unix-based". Well, the point is that that wording allows in
    DOS/Windows, which is clearly not Unix, but it is equally clearly
    "Unix-based".

    And that's the way of CLC...

    --
    But the Bush apologists hope that you won't remember all that. And they
    also have a theory, which I've been hearing more and more - namely,
    that President Obama, though not yet in office or even elected, caused the
    2008 slump. You see, people were worried in advance about his future
    policies, and that's what caused the economy to tank. Seriously.

    (Paul Krugman - Addicted to Bush)
    Kenny McCormack, Jan 27, 2012
    #14
  15. Avinash Sonawane

    tom st denis Guest

    Re: OT:Re: About -lm switch used for linking math.h

    On Jan 27, 9:43 am, (Kenny McCormack)
    wrote:
    > In article <jfttgn$>, Kleuske  <>wrote:
    > >On Fri, 27 Jan 2012 09:48:22 +0000, Kenny McCormack saw fit to publish the
    > >following:

    >
    > >> In article <>, Joe Pfeiffer
    > >> <> wrote: ...
    > >>>If you're on a Unix-based system (including Linux and BSD), the man page
    > >>>for a function will tell you both what .h file you need to #include to
    > >>>compile it, and what library you need to use to link it.  So, for
    > >>>instance, the man page for the sin function says:

    >
    > >> This isn't necessarily true (and, in true CLC fashion, I'm going to go
    > >> to great pains to explain why your statement isn't true in absolute
    > >> generality).  All versions of MS/PC DOS/Windows, since DOS 2.0, are
    > >> Unix-based, yet, when I type "man {anything}" on my Windows Server 2008
    > >> R2 Home Premium Super Deluxe (*), all I get is some dumb error message..

    >
    > >> (*) With wheels and a sandwich...

    >
    > >In what universe is MS-DOS "unix-based?".

    >
    > This one.  Re-read what I wrote.  It is well understood that as of
    > version 2 of DOS - i.e., DOS 2.0 - DOS was based on Unix - the hierarchical
    > file system, the general compilation model, etc.  If necessary, re-readyour
    > history books.  And, of course, all versions of DOS/Windows since then share
    > the legacy.


    Actually DOS is CP/M based ... which wasn't UNIX based.

    > Note: Obviously, I'm playing games with the word "based" - as in
    > "Unix-based".  The previous poster was trying to be politically correctby
    > avoiding saying that Linux is Unix - weaseling out by saying that Linux is
    > merely "Unix-based".  Well, the point is that that wording allows in
    > DOS/Windows, which is clearly not Unix, but it is equally clearly
    > "Unix-based".


    Linux isn't UNIX based either, it's based off of MINIX which is a
    variant OS that is similar to UNIX.

    So not only are your posts tiresome and boring but they're wholesale
    factually incorrect even when you are trying to be a smarty pants.

    Tom
    tom st denis, Jan 27, 2012
    #15
  16. Re: OT:Re: About -lm switch used for linking math.h

    In article <>,
    tom st denis <> blubbered:
    ....
    >So not only are your posts tiresome and boring but they're wholesale
    >factually incorrect even when you are trying to be a smarty pants.


    It all depends on what the meaning of the word "based" is.

    --

    Some of the more common characteristics of Asperger syndrome include:

    * Inability to think in abstract ways (eg: puns, jokes, sarcasm, etc)
    * Difficulties in empathising with others
    * Problems with understanding another person's point of view
    * Hampered conversational ability
    * Problems with controlling feelings such as anger, depression
    and anxiety
    * Adherence to routines and schedules, and stress if expected routine
    is disrupted
    * Inability to manage appropriate social conduct
    * Delayed understanding of sexual codes of conduct
    * A narrow field of interests. For example a person with Asperger
    syndrome may focus on learning all there is to know about
    baseball statistics, politics or television shows.
    * Anger and aggression when things do not happen as they want
    * Sensitivity to criticism
    * Eccentricity
    * Behaviour varies from mildly unusual to quite aggressive
    and difficult
    Kenny McCormack, Jan 27, 2012
    #16
  17. Avinash Sonawane

    Joe Pfeiffer Guest

    Re: OT:Re: About -lm switch used for linking math.h

    (Kenny McCormack) writes:

    > In article <jfttgn$s37$>, Kleuske <> wrote:
    >>On Fri, 27 Jan 2012 09:48:22 +0000, Kenny McCormack saw fit to publish the
    >>following:
    >>
    >>> In article <>, Joe Pfeiffer
    >>> <> wrote: ...
    >>>>If you're on a Unix-based system (including Linux and BSD), the man page
    >>>>for a function will tell you both what .h file you need to #include to
    >>>>compile it, and what library you need to use to link it. So, for
    >>>>instance, the man page for the sin function says:
    >>>
    >>> This isn't necessarily true (and, in true CLC fashion, I'm going to go
    >>> to great pains to explain why your statement isn't true in absolute
    >>> generality). All versions of MS/PC DOS/Windows, since DOS 2.0, are
    >>> Unix-based, yet, when I type "man {anything}" on my Windows Server 2008
    >>> R2 Home Premium Super Deluxe (*), all I get is some dumb error message.
    >>>
    >>> (*) With wheels and a sandwich...

    >>
    >>In what universe is MS-DOS "unix-based?".

    >
    > This one. Re-read what I wrote. It is well understood that as of
    > version 2 of DOS - i.e., DOS 2.0 - DOS was based on Unix - the hierarchical
    > file system, the general compilation model, etc. If necessary, re-read your
    > history books. And, of course, all versions of DOS/Windows since then share
    > the legacy.
    >
    > Note: Obviously, I'm playing games with the word "based" - as in
    > "Unix-based". The previous poster was trying to be politically correct by
    > avoiding saying that Linux is Unix - weaseling out by saying that Linux is
    > merely "Unix-based". Well, the point is that that wording allows in
    > DOS/Windows, which is clearly not Unix, but it is equally clearly
    > "Unix-based".
    >
    > And that's the way of CLC...


    You're using a definition of "Unix-based" that is completely foreign to
    just about everybody.
    Joe Pfeiffer, Jan 27, 2012
    #17
  18. Avinash Sonawane

    Rui Maciel Guest

    Kenny McCormack wrote:

    > All versions of MS/PC DOS/Windows, since DOS 2.0, are
    > Unix-based


    Do you actually have any basis for that assertion?


    Rui Maciel
    Rui Maciel, Jan 27, 2012
    #18
  19. On 26-Jan-12 15:59, Avinash Sonawane wrote:
    > 1) Why I need to use -lm flag for math.h even if I had declared the
    > #include <math.h> in the code?


    You are confusing libraries and headers. A library, eg. "libm",
    contains the code for the function you want to call; A header file, eg.
    "math.h", contains the declaration of the function you want to call.

    You include a header file so the compiler knows about the function you
    want to call. However, you must separately tell the linker to include
    the library with the function itself; the compiler won't tell the linker
    that for you.

    (In programs with a single source file, you may not see the distinction
    between compiler and linker, but it's there--and understanding this is
    very important when you have multiple source files.)

    > 2) If it's all about linking the library then why I don't have to do
    > the same thing with stdio.h too?


    Most linkers are set up so that they automatically link your code
    against one or more libraries by default, eg. "libc". However, you
    still need to tell the compiler about the functions you want to call by
    including the relevant header file, eg. "stdio.h".

    > 3) Or we have to add the -l'library' switch for every library excluding
    > stdio.h?


    For every library that your linker doesn't include by default. Check
    your linker's documentation.

    > 4) In case if the -l'library' switch is used for some specific
    > libraries only then please direct me to the complete list of such
    > libraries.


    There is no complete list of libraries, nor could there be since new
    ones are created every day. However, the documentation for the library
    functions you use should specify what, if any, library you should link to.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Jan 27, 2012
    #19
  20. In article <jful0l$d2v$>,
    Rui Maciel <> wrote:
    >Kenny McCormack wrote:
    >
    >> All versions of MS/PC DOS/Windows, since DOS 2.0, are
    >> Unix-based

    >
    >Do you actually have any basis for that assertion?
    >
    >
    >Rui Maciel


    I do not mean "based" in the sense of "code based" - i.e., that they took
    Unix code to build DOS. Only a moronic pedant would think that - which is
    why we are seeing so much of it in this thread. See: "straw man".

    I mean "based" in the sense of "ideas borrowed from". And I mean borrowed
    rather heavily - not just a smidgen here and there. As I said, read your
    history. DOS 1 was, as Tommy notes, basically a quick hack-up of CP/M, but
    DOS 2 introduced a lot of Unixy stuff and made DOS quite Unix-like. And the
    whole point is we are all better off because of it. It made DOS (and its
    later progeny) much better systems.

    Anyway, I've made my point. I'm done.

    --
    Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
    an 8 bit operating system based on a 4 bit architecture from a 2 bit company
    that can't stand 1 bit of competition.

    Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
    tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
    operating system based on a 4 bit architecture from a 2 bit company that
    can't stand 1 bit of competition.
    Kenny McCormack, Jan 27, 2012
    #20
    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. chirs
    Replies:
    18
    Views:
    748
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,070
    Jonathan Turkanis
    Apr 1, 2004
  3. Mark Healey
    Replies:
    7
    Views:
    1,455
    Tim Prince
    May 22, 2006
  4. Philipp
    Replies:
    9
    Views:
    1,090
    Mark Space
    Jul 23, 2008
  5. VK
    Replies:
    15
    Views:
    1,117
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page