LinuxQuestions.org
Visit Jeremy's Blog.
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 02-12-2004, 05:54 AM   #1
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Rep: Reputation: 15
Problem with math.h


I should be bashed to death for not being able to do this ...I just started learning C, I am using gcc 3.2.3 on a linux box. This is the code I wrote, which finds prime numbers below 100:
Code:
#include <stdio.h>
#include <math.h>


main ()
{
int i, j;
printf ("%d\n", 2);

for (i=3; i<100; i=i+1)
	{
	for (j=2; j<i;j=j+1)
		{
		if (i%j ==0)
			break;
		if (j > sqrt(i))
			{
			printf ("%d\n");
			break;
			}
		}
	}
return 0;
}
And this is the error msg I get after attempting to compile:
Code:
bash-2.05b$ gcc primo.c -o primo
/tmp/ccZdYpje.o(.text+0x65): In function `main':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status
bash-2.05b$
Isn`t sqrt defined in math.h??

Thanks
 
Old 02-12-2004, 06:13 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Use gcc -lm -o myprog myprog.c

The "-lm" invokes linking of the math library.
 
Old 02-12-2004, 06:22 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Problem with math.h

Quote:
Originally posted by loke137
Isn`t sqrt defined in math.h??
Well, yes, it is. But in case of the math library, you also need to link with the math library. It's very understandable that you, as a beginner, didn't know this as for most standard things you #include there's no need for linking with seperate libraries.

Assuming you c-file is called "prime.c", and you want the executable be called "prime", compile with this command line
Code:
gcc -lm -o prime prime.c
"-lm" makes sure the math library is linked.

You'll also see another error, because you forgot to pass the variable i to the printf statement. Change the line to:
Code:
    printf ("%d\n", i);
Also, it's better to declare main() as "int main()". If you don't specify "int" as the return type of main(), you'll get a warning. Not a big deal though, it's only warning, not an error.

Compile with this to make the compiler report all possible warnings:
Code:
gcc -Wall -pedantic -lm -o prime prime.c
 
Old 02-12-2004, 06:38 AM   #4
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Original Poster
Rep: Reputation: 15
Thank you...so it was a problem about compiler knowledge
 
Old 02-12-2004, 07:12 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
To be pedantically precise: it was a problem about linking knowledge :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
math.h Damicles Programming 9 12-04-2010 05:51 AM
Java Problem with Math and angles caged Programming 2 09-08-2005 01:12 AM
Java Math.round problem need help FAST! plz :) Vizy Programming 4 01-08-2005 05:14 PM
Java Math.random and Tables problem Vizy Programming 4 12-27-2004 11:27 AM
bash and math division problem bennethos Programming 5 10-17-2004 01:51 PM

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

All times are GMT -5. The time now is 03:27 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