ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.