math.h

This is a discussion on math.h within the C Programming forums, part of the General Programming Boards category; Code: # include <stdio.h> # include <stdlib.h> # include <math.h> int main () { double k; double x; printf ("Enter ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    Question math.h

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <math.h>
    int main ()
    {
            double k;
            double x;
            printf ("Enter a number ... \n");
            scanf ("%f", &k);
            x = sqrt (k);
    //      x = cos (k);
            printf ("Square root is %f\n", x);
            return 0;
                                                                                    
    }
    Error:
    /tmp/cc2E1F0F.o(.text+0x3e): In function `main':
    : undefined reference to `sqrt'
    collect2: ld returned 1 exit status

    I am working in linux platform and i do not why do i get this error. I think non of the math.h functions are not working. But I included math.h. any suggestions?

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    I have no problem in compiling your code, I'm using codeblocks with mingw - windows xp. Just a suggestion you must use %lf for the conversion specifier of double.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Dial "m" for linking to the math library that contains the definition of sqrt().
    Explicitly specify that you need to link in the math lib at compile time, as in
    Code:
    cc -lm file.c

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by itCbitC View Post
    Code:
    cc -lm file.c
    While you are at it, I would suggest that you turn on compiler warnings:

    Code:
    gcc -Wall -W -pedantic yourprogam.c -o yourprogram -lm
    You might see some helpful stuff like
    Code:
    yourprogram.c: In function ‘main’:
    yourprogram.c:9: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    Watchtower's suggestion tells you how to fix it, but I think it's nice that the compiler can give you a hint that there's a problem so you know where to look (in case Watchtower is not around when you wonder why you get the wrong answer from the program).

    D.
    Last edited by Dave Evans; 07-03-2009 at 02:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trig Functions without math.h
    By daltore in forum C Programming
    Replies: 13
    Last Post: 12-29-2008, 03:47 PM
  2. cosine series with out using math.h
    By indrajit_muk in forum C Programming
    Replies: 5
    Last Post: 12-16-2008, 07:17 PM
  3. undefined reference to `sqrt' when using math.h
    By Milhas in forum C Programming
    Replies: 4
    Last Post: 03-31-2008, 06:11 PM
  4. using round from math.h
    By axr0284 in forum C Programming
    Replies: 5
    Last Post: 05-02-2006, 11:23 AM
  5. math.h
    By falconetti in forum C Programming
    Replies: 2
    Last Post: 11-17-2001, 01:37 PM

Tags for this Thread


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