GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Oct-2006, 18:05
jlwelch jlwelch is offline
New Member
 
Join Date: Oct 2006
Posts: 13
jlwelch is on a distinguished road
Question

The sqrt function


Hello all, everyone's favorite n00b is back with more n0obish questions! This one is actually quite simple, I promise. In short, my C textbook claims that the function "sqrt" is contained within the library math.h so I included it along with stdio.h and stdlib.h in my code. However, whenever I try to use the sqrt function in my code I frequently get the message:

"/tmp/ccG8aDU0.o(.text+0x3d3): In function `GetLen1':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status"

~Note: GetLen1 is the function containing my sqrt line~

Is there something else I am supposed to do before I can use the sqrt command?
  #2  
Old 10-Oct-2006, 18:35
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: The sqrt function


Sounds like you need to link with the library containing sqrt (namely math.lib or libmath.a).
  #3  
Old 10-Oct-2006, 18:38
jlwelch jlwelch is offline
New Member
 
Join Date: Oct 2006
Posts: 13
jlwelch is on a distinguished road

Re: The sqrt function


I have the line:

"#include <math.h>"

in my code already, specifically how would I write an introduction for those others you have suggested? Would it simply be:

"#include <math.lib>"

?
  #4  
Old 10-Oct-2006, 18:43
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: The sqrt function


No, it's going to be some option in your compiler. I would guess from the error that you're using GCC, but do you use the command line or some IDE? On the command line it would be adding an argument to the gcc execution, in an IDE it would depend on the IDE.
  #5  
Old 10-Oct-2006, 18:50
jlwelch jlwelch is offline
New Member
 
Join Date: Oct 2006
Posts: 13
jlwelch is on a distinguished road

Re: The sqrt function


I am sorry but I do not understand. When I compile I type:

"cc filename"

into the command line. I don't really know which compiler that is as I am new to the system, but that is it. Do you know how I can debug this program and make it recognize the sqrt function?
  #6  
Old 10-Oct-2006, 19:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: The sqrt function


Quote:
Originally Posted by jlwelch
I am sorry but I do not understand. When I compile I type:

"cc filename"

into the command line. I don't really know which compiler that is as I am new to the system, but that is it. Do you know how I can debug this program and make it recognize the sqrt function?

The command line to link in the math library is:

Code:
cc filename.c -lm

The "-lm" is an idiom, telling the compiler to pass information to the linker to use libm.a (the math library).

I would use something like

Code:
cc -Wall -pedantic -W filename.c -lm

This lets the compiler give you all of the warnings that it thinks are appropriate for common errors that people make that aren't actually violations of the language but are likely to be mistakes.

Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 11:33
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 08:48
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 13:02
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.