473,406 Members | 2,705 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,406 software developers and data experts.

Build-in sqrt()?


hello everyone,

why this c code can be compiled and linked without -lm?

#include<math.h>
#include<stdio.h>
int main()
{
printf("%f\n",sqrt(2.0));
return 0;
}

even NO sqrt symbol when i use nm to list symbols from the object file

someone can give me a hint? i'm using a gcc-4.1.2 with glibc2.4.1 under
linux

best regards

Nov 27 '06 #1
14 4741
pe*********@gmail.com wrote:
hello everyone,

why this c code can be compiled and linked without -lm?

#include<math.h>
#include<stdio.h>
int main()
{
printf("%f\n",sqrt(2.0));
return 0;
}

even NO sqrt symbol when i use nm to list symbols from the object file

someone can give me a hint? i'm using a gcc-4.1.2 with glibc2.4.1 under
linux
The actual details of creating executable code from C source is
implementation dependant and the C standard says nothing about it.
Under UNIX it is traditionally the case that the math library is a
seperate file and hence has to be specifically included at link time. A
specific implementation may well include the math functions along with
the rest of the standard C library.

Standard C, and this group, really has nothing to say about your
observation.

Nov 27 '06 #2
pe*********@gmail.com wrote:
why this c code can be compiled and linked without -lm?

#include<math.h>
#include<stdio.h>
int main()
{
printf("%f\n",sqrt(2.0));
return 0;
}

even NO sqrt symbol when i use nm to list symbols from the object file

someone can give me a hint? i'm using a gcc-4.1.2 with glibc2.4.1 under
linux
First read your handy docs, e.g.
% info gcc
then ask specific further questions on
gc******@gcc.gnu.org
If you are interested, check your generated code to see whether sqrt()
was evaluated at compile time, or at run time using a built-in CPU
instruction.
Nov 27 '06 #3
On 26 Nov 2006 20:05:09 -0800, "pe*********@gmail.com"
<pe*********@gmail.comwrote in comp.lang.c:
>
hello everyone,

why this c code can be compiled and linked without -lm?
What's "-lm"? It's nothing defined by the C language. If it's
something that you think your implementation needs, you should be
asking in a support group for your implementation.
#include<math.h>
#include<stdio.h>
int main()
{
printf("%f\n",sqrt(2.0));
return 0;
}

even NO sqrt symbol when i use nm to list symbols from the object file
The C standard names no mention of "nm", or what it is supposed to do.
Again, a platform specific group is appropriate and could probably
provide more useful information.
someone can give me a hint? i'm using a gcc-4.1.2 with glibc2.4.1 under
linux

best regards
Among other possibilities, the as-if rule allows the implementation to
replace the call of a standard library function with a constant value
with its known result. It could merely pass
1.4142135623730950488016887242097, or perhaps some shorter version
thereof, to printf() without calling sqrt() at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 27 '06 #4
Hello Peng,
hello everyone,

why this c code can be compiled and linked without -lm?

#include<math.h>
#include<stdio.h>
int main()
{
printf("%f\n",sqrt(2.0));
return 0;
}

even NO sqrt symbol when i use nm to list symbols from the object file

someone can give me a hint? i'm using a gcc-4.1.2 with glibc2.4.1 under
linux
This is a out of topic for a NG like comp.lang.c... The effect your are
seeing here is simply that gcc is smart enough to evaluate sqrt(2.0) at
*compile time*. gcc literally replace:

printf("%f\n", sqrt(2.0))
by:
printf("%f\n", 1.41421356237309504880)

If you would have for instance a value passed to sqrt() that can be
only determined at runtime, then you would need to link against the
math library using '-lm' as expected.

Cheers,
Loic.

Nov 27 '06 #5

On 11ÔÂ27ÈÕ, ÏÂÎç12ʱ22·Ö, "santosh" <santosh....@gmail.com>
wrote:
peng.xia...@gmail.com wrote:
hello everyone,
why this c code can be compiled and linked without -lm?
#include<math.h>
#include<stdio.h>
int main()
{
printf("%f\n",sqrt(2.0));
return 0;
}
even NO sqrt symbol when i use nm to list symbols from the object file
someone can give me a hint? i'm using a gcc-4.1.2 with glibc2.4.1 under
linuxThe actual details of creating executable code from C source is
implementation dependant and the C standard says nothing about it.
Under UNIX it is traditionally the case that the math library is a
seperate file and hence has to be specifically included at link time. A
specific implementation may well include the math functions along with
the rest of the standard C library.

