LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2006, 10:04 PM   #1
joeymac
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Rep: Reputation: 0
gcc-4 anomaly (sqrt function)



I am experiencing a strange problem with gcc-4.0.1 under Mandriva-2006.0. I am trying to generate a file of small primes to use in a project. The relevant code is shown below:


#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[])
{
FILE *output;
int i, j, limit, lowLimit, *theArray;
//...(snip)
lowLimit = (int) sqrt(limit) + 1;
//...(snip)
}

If I compile to object (using gcc -c), the code compiles without error. However, if I attempt to compile to an executable (using gcc -o ...), I get the following diagnostic:

/home/jmcdow/tmp/ccCBoony.o: In function `main':
findLowPrimes.c.text+0x74): undefined reference to `sqrt'
collect2: ld returned 1 exit status

So I check /usr/include/math.h and, sure enough, there is no prototype for sqrt(); yet, if I replace the limit variable with a constant value, no error occurs!

Could anyone please give me some insight on whats up here? I get lost on the gcc forums.



 
Old 01-02-2006, 10:43 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
If you are missing a prototype, like didn't include a header, the error you'll get is "warning: implicit declaration of function `sqrt'".. so that's not the problem.
If your error is "undefined reference" this means the function is probably prototyped but not defined.

sqrt() is actually defined in a library called the math library, and you have to explicitly link with the math library by puting "-lm" on the gcc command line.
 
Old 01-02-2006, 11:45 PM   #3
joeymac
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks a bunch--solved the problem.

I've been using gcc/g++ for years and never knew that standard libraries needed to be explicitly linked.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc: recursive function quirk Disruptor Linux - Software 4 09-23-2014 05:45 AM
sqrt function lmvent Programming 6 10-20-2005 07:53 PM
red hat 9.0 gcc (ceil, floor and sqrt function errors) ?? gemini_shooter Linux - Enterprise 1 06-04-2005 04:23 PM
Can't get a simple C function to work with GCC chris.hicks Programming 3 10-29-2003 11:09 AM
sqrt() function IamDaniel Programming 6 08-09-2003 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration