57

When I compile some C code with gcc, it needs adding -lm. For example, when I want to use fmax in my program, I must use the following command:

gcc myprogram.c -lm

What happens to my program by adding -lm? What does -lm mean?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Armaa
  • 625
  • 1
  • 5
  • 13
  • The canonical may be *[Why do you have to link the math library in C?](https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c)* (18 answers. 314 upvotes. 2009.) – Peter Mortensen Oct 27 '22 at 23:50
  • Possible duplicate (2011): *[GCC -lm -lz -lrt options - what are they about?](https://stackoverflow.com/questions/5663097/gcc-lm-lz-lrt-options-what-are-they-about)* – Peter Mortensen Oct 27 '22 at 23:58

2 Answers2

57

TLDNR: math.h is not a part of the standard C library, so you have to link to it!

-llibrary searches the library library while linking. The m stands for libm, the library that contains <math.h>. For more information, see these couple links.

@luantkow's answer is really good but long! Here's a short version if you don't want to read! ¯\_(ツ)_/¯

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Brian Tung
  • 742
  • 6
  • 7
  • This is not an answer to the question. – kk. Sep 27 '20 at 17:13
  • 3
    @kk is the information in the answer wrong? Can you tell me why? – Khoa Vo Jun 12 '21 at 05:18
  • 2
    "math.h is not a part of the standard C library" Sure it is. But it may not be part of the specific default lib used by a certain gcc port. – Lundin Jun 10 '22 at 14:00
  • @Lundin No, it is not. C++ requires math.h, C does not. The fact is only stdxxx.h are considered standard library, -lc flag – Валерий Заподовников Aug 13 '23 at 08:26
  • @ВалерийЗаподовников No, that is wrong and I have no idea how you came up with that delusion. A _freestanding_ implementation in C may pick a vert limited subset of standard headers (C17 4 §6), whereas a conforming hosted implementation must implement all of the mandatory headers in chapter 7 which is most of them. The std prefix doesn't have any significant meaning at all; in fact some like stdatomic.h are optional. – Lundin Aug 14 '23 at 06:13
34

Let's say you have the main.c file:

#include <math.h>
#include <stdio.h>

float my_foo(float a, float b)
{
    return fmax(a, b);
}

int main()
{
    printf("%f\n", my_foo(4.5, 3.1));
    return 0;
}

If you try to compile it without the -lm flag, you will receive am undefined reference error:

main.o: In function `my_foo':
main.c:(.text+0x1d): undefined reference to `fmax'
collect2: error: ld returned 1 exit status

This is because the linker does not know any implementation of the fmax function. You have to provide it.

In gcc man, you can find the following description of the -llibrary flag:

Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.

The directories searched include several standard system directories plus any that you specify with -L.

Normally the files found this way are library files---archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with lib and .a and searches several directories.

It looks that I have the libm.a file stored at /usr/lib/x86_64-linux-gnu/libm.a:

$ find /usr/lib -iname libm.a

/usr/lib/x86_64-linux-gnu/libm.a

You can check that libm.a contains the definition of fmax:

$ nm /usr/lib/x86_64-linux-gnu/libm.a --defined-only | grep fmax

[...]
s_fmax.o:
0000000000000000 W fmax
[...]

In case the command above will result in an error with following result:

$ nm /usr/lib/x86_64-linux-gnu/libm.a

nm: /usr/lib/x86_64-linux-gnu/libm.a: file format not recognized

It may be caused by the fact that your distribution provides libm.a as a linker script.

$ file /usr/lib/x86_64-linux-gnu/libm.a

/usr/lib/x86_64-linux-gnu/libm.a: ASCII text

File /usr/lib/x86_64-linux-gnu/libm.a

/* GNU ld script
*/
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /usr/lib/x86_64-linux-gnu/libm-2.31.a /usr/lib/x86_64-linux-gnu/libmvec.a )

The script basically informs the linker to try to link libm-2.31.a and libmvec.a. See the GROUP description in Using LD, the GNU linker.

So you should be able to verify that the fmax implementation is provided in libm-2.31.a with:

$ nm /usr/lib/x86_64-linux-gnu/libm-2.31.a --defined-only 2> /dev/null | grep -w fmax

0000000000000000 W fmax
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
luantkow
  • 2,809
  • 20
  • 14
  • For some reason I get `file format not recognized` for `nm /usr/lib/x86_64-linux-gnu/libm.a`. – Adobe Jul 22 '20 at 08:00
  • 1
    @Adobe Thank you for mentioning that. I was able to "reproduce" your issue on Ubuntu 20.04 and described short explanation in my answer. – luantkow Jul 24 '20 at 22:57
  • Does gcc automatically include this library nowadays? I can compile and run this code without the -lm option. – aganm May 31 '21 at 08:19
  • 1
    @aganm Did you by any chance compiled the code with `g++` instead of `gcc`? If yes, the code was compiled as `C++` not `C` and `C++` standard library requires `libm` by default. You can find more here: https://stackoverflow.com/a/12453417/3188346. If no, could you please share the command you used for compilation? I just tried this on Ubuntu 21.04 and `-lm` was necessary to link the executable. – luantkow Jun 01 '21 at 13:49
  • @luantkow Nevermind, I did something wrong. You are correct. – aganm Jun 02 '21 at 11:05
  • Don't omit "-lm" even when using C++. If you call a function of libm, you must link to libm and don't rely on implicit dependencies. Or your command may fail with different linux distributions. – Johannes Schaub - litb Jul 08 '21 at 20:24