Announcement

Collapse
No announcement yet.

Fixing "undefined reference"

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Fixing "undefined reference"

    I've been playing with the gcc compiler. I've written, compiled and run several one-page programs. I just wrote a small program {see below) that uses the trig functions in the math.h library. When I try to compile it, I get an error message. (see below)

    Do I need to add something to my path or use different command line options or ?

    /*
    trigtest.c
    */

    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>


    int main ( int argc, char **argv ) {

    double xVal;
    double yVal;
    double r;

    r = 1.0;

    xVal = cos( r );
    yVal = sin( r );

    return 0;
    }


    tim@optimus:~/src/plot$ gcc trigtest.c
    /tmp/ccm1dv69.o: In function `main':
    trigtest.c.text+0x1d): undefined reference to `cos'
    trigtest.c.text+0x2b): undefined reference to `sin'
    collect2: ld returned 1 exit status
    tim@optimus:~/src/plot$

    #2
    Re: Fixing &quot;undefined reference&quot;

    You have to inform the linker that you intend to use the math libraries, e.g. "gcc trigtest.c -o trigtest -lm" where m is the math library.

    Comment

    Working...
    X