Standard C, and this group, really has nothing to say about your
observation.
Sorry for my asking such a question in comp.lang.c. I post the problem
here because I doubt whether strange behaviour only caused by the
compiler and not something related to linker. So i ask is there a
build-in sqrt() in the topic.

Thank you all the same

Nov 27 '06 #6
pe*********@gmail.com said:
>
hello everyone,

why this c code can be compiled and linked without -lm?
Why does it matter? If it can be compiled and linked without -lm, you should
be rejoicing, not complaining?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 27 '06 #7

Jack Klein wrote:
On 26 Nov 2006 20:05:09 -0800, "pe*********@gmail.com"
<pe*********@gmail.comwrote in comp.lang.c:

hello everyone,

why this c code can be compiled and linked without -lm?

What's "-lm"? It's nothing defined by the C language. If it's
something that you think your implementation needs, you should be
asking in a support group for your implementation.
....siding with Kevin McCormack here...

What possible good does your reply serve? Some dude asks about libm.a
and a half dozen people reply with "OMG THAT'S NOT IN THE
STANDARDS!!!!!111eleven!!!!"

Why not just not reply if all your going to do is prove your inability
to answer a fucking question.

For the record, at least with GCC 4.1.1 on my Fedora box the test
program

#include <math.h>
double f(double y) { return sqrt(y); }

Will have "U sqrt" in the nm output. If you add "-ffast-math" it may
not (in my case it used the SSE2 instruction "sqrtsd" to perform the
operation) be present.

Why you didn't see it could be because it's been inlined (intrinsic).
However, you should link libm.a in for platforms/build settings where
that is not the case.

Wow, was that really so hard?

Tom

Nov 27 '06 #8
Tom St Denis said:
>
Jack Klein wrote:
>On 26 Nov 2006 20:05:09 -0800, "pe*********@gmail.com"
<pe*********@gmail.comwrote in comp.lang.c:
>
hello everyone,

why this c code can be compiled and linked without -lm?

What's "-lm"? It's nothing defined by the C language. If it's
something that you think your implementation needs, you should be
asking in a support group for your implementation.

...siding with Kevin McCormack here...
Way to go, Tom - why not just fry your credibility and serve it with chips?
What possible good does your reply serve?
It points the OP to an appropriate newsgroup, and manages to do so without
using expletives.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 27 '06 #9
Richard Heathfield wrote:
It points the OP to an appropriate newsgroup, and manages to do so without
using expletives.
Well you guys need to co-ordinate. One day you can be the
anal-retentive netcop and the next day someone else could be.

That way we avoid the deluge of 12:1 replies-to-post for all off-topic
posts.

Note: I agree, that clc should remain topical. It's HOW you guys try
to achieve this that pisses me off (mostly because it means wading
through a lot of noise to find any useful conversation).

If something someone is posting is not to your liking, just don't
reply. If nobody replied to off-topic posts they'd take the hint and
look elsewhere for the answers. Now, if someone stops and says "where
can I find more info about $X" that's diff. But if they say "I have a
bug with $X" and $X isn't in the clc charter, just ignore the mofo and
let them sort it out on their own (and shame on them for not reading
the clc FAQ).

And frankly, replying to the dude takes less effort then "correcting
his net posting habits." So you can both say

1. Dude, you're in the wrong group.

2. While I'm at it, here's your answer.

And if the dude persists to post OT junk then killfile the mofo. but I
suspect for most after you tell them of their erroneous posting habits
they'll adjust.

/rant

Tom

Nov 27 '06 #10

Tom St Denis wrote:

[snippage]
>
And frankly, replying to the dude takes less effort then "correcting
his net posting habits." So you can both say

1. Dude, you're in the wrong group.

2. While I'm at it, here's your answer.
Isn't that *exactly* what Jack did? If not in those exact words?

