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

7
  • 3
    So you put #define _USE_MATH_DEFINES and #include <cmath> as high as possible in your include chain, but that did not change anything? Commented Mar 19, 2014 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. Other systems may have it as an extension. Commented Mar 19, 2014 at 10:36
  • Yes Frédéric I did exactly that Commented Mar 19, 2014 at 10:39
  • 1
    Can you show a minimal code sample demonstrating the issue? Commented Mar 19, 2014 at 10:42
  • I think it's a bit hard because I'm trying to compile the Stellarium source code But I'm pretty sure that it is not an issue related to their code because they explain all in this guide Commented Mar 19, 2014 at 10:49

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Or just define M_PI inside your own Math.h, and only directly include your own header.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.