-3

I have the following code in my Eclipse project

#pragma once

#ifdef WIN32 // Compiler enters here !
    #define M_PI 3.14159265358979323846
#else
    #include <cmath>
#endif

#ifndef INFINITY
    #define INFINITY FLT_MAX
#endif

inline float Radians(float deg)
{
    return ((float)M_PI/180.f) * deg;
}

Problem is that I'm getting the following error from the compiler

Luzoso.hpp:22:20: error: 'M_PI' was not declared in this scope
     return ((float)M_PI/180.f) * deg;

I don't understand what the problem might be. I built the project with CMake using ECLIPSE CDT4 - MinGW Makefiles as the generator. Any advise?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
BRabbit27
  • 6,333
  • 17
  • 90
  • 161
  • You might try `__MINGW32__` instead of `WIN32`...? Or perhaps `_WIN32`? You can try `-E` to see compiler output after pre-processing, then search that for `M_PI`. – Tony Delroy May 09 '16 at 01:57
  • Your post doesn't indicate which condition in the `#ifdef` was triggered. Was it the `WIN32` condition or not? – PaulMcKenzie May 09 '16 at 01:58
  • Yes it is define. Well at least that's what I think since Eclipse's editor shows the code as active. Is there a way to be 100% sure what part the #if-else the compiler enters to? – BRabbit27 May 09 '16 at 02:06
  • My crystal ball tells me `#define _USE_MATH_DEFINES` is missing (old MinGW, improperly configured CMake, ...) – uh oh somebody needs a pupper May 09 '16 at 02:08

1 Answers1

5

WIN32 isn't the correct macro. It's actually _WIN32. Either way, that's a macro defined by Visual Studio C++, but you're using MinGW so the actual macro to check for is __MINGW32__ or (64). This still is the wrong way to do things, since MSDN requires:

#define _USE_MATH_DEFINES // for C++
#include <cmath>

In order to access the math constants. MinGW already does this.