Trying to compile a C program with GCC and math.h header

81 views Asked by At

So, I'm trying to compile a simple C program in windows environment using GCC:

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

int main(void) {
    printf("hi\n");
    double x = sin(10);
    printf("%lf\n", x);
    return 0;
}

I use the following command to compile:

gcc -o test test.c -lm

and it gives the following error:

test.c: In function 'main':
test.c:6:16: warning: implicit declaration of function 'sin' [-Wimplicit-function-declaration]
    6 |     double x = sin(10);
      |                ^~~
test.c:3:1: note: include '<math.h>' or provide a declaration of 'sin'
    2 | #include <math.h>
  +++ |+#include <math.h>
    3 |
test.c:6:16: warning: incompatible implicit declaration of built-in function 'sin' [-Wbuiltin-declaration-mismatch]
    6 |     double x = sin(10);
      |                ^~~
test.c:6:16: note: include '<math.h>' or provide a declaration of 'sin'

Note that I declared the <math.h> header and link the library with the command -lm.

What am I doing wrong?

Thanks in advance!

0

There are 0 answers