trouble sqrt in gcc

Discussion in 'C Programming' started by Aric, Aug 26, 2003.

  1. Aric

    Aric Guest

    Hi,

    I'm a bit new to programming in general, really I've just picked it up
    as a hobby, and so I've started by reading "Practical C Programming"
    by Steve Oualline. Things have been going fine in the book until I
    reached exercise 6-1 in the book, which reads:

    Write a program to find the square of the distance between two points.
    (For a more advanced problem, find the actual distance. This problem
    involves using the standard function sqrt. Use your help system to
    find out more about how to use this function.)

    I did fine with the basic problem, but I can't seem to compile when I
    try to use sqrt. Here is my code:

    #include <stdio.h>
    #include <math.h>
    char coordinate[50]; /* input string */
    int myx1; /* first x coordinate */
    int myx2; /* second x coordinate */
    int myy1; /* first y coordinate */
    int myy2; /* second y coordinate */
    double sqrdist; /* distances square */
    double distance; /* actual distance between two points */
    int absxsquare; /* absolute value of (x1 + x2) ^2 */
    int absysquare; /* absolute value of (y1 + y2) ^2 */

    int main()
    {
    printf("Enter the X value of the first coordinate: ");
    fgets(coordinate, sizeof(coordinate), stdin);
    sscanf(coordinate, "%d", &myx1);

    printf("Enter the Y value of the first coordinate: ");
    fgets(coordinate, sizeof(coordinate), stdin);
    sscanf(coordinate, "%d", &myy1);

    printf("Enter the X value of the second coordinate: ");
    fgets(coordinate, sizeof(coordinate), stdin);
    sscanf(coordinate, "%d", &myx2);

    printf("Enter the Y value of the second coordinate: ");
    fgets(coordinate, sizeof(coordinate), stdin);
    sscanf(coordinate, "%d", &myy2);

    absxsquare = fabs ((myx1 - myx2) * (myx1 - myx2));
    absysquare = fabs ((myy1 - myy2) * (myy1 - myy2));
    sqrdist = absxsquare + absysquare;
    distance = sqrt(sqrdist);

    printf("\nThe square of the distance between those two points
    is %f",sqrdist);
    printf("\nThe actual difference is %f\n",distance);

    return(0);
    }

    And here are my compile errors:

    /tmp/ccQe7XsT.o: In function `main':
    /home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'
    collect2: ld returned 1 exit status

    I thought I had implemented the sqrt as the man page for sqrt
    explained, but as you might guess by my code, I'm more than a bit
    lost. Any help would be greatly appreciated.

    Thanks,
    Aric
    Aric, Aug 26, 2003
    #1
    1. Advertising

  2. Aric

    pete Guest

    Aric wrote:

    > #include <math.h>


    > And here are my compile errors:
    >
    > /tmp/ccQe7XsT.o: In function `main':
    > /home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'
    > collect2: ld returned 1 exit status
    >
    > I thought I had implemented the sqrt as the man page for sqrt
    > explained, but as you might guess by my code, I'm more than a bit
    > lost. Any help would be greatly appreciated.


    http://www.eskimo.com/~scs/C-faq/q14.3.html
    http://www.eskimo.com/~scs/C-faq/q13.25.html

    --
    pete
    pete, Aug 26, 2003
    #2
    1. Advertising

  3. Aric

    Greg P. Guest

    "Aric" <> wrote in message
    news:...
    | Hi,
    Hello

    <snip>
    | I did fine with the basic problem, but I can't seem to compile when I
    | try to use sqrt. Here is my code:
    |
    | #include <stdio.h>
    | #include <math.h>
    | char coordinate[50]; /* input string */
    | int myx1; /* first x coordinate */
    | int myx2; /* second x coordinate */
    | int myy1; /* first y coordinate */
    | int myy2; /* second y coordinate */
    | double sqrdist; /* distances square */
    | double distance; /* actual distance between two points */
    | int absxsquare; /* absolute value of (x1 + x2) ^2 */
    | int absysquare; /* absolute value of (y1 + y2) ^2 */

    I understand that this is just an exercise, but in general you should
    reserve your global declarations for static (extern, etc) related data. Is
    Mr. Oualline giving examples with global vars in his code? tssk tssk

    <snip>
    | And here are my compile errors:
    |
    | /tmp/ccQe7XsT.o: In function `main':
    | /home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'
    | collect2: ld returned 1 exit status

    You need to link against the math library in GCC. Some compilers don't
    require you to do so, as they incorporate most of the standard C runtime
    library into a single(or a couple) statically linked libraries that get
    added to your code, increasing its footprint(executable size). Now what if,
    perhaps, you did not use the math functions and the compiler still linked
    the C runtime (which has math in it) to your application, causing unneeded
    size? This is not always the case for some efficient compilers (usually
    needing extra switches), but after a while you will appreciate being able to
    select which specific libraries you want to be added and which to be
    omitted.

    You need to compile as such.

    gcc -o ex6-1.c -lm ex6.c (add whatever extra flags/switches you prefer)

    The -lm option is a passive switch that waits to be sent to the linker
    telling it to add the math library. For fun (at least I consider it to be),
    list your /usr/lib directory:

    cd ~yourusername
    ls -l /usr/lib | less

    or...

    ls -l /usr/lib > mylibraries.list

    And then read them with less (less mylibraries.list).

    You will see some common names in there relating to the C runtime library
    headers (usually with the same prefixed forename) so you can you know which
    libraries to add to your linker resolution when the time comes.

    Note that when if you have a file called "libSOMETHING.so" you would only
    have to pass the flag (-lSOMETHING to gcc rather than adding the prefixed
    "lib" or the extension).

    Happy hacking!

    Greg P.
    Greg P., Aug 26, 2003
    #3
  4. Aric

    Simon Biber Guest

    "Greg P." <> wrote:
    > You need to link against the math library in GCC. Some compilers don't
    > require you to do so, as they incorporate most of the standard C runtime
    > library into a single(or a couple) statically linked libraries that get
    > added to your code, increasing its footprint(executable size). Now what
    > if, perhaps, you did not use the math functions and the compiler still
    > linked the C runtime (which has math in it) to your application, causing
    > unneeded size? This is not always the case for some efficient compilers
    > (usually needing extra switches), but after a while you will appreciate
    > being able to select which specific libraries you want to be added and
    > which to be omitted.


    This library issue is entirely orthogonal to what compiler you use. It has
    nothing to do with GCC. It's a feature of many Unixes, but exists no
    matter what compiler you use on that system. It also doesn't apply when
    you use GCC on a system which does include math functions in the main
    library.

    > You need to compile as such.
    >
    > gcc -o ex6-1.c -lm ex6.c (add whatever extra flags/switches you prefer)


    You want to output the binary executable to a .c file? That's not clever.
    Either leave it with no extension (common on Unixes) or with a .EXE
    extension (common on DOS,Windows,OS/2,etc).

    > The -lm option is a passive switch that waits to be sent to the linker
    > telling it to add the math library.


    Not exactly passive; it is just sent to the linker but not to the
    preprocessor or compiler. The front-end 'gcc' is not a compiler.

    On my system it runs three programs to execute your command. With
    massively simplified arguments, they are:
    Compiler: cc1 ex6.c -o FirstTemp
    Assembler: as FirstTemp -o SecondTemp
    Linker: collect2 SecondTemp -o ex6-1.c -lm

    --
    Simon.
    Simon Biber, Aug 27, 2003
    #4
  5. Aric

    Greg P. Guest

    "Simon Biber" <> wrote in message
    news:3f4bfd71$0$28121$...
    | This library issue is entirely orthogonal to what compiler you use. It has
    | nothing to do with GCC. It's a feature of many Unixes, but exists no
    | matter what compiler you use on that system. It also doesn't apply when
    | you use GCC on a system which does include math functions in the main
    | library.

    True, however, I was replying to the poster regarding the GNU Compiler
    Collection, not *any* compiler on *NIX. If the poster said he was using dmc
    on a win32 platform asking about optimizations, I would reply with
    information regarding the "-o" switches for dmc, regardless of the fact that
    most compilers support this.

    | > gcc -o ex6-1.c -lm ex6.c (add whatever extra flags/switches you prefer)
    |
    | You want to output the binary executable to a .c file? That's not clever.

    That was a mis-type on my behalf, posters do not always reply with 100%
    perfect messages. I am sure that the OP is smart enough to understand that
    the output name should be otherwise
    Greg P., Aug 27, 2003
    #5
  6. Aric

    Simon Biber Guest

    "Greg P." <> wrote:
    > "Simon Biber" <> wrote in message
    > | This library issue is entirely orthogonal to what compiler you use.
    > | It has nothing to do with GCC. It's a feature of many Unixes, but
    > | exists no matter what compiler you use on that system. It also
    > | doesn't apply when you use GCC on a system which does include math
    > | functions in the main library.
    >
    > True, however, I was replying to the poster regarding the GNU
    > Compiler Collection, not *any* compiler on *NIX.


    The OP did not mention Unix at all; you reasonably assumed that from the
    symptoms of his problem. But you didn't make it clear that the problem
    has nothing to do with the compiler, and rather is a feature of the C
    library of the OP's operating system.

    You said:
    > > You need to link against the math library in GCC.


    The word 'you' can have two meanings, I took at as a general 'one needs
    to' rather than 'in your particular case, you need to'. In the general
    case, this advice is flat out wrong. Assuming you mean the specific, ...

    > > Some compilers don't require you to do so, as they incorporate most
    > > of the standard C runtime library into a single(or a couple)
    > > statically linked libraries that get added to your code, increasing
    > > its footprint(executable size).


    Remember that compilers and libraries are completely separate things.
    Can you name one compiler on his platform that doesn't require him to
    do so, as it automatically incorporates statically linked libraries?

    > If the poster said he was using dmc on a win32 platform asking about
    > optimizations, I would reply with information regarding the "-o"
    > switches for dmc, regardless of the fact that most compilers support
    > this.


    Actually, you should redirect it to an appropriate group since asking
    about particular compilers is off-topic in comp.lang.c.

    --
    Simon.
    Simon Biber, Aug 27, 2003
    #6
  7. Aric

    Tim Prince Guest

    Aric wrote:

    >
    > #include <stdio.h>
    > #include <math.h>
    >
    >
    > I thought I had implemented the sqrt as the man page for sqrt
    > explained, but as you might guess by my code, I'm more than a bit
    > lost. Any help would be greatly appreciated.
    >

    A little more reading in your compiler and platform documentation. You
    must either specify linkage with the math library, according to your
    platform (likely guess, as the C faq tells you, -lm), or tell gcc to use
    in-line sqrt instructions exclusively (-ffast-math), thereby disabling
    <errno.h> processing. These can only be guesses, as they depart from the
    topic of this NG, and you haven't specified your target platform.
    --
    Tim Prince
    Tim Prince, Aug 27, 2003
    #7
  8. Aric

    Greg P. Guest

    "Simon Biber" <> wrote in message
    news:3f4c49db$0$28117$...
    | The OP did not mention Unix at all; you reasonably assumed that from the
    | symptoms of his problem. But you didn't make it clear that the problem
    | has nothing to do with the compiler, and rather is a feature of the C
    | library of the OP's operating system.

    I "assumed" due to this statement from the OP:
    >And here are my compile errors:


    >/tmp/ccQe7XsT.o: In function `main':

    ^
    >/home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'

    ^
    >collect2: ld returned 1 exit status


    If you search for other posts regarding gcc use on this newsgroup, the
    replies also "assume" that the user is running a *nix variant (unless they
    otherwise specify MYSYS or Cygwin)

    | Remember that compilers and libraries are completely separate things.
    | Can you name one compiler on his platform that doesn't require him to
    | do so, as it automatically incorporates statically linked libraries?

    The Intel Compiler for Linux automatically links the math lib
    Greg P., Aug 27, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    426
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    642
    Kevin P. Fleming
    Nov 6, 2003
  3. John

    How to use sqrt() function with gcc?

    John, Aug 4, 2006, in forum: C Programming
    Replies:
    3
    Views:
    955
    Keith Thompson
    Aug 4, 2006
  4. Tim Prince

    Re: sqrt() double trouble

    Tim Prince, Jun 5, 2008, in forum: C Programming
    Replies:
    14
    Views:
    583
    user923005
    Jun 7, 2008
  5. Martin Ambuhl

    Re: sqrt() double trouble

    Martin Ambuhl, Jun 6, 2008, in forum: C Programming
    Replies:
    0
    Views:
    482
    Martin Ambuhl
    Jun 6, 2008
Loading...

Share This Page