473,395 Members | 1,456 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,395 software developers and data experts.

Standard C and math library

I have a quick question about the math library included in the standard
C90 (and 99).

The gcc, xlc and possibly other compilers/linkers on some unix
platforms cannot use any math functions in math.h if a switch (-lm) is
used, so *explicitly* having the linker use a file called libm.so or
libm.a/dylib/whatever.

Is it true on all the platforms? (answer: probably no)

Is this a break on the standard?

Since abs(), sqrt() and all the math.h defined funcions/types ARE
(correct me if I make a mistake) put into the ISO C, any compiler and
any linker should NOT use an external library other than the standard C
library for those funcions. Is it so? Is the libc defined in the
standard having also math.h? If not, the separation seems to say ``math
is not in the standard c library, you must use something from outside''.

--
Sensei <se******@tin.it>

cd /pub
more beer

Nov 15 '05 #1
8 2898
Sensei wrote:
I have a quick question about the math library included in the standard
C90 (and 99).

The gcc, xlc and possibly other compilers/linkers on some unix platforms
cannot use any math functions in math.h if a switch (-lm) is used, so
*explicitly* having the linker use a file called libm.so or
libm.a/dylib/whatever.

Is it true on all the platforms? (answer: probably no)

Is this a break on the standard?
No, it's not -- specifically because the standard says nothing about how
programs are compiled or linked.
Since abs(), sqrt() and all the math.h defined funcions/types ARE
(correct me if I make a mistake) put into the ISO C, any compiler and
any linker should NOT use an external library other than the standard C
library for those funcions. Is it so? Is the libc defined in the
standard having also math.h? If not, the separation seems to say ``math
is not in the standard c library, you must use something from outside''.


No, the math functions *are* in the standard library -- but, again,
since the magic words used for compilation and linking are external to
the standard, it doesn't matter.

[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.

Periodically, a plea is made to make the standard library monolithic on
*nix systems -- and perhaps one day it will occur.]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 15 '05 #2
In article <3j************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:

