Compiling in Linux - GCC

This is a discussion on Compiling in Linux - GCC within the C Programming forums, part of the General Programming Boards category; from what I heard, in order to use #include <math.h> you need to use -lm when u compile in linux? ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Compiling in Linux - GCC

    from what I heard, in order to use #include <math.h> you need to use -lm when u compile in linux?

    gcc -c -lm conversion.c


    I still get an error saying

    : undefined reference to `pow'
    collect2: ld returned 1 exit status

    I also tried

    gcc -c conversion.c -lm and still get the same msg
    Last edited by gqchynaboy; 03-01-2005 at 07:23 PM.

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    http://lists.blu.org/pipermail/discu...ry/028406.html

    Do you have libm.so somewhere to be included?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    I still can't get it to work, is their anyway to get the fuction to work without using POW

    Code:
      {
        int length;
        int i;
        int total;
        int numval;
        int multiplier;
        
        length = strlen(string);
        total = 0;
        for ( i=length-1; i>=0; i-- )
    
    
            {
            numval = (int)string[i] - 48;
            multiplier = (int)pow((double)10,(double)((length - 1) - i));
            total = total + ( numval * multiplier );
        }
        
        return total;
    }

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    are you including math.h?
    try to #include<math.h> on the begining of the source.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by Maragato
    are you including math.h?
    try to #include<math.h> on the begining of the source.
    yea alread tried that..

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Ive found it on the net:

    Building: Why am I getting error about an "undefined reference to
    `pow'"?
    On some systems, to link to libpng you also need to link to libm. That should be guessed correctly
    for most systems by configure, but if you're getting this error, the following call to configure should
    help:
    LDFLAGS="-lm" ./configure

    Hope it helps.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    How can I make this line work without using the pow function?

    multiplier = (int)pow((double)10,(double)((length - 1) - i));

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You could always just make your own pow() function...it's like a 5-line function.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    why don't you write your own pow function and call it.
    Last edited by caroundw5h; 03-02-2005 at 10:24 AM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by gqchynaboy
    gcc -c -lm conversion.c


    I still get an error saying

    : undefined reference to `pow'
    collect2: ld returned 1 exit status

    I also tried

    gcc -c conversion.c -lm and still get the same msg

    Then something is dreadfully wrong somewhere in your installation.

    "gcc -c" invokes the compiler on a given file and stops without linking. The error that you report is a linker error. That isn't logical.

    Of course, you can easily make a function that raises a number to an integer power without anything in <math.h>, but what about the next time you need a math function?

    What Linux distribution are you using? Is it a new installation, or have you been using it for a while? Some distributions like Redhat or Fedora let you install something less than complete libraries (if you did a "workstation" install rather than "developer"). That still doesn't explain how you got a linker error when your command line explicitly didn't invoke the linker.

    Regards,

    Dave
    Last edited by Dave Evans; 03-01-2005 at 09:17 PM.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    have you tried to link the libs?

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Like the person above me mentioned, the linker error is throwing me for a loop. But I have noticed pow isn't in math.h any more, at least not on mine, Slackware current updated to ~10.1 barring a couple things, its in tgmath.h for me. Check by greping for pow( in /usr/include and see what comes up. I used pow\( so bash didn't error out though.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    I just created my own Power function ...

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Well Ive googled about tgmath.h here is what Ive found
    http://www.opengroup.org/onlinepubs/.../tgmath.h.html
    But acording to the links provided in http://www.tldp.org pow belongs to math.h in fact tgmath.h is based on math.h + another lib.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    gcc -c -lm conversion.c
    -c compile only (this doesn't generate an executable)
    -lm link with the math library - but there is no code yet, you didn't compile anything
    conversion.c - now you compile it.

    gcc -c conversion.c -lm
    Better options order, but it's still compile only.

    Try
    gcc conversion.c -lm
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Compiling 1394 Development Libraries for Linux
    By Phanixis in forum Linux Programming
    Replies: 6
    Last Post: 10-05-2007, 10:48 AM
  2. Linux kernel not compiling
    By frenchfry164 in forum Tech Board
    Replies: 2
    Last Post: 04-29-2003, 04:10 PM
  3. Cross Compiling with Linux and Turbo C
    By ozgulker in forum Linux Programming
    Replies: 0
    Last Post: 11-18-2002, 02:07 PM
  4. programming linux with gcc, emacs, and makefiles
    By Captain Penguin in forum Linux Programming
    Replies: 1
    Last Post: 11-02-2002, 11:04 AM

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