This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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!


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]