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
lmmin.c:261:10: warning: implicit declaration of function ‘_finite’; did you mean ‘finite’? [-Wimplicit-function-declaration]
116 | if(!_finite(myvalue)){
or
/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
:
#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:
#define _finite(v) (__builtin_isfinite(v))
In case you want to have code that is compatible with both platforms (Windows and Linux), use
#ifdef __linux__
#define _finite(v) (__builtin_isfinite(v))
#endif
or
#include <math.h>
#ifdef __linux__
#define _finite(v) (isfinite((v)))
#endif