Click to See Complete Forum and Search --> : why won't this work? help please


Devil
04-01-2002, 11:16 AM
#include <stdio.h>
#include <math.h>

main()
{
int i;
int 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", i);
break;
}
}
}
return 0;
}


the error I get when I try to compile the code is:

/tmp/cczI2jIi.o: In function `main':
/tmp/cczI2jIi.o(.text+0x74): undefined reference to `sqrt'
collect2: ld returned 1 exit status


I don't understand why it wont compile..

bwkaz
04-01-2002, 11:46 AM
gcc -o test test.c -lm

You're probably missing the -lm part.

Devil
04-01-2002, 11:46 AM
ok i figured it out...

I had to put -lm at the end of the compile command.

I think it is to link it togther with the math.h library

Devil
04-01-2002, 11:47 AM
we posted att the same time.. lol