Nov 27 '06 #11
Tom St Denis wrote:
Jack Klein wrote:
><pe*********@gmail.comwrote in comp.lang.c:
>>why this c code can be compiled and linked without -lm?

What's "-lm"? It's nothing defined by the C language. If it's
something that you think your implementation needs, you should be
asking in a support group for your implementation.

...siding with Kevin McCormack here...

What possible good does your reply serve? Some dude asks about
libm.a and a half dozen people reply with "OMG THAT'S NOT IN THE
STANDARDS!!!!!111eleven!!!!"
It tells the OP where to go for a definitive answer. It also tells
him that the problem has nothing to do with the C language as
discussed here. It also refrains from cluttering the newsgroup
with unchecked advice that may or may not apply to the OPs system.
With any luck it will avoid him cluttering the newsgroup further
with off-topic queries. It SHOULD have discouraged you from
cluttering the newsgroup with off-topic answers.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 27 '06 #12

John Bode wrote:
Tom St Denis wrote:

[snippage]

And frankly, replying to the dude takes less effort then "correcting
his net posting habits." So you can both say

1. Dude, you're in the wrong group.

2. While I'm at it, here's your answer.

Isn't that *exactly* what Jack did? If not in those exact words?
Yes, sorry, he did.

I guess the rheotical "the C language doesn't define nm or -lm" bit is
what sent me off...

My bad.

Sorry Jack.

Tom

Nov 27 '06 #13
pe*********@gmail.com wrote:
hello everyone,

why this c code can be compiled and linked without -lm?

#include<math.h>
#include<stdio.h>
int main()
{
printf("%f\n",sqrt(2.0));
return 0;
}

even NO sqrt symbol when i use nm to list symbols from the object file

someone can give me a hint? i'm using a gcc-4.1.2 with glibc2.4.1 under
linux
The compiler will optimise your program. It evaluates the sqrt at
compile time, without needing to link to the math library. This is
allowed under the "as if" rule in the C standard.

Your program is peng1.c:

C:\docs\prog\c>type peng1.c
#include <stdio.h>

int main(void)
{
printf("%f\n", sqrt(2.0));
return 0;
}

My equivalent program is peng2.c:

C:\docs\prog\c>type peng2.c
#include <stdio.h>

int main(void)
{
printf("%f\n", 1.41421356237309505);
return 0;
}

I compile each of the programs to assembly language:

C:\docs\prog\c>gcc -S peng1.c

C:\docs\prog\c>gcc -S peng2.c

Now I look at the difference between the generated assembly:

C:\docs\prog\c>diff peng1.s peng2.s
1c1
< .file "peng1.c"
---
.file "peng2.c"
The only difference between the two files is the line indicating which
..c file they were compiled from.

The relevant parts of the assembly that GCC emits on my system are as
follows, with a gloss into pseudo-C:

LC1:
.ascii "%f\12\0" /* char *LC1 = "%f\n"; */
.align 8
LC0:
.long 1719614413 /* long LC0[2] = {1719614413, */
.long 1073127582 /* 1073127582}; */
....
fldl LC0 /* double reg = *(double*)LC0; */
fstpl 4(%esp) /* memcpy(esp+4, &reg, 8); */
movl $LC1, (%esp) /* memcpy(esp, LC1, 4); */
call _printf /* printf(); */
movl $0, %eax /* eax = 0; */
leave
ret /* return eax; */

On a system where longs are 32 bits in 2's complement, stored in
little-endian order in four 8-bit bytes, and where doubles are 64 bits
in IEEE-754 format, stored in little-endian order in eight 8-bit bytes,
then the array {1719614413, 1073127582} has the same bit pattern as the
constant 1.41421356237309505.

The original constant 2.0 and the call to sqrt are entirely absent as
they have been optimised away.

--
Simon.
Nov 27 '06 #14
"Tom St Denis" <to********@gmail.comwrites:
Richard Heathfield wrote:
>It points the OP to an appropriate newsgroup, and manages to do so without
using expletives.

Well you guys need to co-ordinate. One day you can be the
anal-retentive netcop and the next day someone else could be.

That way we avoid the deluge of 12:1 replies-to-post for all off-topic
posts.

