0

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!

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Lgcos
  • 91
  • 7
  • 1
    It looks like you have an empty or otherwise bogus file `math.h` somewhere before the standard include directory. Also note that `%lf` is incorrect: you should just use `%f` in `printf`. – chqrlie Mar 11 '23 at 06:39
  • 1
    Your program works for me as is, so I agree with @chqrlie, but just wanted to share with you that if you run `cpp your_file.c` it will tell you which math.h file it's reading (and it's contents). – Allan Wind Mar 11 '23 at 07:58
  • do `gcc -H -c test.c` and see which `math.h` is included. – n. m. could be an AI Mar 11 '23 at 15:44

0 Answers0