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

Answer from Eliah Kagan on askubuntu.com
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_math.php
C Math Functions
To use them, you must include the math.h header file in your program: ... The ceil() function rounds a number upwards to its nearest integer, and the floor() method rounds a number downwards to its nearest integer, and returns the result:
Discussions

Is #include <math.h> necessary?
The compiler's are taking their best guess that you want the pow() function from math.h and are telling you to either properly include the header or define your own pow function. If your intent is to use the built in pow() function you need to include the header both for clarity and to ensure the code can be compiled with stricter warning and error settings. If you tried to compile with the -Werror flag it would fail. More on reddit.com
๐ŸŒ r/C_Programming
14
6
January 31, 2023
gcc - C Failing to compile: Can't find math.h functions - Stack Overflow
I'm writing a prime number finder. Mathematically, it is faster to, instead of doing for (unsigned long i = 2; i More on stackoverflow.com
๐ŸŒ stackoverflow.com
compilation - Why do you have to link the math library in C? - Stack Overflow
If I include or in a C program, I don't have to link these when compiling, but I do have to link to , using -lm with GCC, for example: gcc test.c -o t... More on stackoverflow.com
๐ŸŒ stackoverflow.com
C89 help importing math functions from a text file.

There's no "execute this as if it were code" in C because C is a compiled language- there's nothing that understands C code at run time. You need to write a parser to split each equation up into parts and then write code to handle each math operation.

More on reddit.com
๐ŸŒ r/learnprogramming
4
2
July 4, 2013
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-library-math-h-functions
C Library math.h Functions - GeeksforGeeks
April 3, 2023 - // C code to illustrate // the use of ceil function. #include <math.h> #include <stdio.h> int main() { float val1, val2, val3, val4; val1 = 1.6; val2 = 1.2; val3 = -2.8; val4 = -2.3; printf("value1 = %.1lf\n", ceil(val1)); printf("value2 = %.1lf\n", ceil(val2)); printf("value3 = %.1lf\n", ceil(val3)); printf("value4 = %.1lf\n", ceil(val4)); return (0); }
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ math.h
C math.h
The C <math.h> header file declares a set of functions to perform mathematical operations such as: sqrt() to calculate the square root, log() to find natural logarithm of a number etc.
๐ŸŒ
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 - Basically, libc (the standard C library) is loaded by default, ready to be linked to, so that all the standard library header files are there for the preprocessor to load into your source code. But the math library (libm) contains all the functions related to manipulating variables of the type float , and back in the day, these floating point math functions were quite large and CPU intensive relative to the available computing power, and programmers didnโ€™t want to load libm by default for this reason.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ is #include necessary?
r/C_Programming on Reddit: Is #include <math.h> necessary?
January 31, 2023 -

So I started to study C and noticed something interesting.

TL;DR:

Building the code gives me a warning/error, telling me to declare the function or to include math.h. But I can ignore that message and still run the code and the function gets executed. I build the code by using gcc code_name.c -o code_name or just "run code" with Vs Code's Code Runner. Both will give me the error but running the code with ./code_name works fine, the functions are applied. I can ignore the error message and still run the code. If you use Replit, it works as well, without any messages.

I use VS Code and run my code in two different ways. Either Code-Runner or the Terminal, usually Code-Runner and Terminal when it doesn't work with it.

When running the following code:

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

int main(){
    printf("%f\n", pow(2,3));
    return 0;
}

This happens with Code-Runner:

>https://imgur.com/a/YsgSyvq (First Image)

Note that Code-Runner automatically creates the .exe and I can run the code with the terminal (PowerShell) by using

./code_name

>https://imgur.com/a/YsgSyvq (Second Image)

But again, when I try to use

gcc Working_Numbers.c -o WN     

to build the code, it sends a warning message, telling me that I must use math.h or declare pow() (the math function).

But I can still do

./WN

And the code runs without issues.

>https://imgur.com/a/YsgSyvq (Third Image)

