It's the -l option with the argument m. In other words, it links your program with the m library. On many systems C's mathematical functions are split out into this separate library. Answer from aioeu on reddit.com
Top answer
1 of 2
73

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! ¯\_(ツ)_/¯

2 of 2
46

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
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › what-exactly-does-the-lm-option-of-gcc-787841
What exactly does the -lm option of gcc?
Hello together, I'm learning C and found this problem. the following code: Code: /* sqroot.c */ #include #include int
🌐
Quora
quora.com › Why-do-we-use-gcc-lm-for-compilation-of-program-containing-math-functions
Why do we use gcc -lm for compilation of program containing math functions? - Quora
Answer (1 of 3): You have file called libc.so.6. which will have definition for printf (input/output) like similiar functions. Normally gcc will defaultly include that file by -lc (lib's short form is 'l' ). Like wise for doing math operations you need those function's(pow/sqrt...) definitions it...
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Link-Options.html
Link Options (Using the GNU Compiler Collection (GCC))
Still link with the startup files, libgcc or toolchain provided language support libraries such as libgnat, libgfortran or libstdc++ unless options preventing their inclusion are used as well. This typically removes -lc from the link command line, as well as system libraries that normally go with it and become meaningless when absence of a C library is assumed, for example -lpthread or -lm in some configurations.
🌐
Cprogramming
cboard.cprogramming.com › linux-programming › 7808-whats-lm-do-info-just-overwhelming.html
whats the -lm do ? info was just to overwhelming
the -lm option tells gcc to link (that's what the 'l' stands for) your code with the "math" (this what the math library is called) shared library.
Find elsewhere
🌐
Medium
medium.com › @larmalade › gcc-the-hard-way-how-to-include-functions-from-the-math-library-1cfe60f24a7a
gcc The Hard Way: How to Include Functions from the Math Library | by Larry Madeo | Medium
February 12, 2017 - If you want to use functions from the math library in C, it’s not enough to put #include<math.h> at the top of your source code. In addition, you must add the -lm flag to the gcc compiler command in order to use math functions in your C code.
🌐
University of Texas at Austin
users.ece.utexas.edu › ~patt › 05f.360N › labs › gcc.html
Getting Started with gcc, the GNU C compiler
gcc -lm -ansi -o assemble assembler.c If you would like to learn more about gcc, try the following on the UNIX command prompt:
🌐
Silicon Labs
community.silabs.com › s › question › 0D58Y0000ADHCLySQP › gcc-doesnot-auto-link-libma-with-lm
GCC doesnot auto link libm.a with "-lm"
The Silicon Labs Community is ideal for development support through Q&A forums, articles, discussions, projects and resources.
🌐
Narkive
gcc-bugs.gcc.gnu.narkive.com › RDyEiHMr › gcc-lm-and-libm-a-for-mac-os-x
gcc -lm and libm.a for Mac OS X
Post by Vincent Lemaitre Hi, gcc -lm test_isnan.cc ___gxx_personality_v0 This symbol is needed for exception handling in c++, either compile with g++ or use -fno-exceptions or use higher optimizations.
🌐
Ask Ubuntu
askubuntu.com › questions › 1101058 › ld-does-not-find-math-library-lm
gcc - ld does not find math library -lm - Ask Ubuntu
The order of the options on the command line was wrong. See https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc as suggested above.
🌐
MIT
web.mit.edu › 10.001 › Web › Course_Notes › c_Notes › tips_math_library.html
Using the flag -lm (add math library)
Second, link with the math library when you are compiling your program. To do this, use the '-lm' option to the C compiler:
🌐
LabEx
labex.io › tutorials › c-how-to-add-math-library-during-gcc-build-419176
How to add math library during gcc build | LabEx
The -lm flag is essential for linking the standard math library when compiling C programs with GCC.
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › gcc-command-in-linux-with-examples
gcc command in Linux with examples - GeeksforGeeks
October 4, 2025 - gcc -ggdb3 source.c -Wall -o opt · -lm : This command link math.h library to our source file, -l option is used for linking particular library, for math.h we use -lm. gcc -Wall source.c -o opt -lm ·
🌐
Linux Man Pages
man7.org › linux › man-pages › man1 › gcc.1.html
gcc(1) - Linux manual page
rings -fno-dwarf2-cfi-asm ...ecision=style -fforward-propagate -ffp-contract=style -ffunction-sections -fgcse -fgcse-after-reload -fgcse-las -fgcse-lm -fgraphite-identity -fgcse-sm -fhoist-adjacent-loads -fif-conversion -fif-conversion2 -findirect-inlining -finline-functions ...
🌐
Linux Mint Forums
forums.linuxmint.com › board index › main edition support › software & applications
gcc -lm; undefined reference to sqrt - Linux Mint Forums
April 15, 2015 - #include <math.h> To use functions from an external library you need both an #include and a -l parameter to gcc, in the case of sqrt that is #include <math.h> and -lm.
🌐
Parallax Forums
forums.parallax.com › c/c++
propeller gcc command line options ( -lm ?) repeated. — Parallax Forums
February 23, 2025 - propeller-elf-gcc -I . -L . -I /Users/[username]/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L /Users/[username]/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I /Users/[username]/Documents/SimpleIDE/Learn/Simple Libraries/TextDevices/libsimpletext -L /Users/[username]/Documents/SimpleIDE/Learn/Simple Libraries/TextDevices/libsimpletext/cmm/ -I /Users/[username]/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L /Users/[username]/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -o cmm/Test Print.elf -Os -mcmm -m32bit-doubles -fno-exceptions -std=c99 Test Print.c -lm -lsimpletools -lsimpletext -lsimplei2c -lm -lsimpletools -lsimpletext -lm -lsimpletools -lm
🌐
NVIDIA Developer Forums
forums.developer.nvidia.com › graphics / linux › linux
Gcc cannot find -lm, -lc - Linux - NVIDIA Developer Forums
June 16, 2021 - I am compiling C code from another person, using gcc and getting error messages /usr/bin/ld: cannot find -lm /usr/bin/ld: cannot find -lc This is in a new install of RHEL 8.4. How to resolve this problem?