[FIXED] Undefined reference to 'pow', 'floorf' and 'floor'
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
In this article, we have presented 3 ways to fix the error "Undefined reference to 'pow', 'floorf' and 'floor'" while compiling C programs.
Table of contents:
- Error
- Fix 1: Include math.h
- Fix 2: Add flag -lm
- Fix 3: Use LD_PRELOAD
Error
On compiling a C program, you may face any error as follows:
Command:
gcc code.c
Error:
opengenus.c:(.text+0x2a): undefined reference to 'pow'
opengenus.c:(.text+0x4a): undefined reference to 'floor'
opengenus.c:(.text+0x6a): undefined reference to 'floorf'
This can happen with other math functions as well like ceil.
opengenus.c:(.text+0x4a): undefined reference to 'ceil'
Fix 1: Include math.h
Make sure you have included math.h header file. Include it at the top of your C program as follows:
#include <math.h>
Fix 2: Add flag -lm
If you have included the header file, the issue is with your compilation command. You need to add the link flag -lm. It will link the math library.
gcc code.c
to
gcc code.c -lm
Important: Add the lm flag at the end.
Fix 3: Use LD_PRELOAD
The math library is named as libm.so and is usually, located in /lib or /usr/lib in UNIX system like Ubuntu. Go to these locations and try to locate libm.so.
Once you find it, add it to the environment variable LD_PRELOAD.
export LD_PRELOAD="/usr/lib/libm.so"
With these two fixes, the error should be fixed. Best of luck debugging.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.