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:
- Fix 1: Include math.h
- Fix 2: Add flag -lm
- Fix 3: Use LD_PRELOAD
On compiling a C program, you may face any error as follows:
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:
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 -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.
With these two fixes, the error should be fixed. Best of luck debugging.