Jump to content

calculate Square root of a number PROBLEM

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Esi_Gold

Esi_Gold

    Newbie

  • Members
  • Pip
  • 2 posts
Dear Guys,
I just wrote a simple program to calculate Square root of a number, But when I try to compile it, I get this Error :


/tmp/ccmo2eCt.o: In function `main':

test01.c:(.text+0x59): undefined reference to `sqrt'

collect2: ld returned 1 exit status

Tha's the source Code :

/* Test1 : Test1.c 

   This program calculate root of the numbers */

#include <stdio.h>

#include <math.h>


int main()


{


   int c1;

   printf( "Enter your number\n" );

   scanf( "%d", &c1 );

   printf( "%.2f", sqrt( c1 ) );


return 0;

}

Could anybody help me?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
When you are compiling, you need to link the math library:

Quote

cc test.c -o test.o -lm
Adding the -lm should fix your problem.

#3
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Do like John suggested, and if you're not using GCC, you can check your compiler manual for information on how to link with libraries.

#4
Sree

Sree

    Newbie

  • Members
  • Pip
  • 1 posts
I hav run the same prog but not getting any errors.But I think it 'll bw solved if u declear a int variable seperately & assign the sqrt in it and write.But still interestec to know the cause of the problem.

#5
mholt

mholt

    Newbie

  • Members
  • PipPip
  • 27 posts
Regarding above: If you save the value returned by sqrt then save it to an int, you'll truncate it, meaning you'll lose precision and likely round down, which may not be what you intend...

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

Sree said:

I hav run the same prog but not getting any errors.But I think it 'll bw solved if u declear a int variable seperately & assign the sqrt in it and write.But still interestec to know the cause of the problem.

If you compiled it using an IDE, you won't reproduce the error because the IDE will link the math library automatically. However, if you try compiling the code above without the -lm, via the console, you should reproduce the error. At least I did...

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

Sree said:

I hav run the same prog but not getting any errors.But I think it 'll bw solved if u declear a int variable seperately & assign the sqrt in it and write.But still interestec to know the cause of the problem.

If you declare an integer variable and assign the sqrt() value to it, it shouldn't make any difference than if you embed the function call within the output method. sqrt() returns a numeric value, so if you concatenate it within the output string directly, it should work in the same way. It does make it slightly harder to debug, though, as you can't tell whether the sqrt() call or the output call has generated the error.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
Esi_Gold

Esi_Gold

    Newbie

  • Members
  • Pip
  • 2 posts
Dear Guys, I compiled the source code using -lm , It worked perfect!
Thanks all

Edited by Esi_Gold, 05 May 2008 - 06:15 AM.


#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
No probs. :)
I love the green font, by the way...
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums