3

I'm having problems compiling a c++ project that include cmath with Visual Studio 2012 and I get this error

error C2065: 'M_PI' : undeclared identifier

I've tried this M_PI works with math.h but not with cmath in Visual Studio but it doesn't work at all

How can I solve this issue?

EDIT

the application source code I downloaded a couple of weeks ago was not stable. So today I tried to download the updated sources and now it works like a charm

Community
  • 1
  • 1
nicetoCyou
  • 39
  • 1
  • 5
  • 3
    So you put `#define _USE_MATH_DEFINES` and `#include ` as high as possible in your include chain, but that did not change anything? – Frédéric Hamidi Mar 19 '14 at 10:36
  • 4
    If you do a simple search you would have found out that `M_PI` is not part of the C++ (or the C) standard. They are part of the [POSIX standard though](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/math.h.html). Other systems may have it as an extension. – Some programmer dude Mar 19 '14 at 10:36
  • Yes Frédéric I did exactly that – nicetoCyou Mar 19 '14 at 10:39
  • 1
    Can you show a [minimal code sample demonstrating the issue](http://sscce.org/)? – Angew is no longer proud of SO Mar 19 '14 at 10:42
  • I think it's a bit hard because I'm trying to compile the [Stellarium source code](http://www.stellarium.org/) But I'm pretty sure that it is not an issue related to their code because they explain all in [this guide](http://stellarium.org/wiki/index.php/Compile_with_MSVC2012_and_Qt5_(OpenGL)) – nicetoCyou Mar 19 '14 at 10:49
  • I'm trying to ask them why this appends. I'll tell you if I'm able to solve this issue – nicetoCyou Mar 19 '14 at 11:03
  • Have you tried adding it to Visual Studio's project `Preprocessor Definitions`? – OMGtechy Mar 19 '14 at 12:50

2 Answers2

1

put the following code in a header file and include it, where You need M_PI

#pragma once
#include <cmath>
#ifndef M_PI
namespace
{
    const double M_PI = std::acos(-1.0);
}
#endif
cpp-progger
  • 406
  • 3
  • 6
0

Add this to the header of your program:

#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>

And use M_PI to access the number.