Note: I agree, that clc should remain topical. It's HOW you guys try
to achieve this that pisses me off (mostly because it means wading
through a lot of noise to find any useful conversation).

If something someone is posting is not to your liking, just don't
reply. If nobody replied to off-topic posts they'd take the hint and
look elsewhere for the answers. Now, if someone stops and says "where
can I find more info about $X" that's diff. But if they say "I have a
bug with $X" and $X isn't in the clc charter, just ignore the mofo and
let them sort it out on their own (and shame on them for not reading
the clc FAQ).

And frankly, replying to the dude takes less effort then "correcting
his net posting habits." So you can both say

1. Dude, you're in the wrong group.

2. While I'm at it, here's your answer.

And if the dude persists to post OT junk then killfile the mofo. but I
suspect for most after you tell them of their erroneous posting habits
they'll adjust.
Sure, if *nobody* replied to off-topic posts, there would likely be
fewer of them. But in an unmoderated newsgroup, that's difficult or
impossible to achieve. We aren't even all that successful in ignoring
deliberate trolls.

There's always somebody who thinks he knows the answer and thinks the
best thing to do is to post it here. There's no way to censor such
postings (nor should there be), so the best we can do is try to
educate people about how this newsgroup should work. Since new people
show up all the time, we can't do it just once.

The "While I'm at it, here's your answer" approach is very tempting
(and I probably do it myself sometimes), but it's difficult to post
*anything* here without starting a lengthy discussion. I might post a
solution that's specific to gcc, or to Unix, or whatever. Somebody
else will post a correction. The correction might be incorrect or
incomplete, and I or somebody else will say so. And so on. The final
result is that the newsgroup becomes swamped, and a lot of people get
the idea that this is the place to go for answers about gcc, or Unix,
or Windows. Whereas if I posted my Unix-specific advice to
comp.unix.programmer, the Unix experts there would converge on a
correct answer much more quickly and less painfully, leaving us here
to discuss standard C.

Though I didn't witness it myself, I understand that this is exactly
what happened to comp.lang.c++ a few years ago, and that newsgroup
became nearly useless for those who actually wanted to discuss the C++
programming language. It has since recovered.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 28 '06 #15

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

Similar topics

6
by: Steve Holden | last post by:
Does anyone know the workaround for this error, encountered when trying to build PIL 1.1.4 from source under cygwin (Python 2.3.3)? running build_ext building '_imaging' extension gcc...
8
by: ted | last post by:
How does the speed of the Scons build tool compare with Ant? Right now with out Ant builds take around an hour. Hoping to speed that up. TIA, Ted
0
by: Scott Auge | last post by:
I created this script to help make zip files for software distributions. It will: -- Create a build number -- Create a VERSION file so people know which build they have -- Update the...
2
by: Felix | last post by:
Out of no where I'm starting to get these problems and my Deployment projects won't build anymore. Does anyone have any idea what's causing this? I've tried copying these files from another...
0
by: greg | last post by:
I created deployment project for my web application (to install on the same computer so I dont need to add runtime) Added project output AND content files would not build content...
0
by: Andrew Ayre | last post by:
Hi, I can't seem to get the library built, and any help is greatly appreciated. Here is the info: Windows XP Borland C++ Builder 5 Latest Boost source code (downloaded at the weekend) Windows...
3
by: michael.lang | last post by:
I am using PostBuild events in a C# project to run some tasks, but only when in release configuration mode. The build events run perfectly as expected when run. The problem comes when I save the...
1
by: Ted Zeng | last post by:
Hi, I am new to Python. I am trying to build MySQLdb for my PowerPC PowerMac. I just downloaded the source last week and tried to build it. But I got the error messages as follow. I checked and...
1
by: John Dalberg | last post by:
The "Build Web Site" build option seems to do a complete build even when no changes have been made. Is there a way to do a build only when a source file has changed? This is a web site app. John...
0
by: Jack | last post by:
Since I don't have VS2003, I'm trying to build cjson with MingW and Cygwin but I'm getting lots of errors like these: build\temp.win32-2.5\Release\cjson.o(.text+0x8e):cjson.c: undefined...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.