"Undefined Reference" to pow and sqrt even though math.h header is there

This is a discussion on "Undefined Reference" to pow and sqrt even though math.h header is there within the C Programming forums, part of the General Programming Boards category; Compiling this code tells me that there is no reference to pow() or sqrt(). The book didn't tell me under ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    "Undefined Reference" to pow and sqrt even though math.h header is there

    Compiling this code tells me that there is no reference to pow() or sqrt(). The book didn't tell me under which header these functions are located but I assume they are under math.h. Am I missing something here?


    Code:
    #include <stdio.h>
    #include <math.h>
    
    double hypotenuse(double a, double b);
    
    int main()
    {
       double side1, side2;
    
       printf("Enter side 1: ");
       scanf("%lf", side1);
       printf("Enter side 2: ");
       scanf("%lf", side2);
       printf("%.2f\n", hypotenuse(side1, side2) );
    
       return 0;
    }
    
    double hypotenuse(double a, double b)
    {
       return sqrt( pow(a, 2) + pow(b, 2) );
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    If you're using gcc as your compiler, try adding -lm to the end of your compile line to link in the math library.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > scanf("%lf", side1);
    And don't forget the & either when using scanf

    gcc -W -Wall -ansi -pedantic -O2 foo.c
    Tells you when you make a mess of printf/scanf conversions (amongst other things)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    182
    That did it!

    What does the -lm argument do?

    Is there anyway to get cc to tell me if I made a mess(should I be using gcc instead of cc?)?

    Thanks guys!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    -l specifies an additional library to search
    m is the name of the library, in this case, its libm.a

    -lfoo always maps to libfoo.a on a Unix/Linux system.

    > should I be using gcc instead of cc?
    Maybe you already are - cc is mapped to gcc on my box.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    182
    ahh, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple double sqrt, pow
    By Tarento in forum C Programming
    Replies: 1
    Last Post: 06-01-2006, 01:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21