I have two segments of code, both identical except for one line. One program fails to compile, and the other one is successful. I do link the math libraries when I execute cc to compile the code.
I'm using the double sin(double)
function. It's apparently defined in math.h, although I looked in /usr/include/math.h and found no reference to the sin()
function.
See http://www.gnu.org/software/libc/manual/html_mono/libc.html#Trig-Functions
The sin()
function does work in one code segment I give but not in the other.
//Successful program - demo1.c
#include <stdio.h>
#include <math.h>
int main (void)
{
double input, sine_A;
input = 6.2830;
sine_A = sin(6.2830);
printf("sine=%f\n",sine_A);
return 0;
}
This is the failed program:
//Failed program - demo2.c
#include <stdio.h>
#include <math.h>
int main (void)
{
double input, sine_A;
input = 6.2830;
sine_A = sin(input);
printf("sine=%f\n",sine_A);
return 0;
}
$ cc -lm demo2.c
/tmp/ccnpIWZd.o: In function `main':
demo2.c:(.text+0x1c): undefined reference to `sin'
collect2: ld returned 1 exit status
This has been leaving me feeling a bit stupid, or at least, feeling I missed something over the years.