* Newbie help please
@ 2003-09-26 3:22 Albert van Aardt
2003-09-26 3:41 ` Felix Lee
0 siblings, 1 reply; 3+ messages in thread
From: Albert van Aardt @ 2003-09-26 3:22 UTC (permalink / raw)
To: gcc-help
I am trying to compile a simple C program:
#include "stdio.h"
#include "math.h"
int number;
main()
{
printf("Which number do you wish to start the test with? ");
scanf("%d",&number);
printf("\n");
printf("I'm starting with number %d\n",number);
if (number<1) number=1;
if (number==1) printf("1\n");
if (number<=2) printf("2\n");
if (!(number % 2)) number++;
if (number==1) number=3;
do
{
if (isprime(number)) printf("%d\n",number);
number+=2;
}while (1) ;
}
isprime(int thenumber)
{
int isitprime=1,loop;
for(loop=3 ; (isitprime) && (loop<(sqrt(thenumber)+1)) ; loop+=2)
isitprime = (thenumber % loop);
return(isitprime);
}
Compile it:
gcc prime.c
but then I get this error:
/tmp/ccdHcjqd.o(.text+0x128): In function `isprime':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status
I have also tried some other programs, and all seem to give this "sqrt" error.
Clearly I am doing something stupid, but surely GCC can handle square roots?
Please advise!
(RedHat 9)
--
Albert & Sue van Aardt
Whangarei
New Zealand
Linux rules!
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: Newbie help please
@ 2003-09-26 4:35 Rakesh Kumar - Software, Noida
0 siblings, 0 replies; 3+ messages in thread
From: Rakesh Kumar - Software, Noida @ 2003-09-26 4:35 UTC (permalink / raw)
To: Albert van Aardt, gcc-help
It seems you hadn't linked the math library. Try -lm switch. For example
"gcc prime.c -lm"
Regards
Rakesh
> -----Original Message-----
> From: Albert van Aardt [mailto:avanaardt@free.net.nz]
> Sent: Friday, September 26, 2003 8:50 AM
> To: gcc-help@gcc.gnu.org
> Subject: Newbie help please
>
>
> I am trying to compile a simple C program:
>
> #include "stdio.h"
> #include "math.h"
>
> int number;
>
> main()
> {
> printf("Which number do you wish to start the test with? ");
> scanf("%d",&number);
> printf("\n");
> printf("I'm starting with number %d\n",number);
> if (number<1) number=1;
> if (number==1) printf("1\n");
> if (number<=2) printf("2\n");
> if (!(number % 2)) number++;
> if (number==1) number=3;
> do
> {
> if (isprime(number)) printf("%d\n",number);
> number+=2;
> }while (1) ;
> }
>
> isprime(int thenumber)
> {
> int isitprime=1,loop;
> for(loop=3 ; (isitprime) &&
> (loop<(sqrt(thenumber)+1)) ; loop+=2)
> isitprime = (thenumber % loop);
> return(isitprime);
> }
>
>
>
>
> Compile it:
>
> gcc prime.c
>
> but then I get this error:
>
>
> /tmp/ccdHcjqd.o(.text+0x128): In function `isprime':
> : undefined reference to `sqrt'
> collect2: ld returned 1 exit status
>
>
> I have also tried some other programs, and all seem to give
> this "sqrt" error.
> Clearly I am doing something stupid, but surely GCC can
> handle square roots?
>
>
> Please advise!
>
> (RedHat 9)
>
> --
>
>
> Albert & Sue van Aardt
> Whangarei
> New Zealand
>
> Linux rules!
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-09-26 4:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-26 3:22 Newbie help please Albert van Aardt
2003-09-26 3:41 ` Felix Lee
2003-09-26 4:35 Rakesh Kumar - Software, Noida
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).