Thread: Trouble with cos and sin function from math.h

  1. #1
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50

    Trouble with cos and sin function from math.h

    When I compile my code, it can't find the cos or sin functions. Here are my #include statements.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include "graphics.h"
    at the start of my code.

    here's my offending function:

    Code:
    /*Order* and Current* are struct pointers, just to confirm. */
    
    void move( Order* order, Current* current)
    {
            int movement = atoi( order->parameter);
            double angle = current->angle;
            int *x = &current->position->x;
            int *y = &current->position->y;
    
            /*Modify the x, y coordinates based on the movement, 
              and the angle known from the current*/
            *x += movement * (cos( angle));
            *y += movement * (sin( angle));
            /*The above two lines are the offending lines*/
    
    
    }
    And here are the errors I come up with when "making" my program.

    graphics.o: In function `move':
    /home/<CENSORED>/Programs/UCP/Assignment/Program/graphics.c:101: undefined reference to `cos'
    /home/<CENSORED>/Programs/UCP/Assignment/Program/graphics.c:102: undefined reference to `sin'
    Does anybody have any ideas what I'm doing wrong?

    This is for an assignment, so don't tell me if there's a better way to do it (let me figure that out myself), just help me figure out why it can't see the cos() function.

    Thanks,
    Doc
    Last edited by The Doctor; 09-18-2012 at 09:14 PM.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,548
    You may need to link to the library, e.g., by passing the -lm option when compiling/linking.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Why haven't I needed to do that so far? And could I do that in the Makefile?

    EDIT: I added that to my $(CFLAGS) variable in the makefile, and it still comes up with the same error.

    Here's my CFLAGS variable (which I put in front of every compile command):
    CFLAGS = -Wall -pedantic -ansi -lm
    My teacher told me to put all those warning ones. I'm supposed to be using C89.
    Last edited by The Doctor; 09-18-2012 at 09:18 PM.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    -lm is a linker flag, so it goes in the LDFLAGS variable in your Makefile.

  5. #5
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Just compiled on another computer, and it compiled fine. There might be something wrong with my math library. Any suggestions on how to fix it?

    Also, I actually don't have a LDFLAGS variable... Would I put it straight after the CFLAGS variable when I'm writing my rules?
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,738
    > Just compiled on another computer, and it compiled fine. There might be something wrong with my math library. Any suggestions on how to fix it?
    Historically, the math library was always a separate library. When C was invented nearly 40 years ago, FP hardware was rare to non-existent, and all FP work was done in s/w. A separate library allowed you to easily choose between "none", "fast" or "accurate" implementations.

    Since C evolved in tandem with Unix, most systems with a Unix heritage maintain the separate math library.

    Compilers on Wintel machines either had a single library, or tried to be "smart" at detecting whether you needed FP or not. Occasionally, this too would break.

    > Also, I actually don't have a LDFLAGS variable... Would I put it straight after the CFLAGS variable when I'm writing my rules?
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with math operations-Beginner program
    By brewgrass in forum C++ Programming
    Replies: 11
    Last Post: 08-08-2011, 03:47 AM
  2. C math log() function misbehaves...
    By csonx_p in forum C Programming
    Replies: 13
    Last Post: 05-23-2008, 05:50 AM
  3. math function vs functions...
    By niceguy in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 11:38 PM
  4. Unknown Math Function
    By verd in forum C Programming
    Replies: 2
    Last Post: 02-06-2005, 03:31 PM
  5. math function
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2001, 02:01 PM