Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About -lm switch used for linking math.h

Reply
Thread Tools

About -lm switch used for linking math.h

 
 
Avinash Sonawane
Guest
Posts: n/a
 
      01-26-2012
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-26-2012
Avinash Sonawane <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      01-26-2012
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.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-26-2012
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.


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-27-2012
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
(E-Mail Removed)d
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-27-2012
On 2012-01-26, James Kuyper <(E-Mail Removed)> 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.)
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      01-27-2012
Avinash Sonawane <(E-Mail Removed)> 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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-27-2012
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.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-27-2012
In article <(E-Mail Removed)>,
Joe Pfeiffer <(E-Mail Removed)> 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.
 
Reply With Quote
 
Kleuske
Guest
Posts: n/a
 
      01-27-2012
On Fri, 27 Jan 2012 09:48:22 +0000, Kenny McCormack saw fit to publish the
following:

> In article <(E-Mail Removed)>, Joe Pfeiffer
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linking 2 VLANs on one switch rezalas Hardware 3 05-17-2008 04:23 AM
Switch/Switch problem fibre gigbit ethernet hartmut.albers@gmail.com Cisco 2 09-06-2005 04:59 PM
Is a frame switch and an ISDN switch really needed? owmanstubbedmytoe Cisco 2 12-05-2004 07:15 AM
bridge / layer 2 switch / layer 3 switch Joel M. Baldwin Cisco 2 11-06-2003 11:19 PM
difference b/w layer 2 switch and layer 3 switch praveen Cisco 1 10-22-2003 07:19 AM



Advertisments