So does C already have these math functions built into it or is #include <math.h> necessary?

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ math_h.htm
C Library - <math.h>
Python TechnologiesDatabasesComputer ProgrammingWeb DevelopmentJava TechnologiesComputer ScienceMobile DevelopmentBig Data & AnalyticsMicrosoft TechnologiesDevOpsLatest TechnologiesMachine LearningDigital MarketingSoftware QualityManagement Tutorials View All Categories ... The math.h header defines various mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result.
Find elsewhere
๐ŸŒ
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: ... Make sure the '-lm' option is the last item on the line. A common mistake is to forget this option when compiling, which results in error messages informing you that ...
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programming-questions โ€บ compiling-program-with-math-library-linux.aspx
Compiling C program with math.h library in Linux โ€“ IncludeHelp
Let's understand with an example: Here is a program that will read an integer number and we want to get square root and cube of entered integer number. As we are aware that sqrt() is used to get square root and pow() is used to get power of any number. #include <stdio.h> #include <math.h> int main() { int num; float sqrRoot; long cube; printf("Enter any integer number: "); scanf("%d",&num); sqrRoot=sqrt(num); cube=pow(num,3); printf("Number: %d \nSquare Root: %f \nCube: %ld \n",num,sqrRoot,cube); return 0; }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-library-math_h
C Library - math.h - GeeksforGeeks
July 23, 2025 - The <math.h> header file in C contains the standard math library functions which can be utilized for various mathematical operations. All math.h library functions accept double type as an argument and return double as a result.
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ math-functions
Math Functions in C Programming (Full List With Examples)
5 days ago - Learn about math functions in C programming with easy-to-understand examples. Learn how to use each function with clear syntax and practical applications.
Top answer
1 of 14
324

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.

2 of 14
104

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".

๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2371716 โ€บ import-math-from-math-import-ceil
Import math, from math import ceil | Sololearn: Learn to code for FREE!
First import...... Then write the code For ex : Import ...... //Your code ... In the second case. Don't type 'math.ceil' Try only 'ceil'. You are not importing the whole math module in the second case, you are just taking its one part.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-how-to-add-math-library-during-gcc-build-419176
How to add math library during gcc build | LabEx
April 3, 2023 - By mastering the techniques of linking math libraries during GCC builds, C programmers can significantly expand their computational capabilities. This tutorial provides essential insights into library integration, compiler flags, and practical strategies for implementing mathematical functions in C programming projects.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ c89 help importing math functions from a text file.
r/learnprogramming on Reddit: C89 help importing math functions from a text file.
July 4, 2013 -

I need to run math functions from a text file but I dont know how to do this efficiently. At the moment I am just comparing the string input to case statements but it quickly gets out of hand with the amount needed. Does anyone know how to import math functions from a text file and run them? For example x*sin (x)/cos (x).

๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-how-to-link-math-library-in-c-compilation-419183
How to link math library in C compilation | LabEx
Learn essential techniques for linking math libraries in C programming, optimize compilation process, and enhance mathematical computations with practical coding strategies.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_ref_math.php
C math (math.h) Library Reference
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The <math.h> library has many functions that allow you to perform mathematical tasks on numbers.
๐ŸŒ
Medium
medium.com โ€บ @Dev_Frank โ€บ math-functions-in-c-16248fe98ae8
MATH FUNCTIONS IN C. When working with numbers in C, haveโ€ฆ | by Dev Frank | Medium
April 21, 2024 - When working with numbers in C, have you ever wondered how to perform complex calculations like trigonometric functions, squares and so on. In this topic, weโ€™ll explore the MATH functions in C, how to use them, and some practical examples to get you started with calculations in C programming.
๐ŸŒ
Quora
quora.com โ€บ When-do-we-use-math-h-in-C
When do we use math.h in C? - Quora
January 21, 2023 - Answer (1 of 17): First of all allow me to correct you, there is no such thing as #math.h in C programming. you have a header file #include the header files are included in a program so that we can access the operations or functions that are available under this header. There are many h...