The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
some math problems in C cdfd123 High Level Programming 4 08-19-2007 04:39 PM
Math calculation help sickboy Shell Programming and Scripting 4 06-11-2005 10:22 AM
math.h not working? o.0 primal High Level Programming 2 03-24-2002 06:23 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-09-2006
Registered User
 

Join Date: Mar 2006
Posts: 11
something about <math.h>

Hi, I got an easy problem for you but really difficult for me 'cause I am pretty new to this field
I got header file <math.h> included in my .c file , then I write the code as below:

k = sqrt(i); /* both variables k and i are int */

then I cc temp.c
it says like this
undefined symbol first referenced in file
sqrt temp.o

it really makes me confused, so any one would like to give me a hand?
thanks a lot in avance!
Reply With Quote
Forum Sponsor
  #2  
Old 03-09-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Try:
cc -lm temp.c

The sqrt routine is in the math library. So you need to search the math library to build the executable program.
Reply With Quote
  #3  
Old 03-09-2006
Registered User
 

Join Date: Mar 2006
Posts: 11
thanks for your suggestion,and I tried as you told me,but it remained the same error,,,
i386ld fatal: Symbol referencing errors. no output written to a.out

would you offer me more details ,thank you.
Reply With Quote
  #4  
Old 03-09-2006
botao's Avatar
Registered User
 

Join Date: Mar 2006
Location: brazil
Posts: 9
Cool fix the type and the compilation environment

### first : the sqrt function requires a "double" parameter , and returns a double .

just like this :

/* ---- snip ---- */

double x = 2.0 ;
double y ;

y = sqrt (x) ;

/* ---- snip ---- */

### second : make sure the math library will be found :

$ find / -name libm.a

(it normally will be found in /lib or /usr/lib )

$ echo $LIBPATH or $LD_LIBRARY_PATH (depending on your o.s.) and
make sure the parent dir that contains libm.a is included in it .

### and finally , compile you program as follows :

$ cc -o yourprog yourprog.c -lm

(put the '-lm' at the end of the line)

good luck , and success !
Reply With Quote
  #5  
Old 03-09-2006
Registered User
 

Join Date: Mar 2006
Posts: 11
thank you all.

it works by typing "cc temp.c -lm"

-lm should be added at the end of the line.

and I think the type sqrt(1) returns can be changed into int forcefully, so it isn't the right problem lying there.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0