VS中M_PI问题

时间:2022-06-30 07:20:40
In my app I use the constant M_PI.
As recommended, in my cpp file I wrote:

#define _USE_MATH_DEFINES
#include <cmath>

The compiler complains: "error C2065: 'M_PI' : undeclared identifier"

I have transferred the solution and source files from the Visual Studio 2005 Standard. In this version of VS all was compiled and linked just fine.

Replacing the <cmath> with <math.h>

#define _USE_MATH_DEFINES
#include <math.h>

solves the problem: all compiles OK.

Also if I define _USE_MATH_DEFINES in ProjectProperties/C++/Prepocessor
and #include <cmath> in the cpp module, all works.  详细信息 (展开)
Product LanguageEnglish
Version
Visual Studio 2008 (All Products and Editions)
Operating SystemWindows XP Home Edition
Operating System LanguageEnglish
Steps to ReproduceIn cpp file:
#define _USE_MATH_DEFINES
#include <cmath>

In any function or class enter

float a = tan(M_PI/4);

Actual Resultserror C2065: 'M_PI' : undeclared identifier
Expected ResultsNo error
TAP Code (if applicable)
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]



文件附件 文件名  提交者  提交时间  文件大小    
Gradiator.zip(受限)  Geoyar  2008/5/19  -  

评论 (4)解决方法 (0)
登录 发布 评论。

请输入评论。
 
由 Microsoft 在 2008/5/18 22:34 发送
Thanks for reporting the issue.
In order to fix the issue, we must first reproduce the issue in our labs. We are unable to reproduce the issue with the steps you provided. We tried to repro with the following steps:

1. Tried to reproduce this issue with Visual Studio 2008 Standard Edition
2. Tried to reproduce this issue with VS 2008 Team Suite

It may help if you provide us with:

1. a demo project.
2. more snapshots.

If we do not receive a response from you after 7-days , we will automatically close your issue. There is no obligation to respond -- at any time you may edit your issue via Connect and change the status to “Active.”

Thank you,
Visual Studio Product Team
由 Geoyar 在 2008/5/19 9:23 发送
Hi,

I attached the project files (zipped) to my feedback (Gradiator.zip). The zip also includes the solution file (there are several projects in this solution.)

The project is a thing in progress, but you might compile and run it.
The problem occured in the file RGBCube.cpp in the debug configutation.

thank you.

GY
My mashine is HP Pavilion a320n running MS Windows XP Home SP3; 512 MB memory, 80 GB HD.
I have MS VS2003 Enterprise, MS VS 2005 and 2008 Standard and MS VS 2008 Express installed.
由 Microsoft 在 2008/5/28 2:18 发送
Thanks for your response. We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Thank you,
Visual Studio Product Team
由 Microsoft 在 2008/8/7 19:54 发送
Hi,

Thanks for reporting this issue. I have resolved it as Not Repro, as this compiled with VC9 RTM + Feature Pack Refresh:

C:\Temp>type meow.cpp
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;

int main() {
    double a = tan(M_PI / 4);
}

C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp
meow.cpp(6) : warning C4189: 'a' : local variable is initialized but not referenced

Because your code works when you define _USE_MATH_DEFINES through the IDE, when you define it in your source file, you must be doing it too late.

I note that RGBCube.cpp includes stdafx.h before <cmath>. stdafx.h includes <limits>, which includes <cmath> (the Standard headers are allowed to include each other in unspecified ways). In fact, <limits> has included <cmath> since 9/26/2000, the beginning of our version control history. Therefore, I'm uncertain as to why you began seeing this between VS 2005 and VS 2008. I'm also uncertain as to why swapping <cmath> with <math.h> makes things work for you.

Regardless of what's really going on, when you use macros like _USE_MATH_DEFINES to control the behavior of the Standard Library headers, you must do so before those headers are actually included. The best way to ensure this is to define these macros on the command line (or through the IDE, which is equivalent). Otherwise, you should define them in a central header (like a PCH), *before* any other headers have been included.

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
source:
http://connect.microsoft.com/VisualStudio/feedback/details/344610/visual-c-2008-standard-m-pi-is-not-defined