How to fix GCC undefined reference to `_finite' or implicit declaration of function ‘_finite’
Problem:
If you’re trying to compile Windows code on Linux, you will often see messages like
linker_warning.txt
lmmin.c:261:10: warning: implicit declaration of function ‘_finite’; did you mean ‘finite’? [-Wimplicit-function-declaration]
116 | if(!_finite(myvalue)){or
linker_output.txt
/usr/bin/ld: mymath.c:(.text+0x1057): undefined reference to `_finite'and your code won’t compile.
Solution
_finite is a function that is only available on Windows. In order to use it on Linux using GCC or G++, one option is to use isfinite() from math.h:
finite_macro.c
#include <math.h>
#define _finite(v) (isfinite((v)))In case that function is not available (like on some Microcontroller platforms), you can use __builtin_isfinite(). Note that glibc defines isfinite() as an alias for __builtin_isfinite(). Use it like this:
finite_builtin.c
#define _finite(v) (__builtin_isfinite(v))In case you want to have code that is compatible with both platforms (Windows and Linux), use
finite_compat.c
#ifdef __linux__
#define _finite(v) (__builtin_isfinite(v))
#endifor
finite_compat_alt.c
#include <math.h>
#ifdef __linux__
#define _finite(v) (isfinite((v)))
#endifCheck out similar posts by category:
C/C++, GCC Errors
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow