Thread: math.h

  1. #1
    Registered User
    Join Date
    Jul 2009

    Question math.h

    # 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;
    /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
    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
    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
    cc -lm file.c

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

    gcc -Wall -W -pedantic yourprogam.c -o yourprogram -lm
    You might see some helpful stuff like
    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).

    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, 04:47 PM
  2. cosine series with out using math.h
    By indrajit_muk in forum C Programming
    Replies: 5
    Last Post: 12-16-2008, 08: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, 02:37 PM

Tags for this Thread