-1

As mentioned over here and here

So if your program is using math functions and including math.h, then you need to explicitly link the math library by passing the ‘-lm’ flag

But I just manage to get the linking without using -lm flag with gcc on my benign C code.

and It work perfectly well.

Any clue.

gcc -version

gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Community
  • 1
  • 1
Noobie
  • 461
  • 1
  • 12
  • 34
  • 1
    Let me get this straight, something's *working* and you have a complaint? :-) – paxdiablo Feb 28 '17 at 07:07
  • @paxdiablo :) I just want to know would it work same across all machine or it the specific version of gcc on MacOS that does this – Noobie Feb 28 '17 at 07:12
  • FWIW, I think it's anachronistic behaviour on the part of gcc in that the math code is in a library separate to the other libc stuff. But, from memory, gcc/ld has some pretty powerful config files that control this sort of thing so it's possible Apple (or BSD) just fixed it up. I wouldn't rely on it working everywhere. – paxdiablo Feb 28 '17 at 07:17
  • 2
    Guys, look at output of `gcc -v`. It's not even gcc! – el.pescado - нет войне Feb 28 '17 at 07:27
  • Show your code, if you want others to confirm it on other platforms... – Serge Ballesta Feb 28 '17 at 07:36
  • 1
    @paxdiablo - a separate `libm` has been the norm since well before GCC was created; I completely agree that relying on (parts) the math library being included without specifying it is unwise and will reduce the code's portability. – Toby Speight Feb 28 '17 at 10:50

1 Answers1

2

On many systems some parts of what's traditionally in libm are included in the standard C library for various reasons. Other parts may be implemented directly in the math.h header, yet others might be just implemented inline by the compiler.

Whatever code you had, you got away with not linking with libm. You'll often end up in situations where you get away with doing something despite it not being perfectly correct. A good habit is to ignore that luck and still do what the standards/documentation says because it reduces the number of problems in the future.

Art
  • 19,807
  • 1
  • 34
  • 60