Find the answer to your Linux question:
Results 1 to 10 of 10
When I try to compile the following program, the gcc complains that: main.c .text+0x35): undefined reference to `sqrt' collect2: ld returned 1 exit status What's the problem? I am using ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    16

    Question undefined reference to `sqrt'

    When I try to compile the following program, the gcc complains that:

    main.c.text+0x35): undefined reference to `sqrt'
    collect2: ld returned 1 exit status

    What's the problem? I am using Fedora 8 by the way.


    #include <stdio.h>
    #include <math.h>

    int main(void)
    {

    double x = 10;

    double y;

    y = sqrt(x);

    return 0;

    }

    Thanks for any help!

  2. #2
    Just Joined!
    Join Date
    Mar 2008
    Posts
    16
    I've checked that the math.h file is in the following folder:
    /usr/include

    But why cann't gcc find it?

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    16
    I need to add the "-lm" option for the gcc

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    -lm option

    For some reason I have to do this as well(add the -lm option to the gcc compile line)

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Correct. You need to add "-lm" to gcc.

    Most of the C Standard Library exists in a library called libc.so. Math functions, however, exist in libm.so. gcc will automatically link to libc: any other linking must be specified. To tell it what library to link to, you remove the '.so' from the end and the 'lib' from the beginning. Therefore, "-lm" means "link against libm.so".

    I hope this makes sense.
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    Math library

    Isn't the math header supposed to be part of the standard library, according to the ansi standard?

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Both the header and the library are considered a part of the C Standard Library. However, this is not a compiletime error ("I don't know what sqrt is"). This is a linktime error ("I can't find the precompiled sqrt function").

    I admittedly don't know the history behind this decision, or what the ANSI standard says about it, but that's the way it is. If you find out why it is this way, I'd be interested.
    DISTRO=Arch
    Registered Linux User #388732

  8. #8
    Just Joined!
    Join Date
    Mar 2008
    Posts
    16
    Thanks for the useful information, Cabban.

    I just knew that one still needs to add "-lm" to Eclipse CDT linker (gcc) options.

    Quote Originally Posted by Cabhan View Post
    Correct. You need to add "-lm" to gcc.

    Most of the C Standard Library exists in a library called libc.so. Math functions, however, exist in libm.so. gcc will automatically link to libc: any other linking must be specified. To tell it what library to link to, you remove the '.so' from the end and the 'lib' from the beginning. Therefore, "-lm" means "link against libm.so".

    I hope this makes sense.

  9. #9
    Just Joined!
    Join Date
    Feb 2008
    Posts
    1

    I have some issue about anything similar to '-lm' ?

    So, -lm for math.h,but if these is anything similar to math.h,where can I know to add it for gcc

  10. #10
    Just Joined!
    Join Date
    Sep 2008
    Location
    Silicon Forest, Oregon
    Posts
    4
    Old info, but still useful. I just ran into the same problem. Adding m to the libraries in Eclipse solved the problem. Thanks.

Posting Permissions

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