Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange behavior with math.h -> function log10 1

Status
Not open for further replies.

vjcyrano

Programmer
Dec 27, 2005
37
0
0
US
Cant understand this strange behavior with math.h.


<code>

#include<math.h>

int main()
{
double t = log10(10);
return 1;
}
</code>

works fine

but

<code>
#include <math.h>
int main()
{
double t1=10;
double t = log10(t1);
return 1;
}
</code>

gives the following error


log2.c:(.text+0x36): undefined reference to `log10'
collect2: ld returned 1 exit status



I am compiling this on linux platform using gcc.

 
I get the same behaviour ! Weird. I can't explain why that is, but I can give you a solution.

If you link to libm, then it works :

gcc bla.c -lm -o bla

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Please use [code][/code] tags when posting code.

A quick read of the manual (you read the manual didn't you?) reveals

gcc manual said:
-fno-builtin
-fno-builtin-function
Don't recognize built-in functions that do not begin with `__builtin_' as prefix. See Other built-in functions provided by GCC, for details of the functions affected, including those which are not built-in functions when -ansi or -std options for strict ISO C conformance are used because they do not have an ISO standard meaning.

...

The ISO C90 functions abort, abs, acos, asin, atan2, atan, calloc, ceil, cosh, cos, exit, exp, fabs, floor, fmod, fprintf, fputs, frexp, fscanf, isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit, tolower, toupper, labs, ldexp, log10

Clearly, if it's built-in, you don't need to link to the library.


--
 
Thanks sedj..
But I am not able to understand why it does that ..
 
Perhaps in the first example the compiler / optimiser is spotting the constant 10 and working out the answer at compile time instead of linking the function?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top