compilation - Why do you have to link the math library in C? - Stack Overflow
Open Source Mathematics Libraries
All-Platform SIMD C Math Library
MathC - a pure C math library for 2D and 3D programming
Videos
The functions in stdlib.h and stdio.h have implementations in libc.so (or libc.a for static linking), which is linked into your executable by default (as if -lc were specified). GCC can be instructed to avoid this automatic link with the -nostdlib or -nodefaultlibs options.
The math functions in math.h have implementations in libm.so (or libm.a for static linking), and libm is not linked in by default. There are historical reasons for this libm/libc split, none of them very convincing.
Interestingly, the C++ runtime libstdc++ requires libm, so if you compile a C++ program with GCC (g++), you will automatically get libm linked in.
Remember that C is an old language and that FPUs are a relatively recent phenomenon. I first saw C on 8-bit processors where it was a lot of work to do even 32-bit integer arithmetic. Many of these implementations didn't even have a floating point math library available!
Even on the first 68000 machines (Mac, Atari ST, Amiga), floating point coprocessors were often expensive add-ons.
To do all that floating point math, you needed a pretty sizable library. And the math was going to be slow. So you rarely used floats. You tried to do everything with integers or scaled integers. When you had to include math.h, you gritted your teeth. Often, you'd write your own approximations and lookup tables to avoid it.
Trade-offs existed for a long time. Sometimes there were competing math packages called "fastmath" or such. What's the best solution for math? Really accurate but slow stuff? Inaccurate but fast? Big tables for trig functions? It wasn't until coprocessors were guaranteed to be in the computer that most implementations became obvious. I imagine that there's some programmer out there somewhere right now, working on an embedded chip, trying to decide whether to bring in the math library to handle some math problem.
That's why math wasn't standard. Many or maybe most programs didn't use a single float. If FPUs had always been around and floats and doubles were always cheap to operate on, no doubt there would have been a "stdmath".
What are some comprehensive math libraries for C? Specifically for using linear algebra, calculus, probability, and statistics? I want to practice deep learning algorithms but don't want to dig down so low as to implement all of the underlying math as well.
Append -lm to the end of your gcc command.
With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. 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.
For example: gcc -o foo foo.c -lm
If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.
gcc xyz.c -o xyz -lm
Here,
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to specify the output file.
xyz is the name of the output file.
-lm is an option to link againt the math library (libm).
for more details here is the link containing complete article on it.
Compiling C program with math.h in Linux.
Hi C friends,
I wanted to share with you my math library that I use for my game engine. It's a highly optimized, single-header 3D math library written in C that uses SIMDE as the back-end. This means that if a platform supports vector registers, then the compiler will do a very good job of auto-vectorizing the code. Currently tested on: x86-64, ARM64, RISC-V, PowerPC, MIPS, SPARC, and KVX.
Contains the standard types and functions needed for 3D graphics.
Tested with Clang, GCC, MSVC.
This type of library is usually only done for x86, and maybe sometimes for ARM, but this allows you to compile the same SIMD-optimized code for any platform. If the platform doesn't have vector registers, then the compiler will treat it as scalar code and optimize it accordingly.
It also optionally uses SLEEF, so if you have that installed on your system, simply uncomment "sleef.h" at the top of the file and you'll be able to use vectorized math.h functions. If you don't have SLEEF, then the compiler will do it's best to vectorize this code, or if you have a recent Intel CPU, it will call SVML elementary functions.
To use, just clone SIMDE into the same folder as cml.h.
https://github.com/pbotmeyertron/c_math_library