Find the answer to your Linux question:
Results 1 to 7 of 7
Hey..im writing a simple application using the pow() function on linux.. the problem is that considering the following code: int x = 2; double z = pow(x,2); this works perfectly ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60

    pow() function problem

    Hey..im writing a simple application using the pow() function on linux..
    the problem is that considering the following code:
    int x = 2;
    double z = pow(x,2);
    this works perfectly fine..compiles and runs normally on both windows and linux..

    considering this code however:
    int x = 2;
    double z = pow(2,x);
    this creates an error: "/tmp/ccy3HUjD.o: In function `main':
    powers_one.c.text+0x309): undefined reference to `pow'
    collect2: ld returned 1 exit status"

    BUT IT WORKS ON WINDOWS!...any idea whats the problem?

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Did you use the -lm compile flag?

    Regards

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    Quote Originally Posted by Franklin52 View Post
    Did you use the -lm compile flag?

    Regards
    im kinda new to linux...what do you mean?

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    The standard C library math functions are not included in libc so you have to compile your source with the -lm flag as follow:

    Code:
    gcc powers_one.c -o powers_one -lm
    Regards

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    Quote Originally Posted by Franklin52 View Post
    The standard C library math functions are not included in libc so you have to compile your source with the -lm flag as follow:

    Code:
    gcc powers_one.c -o powers_one -lm
    Regards
    ya it worked...thanks..but can u tell me what the -lm does exactly?

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    There are several mathematical functions (e.g. sin, cos, sqrt, exp, log, etc.) provided in the maths library /usr/lib/libm.a.
    The math library is separate from the regular C library so you need to explicitly link in the math library, using the -lm flag.

    Regards

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    More specifically, "-lSTRING" finds the library that is named STRING and uses the definitions described in it. You need to do this whenever you use a library that is not libc (the standard C library).

    So for instance, I was doing some work with Ogg Vorbis files, and I was using some functions defined in libvorbis. Therefore, when I compiled, I had to add "-lvorbis" to the gcc call.

    Franklin's explanation explains why you need this for the math functions.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...