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

 
 
Wolfgang.Draxinger
Guest
Posts: n/a
 
      01-27-2012
On Thu, 26 Jan 2012 23:09:23 -0700
Joe Pfeiffer <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      01-27-2012


"Wolfgang.Draxinger" <(E-Mail Removed)-muenchen.de> wrote in
message news:(E-Mail Removed)-muenchen.de...
> On Thu, 26 Jan 2012 23:09:23 -0700
> Joe Pfeiffer <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-27-2012
"BartC" <(E-Mail Removed)> writes:

> "Wolfgang.Draxinger" <(E-Mail Removed)-muenchen.de> wrote in
> message news:(E-Mail Removed)-muenchen.de...
>> On Thu, 26 Jan 2012 23:09:23 -0700
>> Joe Pfeiffer <(E-Mail Removed)> 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.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-27-2012
In article <jfttgn$s37$(E-Mail Removed)>, Kleuske <(E-Mail Removed)> wrote:
>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?".


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)

 
Reply With Quote
 
tom st denis
Guest
Posts: n/a
 
      01-27-2012
On Jan 27, 9:43*am, (E-Mail Removed) (Kenny McCormack)
wrote:
> In article <jfttgn$(E-Mail Removed)>, Kleuske *<(E-Mail Removed)>wrote:
> >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?".

>
> 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
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-27-2012
In article <(E-Mail Removed)>,
tom st denis <(E-Mail Removed)> 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

 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      01-27-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) (Kenny McCormack) writes:

> In article <jfttgn$s37$(E-Mail Removed)>, Kleuske <(E-Mail Removed)> wrote:
>>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?".

>
> 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.
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      01-27-2012
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
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      01-27-2012
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
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-27-2012
In article <jful0l$d2v$(E-Mail Removed)>,
Rui Maciel <(E-Mail Removed)> 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.
 
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