[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.


It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place. It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.

But sometimes, "-lm" meant not to just link in fp libraries, but
to take an extra pass over the code and do some inlining of
hand-optimized assembly to replace certain fp operations, depending
on what stuff your fpu actually did in hardware vs. what your
program called for. The extra pass could actually take up some
cpu time and back then we didn't have a lot to spare.
--
7842++
Nov 15 '05 #3
Anonymous 7843 wrote:
In article <3j************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:
[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.

It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place. It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.


Not so strange, really. The linker that needed to make
a pass over a great big barrel of functions that weren't used
required time to do so, and the time to pass over and ignore
all those math functions (count them; they are, one might say,
numer-ous) was noticeable. If you argue against separable
math libraries in 197x, you argue against precompiled headers
today -- it's a matter of scale, nothing more.

--
Er*********@sun.com
Nov 15 '05 #4
In article <J8********************@comcast.com> Eric Sosman <es*****@acm-dot-org.invalid> writes:
....
Not so strange, really. The linker that needed to make
a pass over a great big barrel of functions that weren't used
required time to do so, and the time to pass over and ignore
all those math functions (count them; they are, one might say,
numer-ous) was noticeable.


Not really. Back in the early 70s I used a CDC Cyber for my work.
Most common was *not* to prelink before execution. It was on
execution that the linker came into play and created the binary
actually used. Linking from the compiled (relocatable) binary
to an executable took no noticable time. Consider a Fortran source
consisting of a few hundred subroutines. When compiling, each
routine was compiled on its own, relocatable, and whatever. When
executing you just entered LGO and linking and whatever began.
You could do all that before execution (and make the equivalent of
an EXE), but in most cases it was not worth the trouble. Linking
took no noticable time. For me it was really a culture shock when
I shifted to Unix. It took really extremely much time just to link.
(And to put it in perspective, that CDC machine had 65536 60-bits
words of memory, and served about 100 interactive users. The
interactive response was not worse than what I am getting at my
workstation.)
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #5
>> [In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.
It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place. It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.


The tricks get cuter than that. For example, some implementations
have two copies of printf(), one with floating point support and
one without it. The one with floating point support would drag in
other internal functions that wouldn't be used without floating
point support (for example, the routines that formatted floating-point
numbers as text). All this was based on implementor-known order
of loading functions in case there were more than one with the same
name. I believe at least one required use of -lm to get the version
of printf() which used floating point.
But sometimes, "-lm" meant not to just link in fp libraries, but
to take an extra pass over the code and do some inlining of
hand-optimized assembly to replace certain fp operations, depending
on what stuff your fpu actually did in hardware vs. what your
program called for. The extra pass could actually take up some
cpu time and back then we didn't have a lot to spare.


Gordon L. Burditt
Nov 15 '05 #6
Sensei wrote:

I have a quick question about the math library included in the
standard C90 (and 99).

The gcc, xlc and possibly other compilers/linkers on some unix
platforms cannot use any math functions in math.h if a switch (-lm)
is used, so *explicitly* having the linker use a file called
libm.so or libm.a/dylib/whatever.

Is it true on all the platforms? (answer: probably no)

Is this a break on the standard?

Since abs(), sqrt() and all the math.h defined funcions/types ARE
(correct me if I make a mistake) put into the ISO C, any compiler
and any linker should NOT use an external library other than the
standard C library for those funcions. Is it so? Is the libc
defined in the standard having also math.h? If not, the separation
seems to say ``math is not in the standard c library, you must use
something from outside''.


The standard says what facilities are to be available, and what
include files to specify. It DOES NOT specify a mechanism for
running the system, such as "pull lever A, wait for a ball to pop
out of slot B, then throw toggle switch C". For that you have to
look to your compiler/system documentation, and anything goes.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #7
On 2005-07-12 19:15:04 -0500, Artie Gold <ar*******@austin.rr.com> said:
No, it's not -- specifically because the standard says nothing about
how programs are compiled or linked.
Ah! Ok.
No, the math functions *are* in the standard library -- but, again,
since the magic words used for compilation and linking are external to
the standard, it doesn't matter.
I see.
[In a lot of ways, it's a strange historical wart; I suspect you don't
remember the days of external floating point coprocessors and such. In
addition, unlike today where it's mostly embedded systems (and
particularly memory constrained ones at that) where memory footprint
matters at that level of granularity, it once *did* matter --
particularly at the time the convention was adopted.
Well, I've grown under C64 and Prodest, I had a 286 without 287
coprocessor... I know history in this case :) Anyway the standard C99
is far too young to be affected by math coprocessors and DX/SX
distinction. That's why I'm puzzled.
odically, a plea is made to make the standard library monolithic on
*nix systems -- and perhaps one day it will occur.]


One thing I thought is that using floating point library when not
needed would be a ``waste of space''... even though is just some kb...

--
Sensei <se******@tin.it>

cd /pub
more beer

Nov 15 '05 #8
On Wed, 13 Jul 2005 01:18:35 GMT, in comp.lang.c ,
an******@example.com (Anonymous 7843) wrote:
It was strange even then. Any static linker would strip out unused
math functions or simply skip them in the first place.
Euh, in the Olden Days, the time taken to run through and decide you
didn't need the stuff was appreciable. Imagine linking one of today's
apps on a 4.77Mhz chip.....
It was unlikely
that "-lm" would bloat your program if you didn't actually use any
floating point stuff.


Some compiler suites were especially braindead and included all
functions in Library X, whether or not you used them. Some still do
(try this with MFC sometime...) Its a QOI issue.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #9

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

Similar topics

2
by: F. Petitjean | last post by:
I have written a script to find the modules which export the largest number of names. The gc.getreferrers(*objs) function gives also an idea of the dependencies between the modules. The code...
16
by: Vadim Biktashev | last post by:
Hello all I would like to give a certain name to a certain global variable. Unfortunately, this name is already used in math.h for a mathematical function. Worse, I do need to use maths library...
15
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
12
by: theoderich | last post by:
Following is possible with gcc and g++: #include <math.h> double sin(double) { return 1; } int main()
6
by: junky_fellow | last post by:
On what basis it is decided that a particular funtion should be a part of Standard C library ? Do I get these standard C libraries along with the C compiler ? Or I can use the c complier from...
5
by: Martin Vilcans | last post by:
Hi, I'm new to this mailing list and fairly new to Python as well. I'm working on a prototype for a 3D game using OpenGL, and take this opportunity to learn Python better. I'm looking for a good...
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
39
by: Jim Showalter | last post by:
Does a hosted implementation require any particular libraries in order to be considered a standard C compiler? -- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ _/...
14
by: artifact.one | last post by:
It'd be really pleasant (in my opinion) if the next revision of the C language actually allowed some portable control over data alignment. Compiler-specific mechanisms for this stuff are so...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.