1

If I need to use the math library, I need to add -lm when I'm using GCC in Linux. But on Windows when I'm using GCC in MinGW-w64, I didn't use -lm and it works well.

I know the reason why it is necessary to link libm. But I don't really know why I can omit that in Windows?

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Shuai
  • 962
  • 1
  • 10
  • 21

2 Answers2

7

Because, under MinGW, the math functions aren't actually defined in libm. "libm" is an empty library used as a placeholder; the math functions are actually defined in MSVCRT.DLL, which MinGW uses as its standard library.

(The validity of using the system's MSVCRT as a standard C library is debatable. Nevertheless, that's what MinGW does.)

1

In my opinion, it is a significant, longstanding bug in the Unix and Linux C library setup that you actually need to use -lm. I'd say you should thank MinGW for fixing this.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103