Compiling some maths examples

This is a discussion on Compiling some maths examples within the C Programming forums, part of the General Programming Boards category; Hi all, I have been running through the basics math.h examples in c. Code: #include <stdio.h> #include <math.h> int main(void) ...

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    Compiling some maths examples

    Hi all,

    I have been running through the basics math.h examples in c.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
      double x = 2.25;
      double less = 0.0;
      double more = 0.0;
      double root = 0.0;
      
      double angle = 45.0;
      double pi = 3.1419265;
      double sine = 0.0;
      double cosine = 0.0;
      
      printf("\nExamples in maths.h - x = 2.25\n");
      
      less = floor(2.25);
      printf("\nless = floor(x) is %.2f",less);
      
      more = ceil(2.25);
      printf("\nmore = ceil(x) is %.2f",more);
      
      root = sqrt(2.25);
      printf("\nroot = sqrt(x) is %.2f",root);
      
      // sine = sin(pi*angle/180.0);  
      printf("\nsin(%.6f*%.2f/180.0) is sin(%.3f) = %.3f\n",pi, angle, sine);
      
      return 0;
    }
    All makes perfect sense. When compiling though... it complains if put x instead of 2.25 in each of the functions. - while in the generated object file.

    [jryan@freedom lessons]$ gcc 2.20_mathExamples.c -o 2.20_mathExamples
    /home/jryan/tmp/ccvACeHe.o: In function `main':
    2.20_mathExamples.c.text+0xb2): undefined reference to `floor'
    collect2: ld returned 1 exit status

    So take the error about floor for example... I had the other functions remmed out at the time of the above error - they all error with the same message. So, if I hardcode 2.25 instead of x in floor like in the code above. eg floor(2.25) instead of floor(x), it works! Hmm, what I am I doing wrong here? Similarly it does not recognise sign even though I did include math.h.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    You need to link in the math library. Add -lm to the command line to compile/link.

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by grumpy View Post
    You need to link in the math library. Add -lm to the command line to compile/link.
    Aah thanks Grumpy. I was keying up and grabbing the previous gcc command in my list. Will give it a whirl.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    Quote Originally Posted by hamsteroid View Post
    So take the error about floor for example... I had the other functions remmed out at the time of the above error - they all error with the same message. So, if I hardcode 2.25 instead of x in floor like in the code above. eg floor(2.25) instead of floor(x), it works! Hmm, what I am I doing wrong here? Similarly it does not recognise sign even though I did include math.h.
    The compiler is smart. When it sees floor(2.25) it knows that the answer is 2, so when it generates code it simply uses the value 2 instead of actually calling floor. That's why it works for constant values but not variables.

  5. #5
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    That's impressive. All the things that the poor ole compiler will look out for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Compiling C++ under C code
    By rjasso in forum C Programming
    Replies: 2
    Last Post: 11-11-2004, 12:44 PM
  4. maths???
    By nerdyneo in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2003, 12:04 PM
  5. maths in a program
    By anthonye in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 09:08 AM

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