Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does math.h need to be linked in makefile but not string.h? [duplicate]

Tags:

c

makefile

linker

I've been including <string.h> all over the place and the moment I go to include <math.h> for pow() I discover that I need to modify my makefile to get access to the definition of pow.

I also do not know where I can look up more libraries if I need to include them. I randomly read that -lm flag (or at least the m of it) indicates the standard c math library, but I have no idea what any of the other standard c libraries might be called.

I just looked in user/local/lib and all I see is:

$ cd /usr/local/lib
$ ls -al
drwxr-xr-x  3 root root  4096 Apr 25  2012 .
drwxr-xr-x 10 root root  4096 Apr 25  2012 ..
drwxrwsr-x  4 root staff 4096 Oct 14 10:19 python2.7
like image 314
tarabyte Avatar asked Oct 22 '13 18:10

tarabyte


People also ask

Do you have to link math H?

It is not automatically linked to along with the rest of the standard C library. If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command.

Is math H included by default?

It's C source code that tells the compiler how to generate calls to pow and other functions. It should be available by default; all you need is #include <math. h> , and the compiler will know where to find it.

How do I link LibM?

Link your program with the `libm. a' library, e.g. by specifying `-lm' on the link command line.

What is LibM in Linux?

AOCL-LibM is a software library containing a collection of basic math functions optimized for x86-64 processor-based machines. It provides many routines from the list of standard C99 math functions.


1 Answers

string.h is a header reference to a module of the standard library, but due to historical reasons math.h is not a header reference to a module of the standard library. Only the standard library is typically included by default in the linker when compiling programs.

At one point in time, one might substitute libm.so math.h implementations with other implementations that were better optimized for memory, CPU performance, etc. Effectively there was not a single libm.so "default" implementation. However, most systems at least provided an implementation. That implementation is in the default library location as libm.so and would be linked in with -lm.

In the event that you had a faster (perhaps even at the expense of less accurate) library that you understood you could use, you had the ability to override the system-provided math.h libm.so implementation.

The early CRAY systems (I didn't work on one) did optimize a few math implementations to not do proper "full" math operations, with the understanding that to get a 100% correct answer, you would "finish" the assembly operations with code that often wasn't important due to the number of significant digits in the computation. (From my understanding of a Cryptology Museum display in Washsington D.C.)

The problem with multiple implementations is that you now have a choice, and the standard C library is not structured to provide you with a choice of implementation.

like image 75
Edwin Buck Avatar answered Sep 21 '22 02:09

Edwin Buck