xiph / speexdsp

Speex audio processing library - THIS IS A MIRROR, DEVELOPMENT HAPPENS AT https://gitlab.xiph.org/xiph/speexdsp
https://speex.org
Other
490 stars 191 forks source link

Build failure - error C2065: 'M_PI': undeclared identifier` #49

Open Dutchman101 opened 5 months ago

Dutchman101 commented 5 months ago

Due to build error (math library non-conformance), the project can't be built on certain platforms/IDE's. For this issue report i reproduced it using Visual Studio 2022 17.10.

The error:

speexdsp\libspeexdsp\testresample2.c(57,43): error C2065: 'M_PI': undeclared identifier

Happens here: libspeexdsp/testresample2.c Line 57

According to web sources, it is because 'M_PI' is not part of math.h or cmath in either C or C++, therefore i can imagine this out-of-the-box failure will appear for multiple environments.

Web sources suggest one of these solutions. I suggset speexdsp picks one for implementation.

Microsoft way (docs): "Math Constants are not defined in Standard C/C++. To use them, you must first define _USE_MATH_DEFINES and then include cmath or math.h BEFORE any other headers":

#define _USE_MATH_DEFINES
#include <cmath>
#incude other headers...

Different approaches, as you decide on portability and choice:

const double pi = 3.14159265358979323846;
#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

GCC way:

#undef __STRICT_ANSI__
#include <cmath>

Compilers that support C++ 20 (use header numbers and constant pi), like so:

#include <numbers>
std::numbers::pi

cc @tmatth