Math.h linker error using sin () in C

I have two code segments, both identical, with the exception of one line. One program does not compile, and the other successfully. I link math libraries when I execute cc to compile the code.

I am using a function double sin(double). It seems to be defined in math.h, although I looked through /usr/include/math.h and did not find a function reference sin().

See http://www.gnu.org/software/libc/manual/html_mono/libc.html#Trig-Functions

The function sin()works in one segment of code that I give, but not in another.

//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 an unsuccessful 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

It made me feel a little silly or at least the feeling that I had missed something over the years.

+4
2

. gcc, -lm .

, ? , - , sin , gcc , libm - .

+1

-lm , , sin , , . , , gcc , GCC, ( ):

GCC C. __builtin_ , C, -fno-builtin. (. C ) ; , , .

, gcc, sin , :

call    sin
0

All Articles