math.h

This is a discussion on math.h within the C Programming forums, part of the General Programming Boards category; i'm trying to use the log() function, and i've included math.h, but i still get this error on compilation: Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    9

    math.h

    i'm trying to use the log() function, and i've included math.h, but i still get this error on compilation:

    Code:
    util.c:15: undefined reference to `log'
    someone please inform me of what i'm doing wrong so that i can bang my head against the wall for being stupid.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    you need to compile against the math library. in gcc add -lm to the end of your linker call.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Please post the code of util.c so we can see your problem. Why would you need to add -lm to the end of your linker call? For example this works fine with gcc without needing any lm switch.

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main(void)
    {
    	double x = log(10);
    
    	printf("%f\n", x);
    
    	return 0;
    }

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I've noticed that in my current version of gcc (3.3.5-20050130), you don't need to specify -lm to link against the math libraries, but in older versions, I believe that it is required. Thus, if you plan to distribute the source and a makefile, it's a good idea to include -lm just for backwards compatibility.

    Edit: I just tested on my university's servers, and with gcc 3.3.4 (and likely earlier), you need to explicitly link to the math library.
    Last edited by XSquared; 08-04-2005 at 08:47 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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