<math.h> problem

Discussion in 'C Programming' started by Ben Pfaff, Aug 14, 2003.

  1. Ben Pfaff

    Ben Pfaff Guest

    "John B." <> writes:

    > I'm self teaching myself C on a Linux box but I can't get a simple program
    > to recognize math functions. I start the program with:


    You forgot to read the FAQ.
    --
    "I am worth much more brick-bat!!"
    --Uday Joshi
     
    Ben Pfaff, Aug 14, 2003
    #1
    1. Advertising

  2. Ben Pfaff

    John B. Guest

    I'm self teaching myself C on a Linux box but I can't get a simple program
    to recognize math functions. I start the program with:

    #include <stdio.h>
    #include <math.h> but when I compile the program, it does not
    recognize sin(x); or pow(x,y) etc, in any equation.

    /usr/include does have a math.h file. Any help or things to try would
    be greatly appreciated!!!!. (I do alot of graphic things with trig.)
    thanks
     
    John B., Aug 14, 2003
    #2
    1. Advertising

  3. Ben Pfaff

    Tim Hagan Guest

    "John B." wrote:
    >
    > I'm self teaching myself C on a Linux box but I can't get a simple program
    > to recognize math functions. I start the program with:
    >
    > #include <stdio.h>
    > #include <math.h> but when I compile the program, it does not
    > recognize sin(x); or pow(x,y) etc, in any equation.
    >
    > /usr/include does have a math.h file. Any help or things to try would
    > be greatly appreciated!!!!. (I do alot of graphic things with trig.)
    > thanks


    It's a FAQ.

    14.3: I'm trying to do some simple trig, and I am #including <math.h>,
    but I keep getting "undefined: sin" compilation errors.

    A: Make sure you're actually linking with the math library. For
    instance, under Unix, you usually need to use the -lm option, at
    the *end* of the command line, when compiling/linking. See also
    questions 13.25, 13.26, and 14.2.

    See the rest at http://www.eskimo.com/~scs/C-faq/top.html

    --
    Tim Hagan
     
    Tim Hagan, Aug 14, 2003
    #3
  4. Ben Pfaff

    Derk Gwen Guest

    "John B." <> wrote:
    # I'm self teaching myself C on a Linux box but I can't get a simple program
    # to recognize math functions. I start the program with:
    #
    # #include <stdio.h>
    # #include <math.h> but when I compile the program, it does not
    # recognize sin(x); or pow(x,y) etc, in any equation.

    It compiles but has link errors? On most platform, you have to link the math
    library. On unices this is done with a -lm on the ld command (or *cc command
    used to call ld).

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    GERBILS
    GERBILS
    GERBILS
     
    Derk Gwen, Aug 14, 2003
    #4
  5. Ben Pfaff

    Kevin Bracey Guest

    In message <>
    Derk Gwen <> wrote:

    > "John B." <> wrote:
    > # I'm self teaching myself C on a Linux box but I can't get a simple
    > # program to recognize math functions. I start the program with:
    >
    > It compiles but has link errors? On most platform, you have to link the
    > math library. On unices this is done with a -lm on the ld command (or *cc
    > command used to call ld).


    I've never quite understood why this is. Presumably it has something to
    do with reducing code size, but are Unix linkers not sophisticated enough to
    omit unused portions of libraries automatically?

    Surely a modern Unix C environment could default to including the whole ISO C
    library and omit the portions not required?

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
     
    Kevin Bracey, Aug 19, 2003
    #5
    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. chirs
    Replies:
    18
    Views:
    793
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,151
    Jonathan Turkanis
    Apr 1, 2004
  3. Mark Healey
    Replies:
    7
    Views:
    1,539
    Tim Prince
    May 22, 2006
  4. Philipp
    Replies:
    9
    Views:
    1,164
    Mark Space
    Jul 23, 2008
  5. VK
    Replies:
    15
    Views:
    1,278
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page