DanielJohnson a écrit :
You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.
We have often discussed here this msfeature of gcc but they will never
change it as it seems.
You cannot remove historical quirks like this without breaking
anything, especially if they've been almost universial practice
for a couple of decades. I've considered adding -lm by default
to my compiler as well, but I figured this would end up doing
the users a disservice. Because if you write build scripts for
a compiler which does not require you to link that library
explicitly, then chances are your project will break if you
use it with a different compiler.
In that case the time you have to deal with this problem is
just deferred until a later date. So I think it's best to have
to encounter, recognize, and fix this problem as early as
possible.
It's not just compilers by different vendors that would be
affected, but also older versions of a compiler that recently
abandoned this requirement. If gcc comes out with a new
release that always links with that library by default, as you
you suggest, then code developed with that compiler version is
more likely not to build with older versions.
There are many other more system-specific libraries that can
break the necks of developers, such as the networking libraries
you explicitly have to link with on Solaris/UnixWare/Reliant,
and then there are realtime, threading and dynamic linking libs
you need on some Unices, which tend to be very inconsistent.
You may also run into problems with weak empty function stubs
in a Unix standard C library object, which make it seem like you
don't have to link with any other libs when in fact you do.
I've given some thought to this topic as well, and one approach
that came to my mind, is to have the linker or compiler give
you more info than ``symbol x is undefined!''. Maybe we should
have some sort of database of all standard library functions
available on a given system. Then the linker error message
could read ``symbol x undefined (but available in /usr/lib/x.so:
add -lx to the linker flags!)'' instead.
I'm currently doing some ELF file access code, and maybe I'll
also end up writing a linker, so I may play with such a symbol
database some time soon. In general, though, I feel that system
library dependencies are already a terrible battlefield which
would benefit immensely from preserving at least a tiny bit of
consistency across multiple platforms and compilers.
(BTW, if I recall correctly, AIX by default does not even
install the math library, so a compiler that unconditionally
links with it would have an unneeded dependency.)