undefined reference to `fmod'

This is a discussion on undefined reference to `fmod' within the C Programming forums, part of the General Programming Boards category; I am going through K&R C Answer Book and coding exercise4-3.c there is a call for fmod library function which ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    undefined reference to `fmod'

    I am going through K&R C Answer Book and coding exercise4-3.c
    there is a call for fmod library function which I thought was present in math.h but I am resulting an 'underfined reference to 'fmod'
    the line in question is about 3 quarters the way down - just below the case '%': line

    any clues what I am doing wrong.

    gcc -o exercise4-3.exe exercise4-3.c
    /tmp/ccOFQfVw.o: In function `main':
    /tmp/ccOFQfVw.o(.text+0x169): undefined reference to `fmod'

    gcc -v
    Using builtin specs.
    gcc version 2.95.4 20020320 [FreeBSD]


    Code:
    #include  <stdio.h>
    #include  <math.h>      /* for atof()   */
    #include  <ctype.h>     /* for atof()   */
    #include  <string.h>
    
    #define    MAXVAL   1000  /* maximum depth  of val stack       */
    #define    MAXOP    100   /* max size of operand or operator   */
    #define    NUMBER   '0'   /* signal that a number was found    */
    #define    BUFSIZE  100
    
    
    int sp = 0;           /* next free stack position  */
    double val[MAXVAL];   /* value stack */
    int getop(char []);
    void push(double);
    double pop(void);
    int getch(void);
    void ungetch(int);
    char buf[BUFSIZE];   /* buffer for ungetch */
    int  bufp = 0;       /* next free positino in buf */
    
    
    double atof(char s[]);
    
    int i, sign;
    
    /* reverse Polish calculator   */
    main()
    {
      int type;
      double op2;
      char s[MAXOP];
    
      while ((type = getop(s)) != EOF) {
        switch(type) {
        case NUMBER:
          push(atof(s));
          break;
        case '+':
          push(pop() + pop());
          break;
        case '*':
          push(pop() * pop());
          break;
        case '-':
          op2 = pop();
          push(pop() - op2);
          break;
        case '/':
          op2 = pop();
          if (op2 != 0.0)
            push(pop() / op2);
          else 
            printf("error: zero divisor\n");
          break;
        case '%':
          op2 = pop();
          if (op2 != 0.0)
            push(fmod(pop(), op2));
          else
            printf("error: zero divisor\n");
          break;   
        case '\n':
          printf("\t%.8g\n", pop());
          break;
        default:
          printf("error: unkown command %s\n", s);
          break;
        }
    
      }
      return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I don't see that. I do see lots of complaint about push and pop and getop.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Append -lm to your command line invocation. gcc tends to need to be told to include the math library.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    Brilliant cwr - that is the answer!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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