Thread: sqrt() what am I doing wrong?

  1. #1
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266

    sqrt() what am I doing wrong?

    I'm getting this error message from gcc when trying to compile my code...

    # gcc project7.c
    /tmp/cc4pGFvN.o(.text+0xbb): In function `isPrime':
    : undefined reference to `sqrt'
    collect2: ld returned 1 exit status


    I can't find anything wrong with the source...at least nothing obvious.
    It doesn't seem to like the sqrt() function, yet as you can see I've included the math.h library.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int isPrime ( int number );
    
    int main()
    {
    	int primeFound = 0;
    	int number;
    	
    	for ( number = 3 ; number <= 10000 ; number += 2 )
    	{
    		if ( isPrime ( number ) )
    		{
    			primeFound++;
    			printf ("%7d" , number);
    			if ( primeFound % 5 == 0 )
    				printf("\n");
    			
    		}
    	}
    	
    	printf ("\nPrimes found %d\n" , primeFound);
    	return 0;
    }
    
    int isPrime ( int number )
    {
    	int n;
    	
    	for ( n = 3 ; n <= sqrt(number) ; n += 2 )
    	{
    		if ( number % n == 0 )
    			return 0;
    	}
    	
    	return 1;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Link to the math library by passing -lm to the linker.
    gcc -lm project7.c

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And if that doesn't work, try a different order
    gcc project7.c -lm

    The position and order of libraries is important.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Doh! Thanks for the advice, but I found using g++ instead of gcc fixes the problem.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Scribbler
    Doh! Thanks for the advice, but I found using g++ instead of gcc fixes the problem.
    Using g++ as a workaround is a poor solution to the problem. It can lead to weird thing happening in your programs or produce incorrect warning/error messages. You should use gcc to compile C programs and g++ to compile C++ programs.

    Adding -lm to the compile command is the correct way to fix your problem.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  2. Sqrt() Function
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 04-22-2003, 04:31 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM