C++ M_PI 适用于 math.h 但不适用于 Visual Studio 中的 cmath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6563810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
M_PI works with math.h but not with cmath in Visual Studio
提问by hyperknot
I am using Visual Studio 2010. I have read that in C++ it is better to use <cmath>
rather than <math.h>
.
我正在使用 Visual Studio 2010。我读过在 C++ 中最好使用<cmath>
而不是<math.h>
.
But in the program I am trying to write (Win32 console application, empty project) if I write:
但是在我尝试编写的程序中(Win32 控制台应用程序,空项目),如果我这样写:
#define _USE_MATH_DEFINES
#include <math.h>
it compiles, while if I write
它编译,而如果我写
#define _USE_MATH_DEFINES
#include <cmath>
it fails with
它失败了
error C2065: 'M_PI' : undeclared identifier
错误 C2065:“M_PI”:未声明的标识符
Is it normal? Does it matter if I use cmath or math.h? If yes, how can I make it work with cmath?
正常吗?我使用 cmath 还是 math.h 有关系吗?如果是,我怎样才能让它与 cmath 一起工作?
UPDATE: if I define _USE_MATH_DEFINES in the GUI, it works. Any clue why this is happening?
更新:如果我在 GUI 中定义 _USE_MATH_DEFINES,它就可以工作。任何线索为什么会发生这种情况?
回答by Goz
Interestingly I checked this on an app of mine and I got the same error.
有趣的是,我在我的一个应用程序上检查了这个,我得到了同样的错误。
I spent a while checking through headers to see if there was anything undef'ing the _USE_MATH_DEFINES
and found nothing.
我花了一段时间检查标题以查看是否有任何未定义的内容_USE_MATH_DEFINES
,但一无所获。
So I moved the
所以我移动了
#define _USE_MATH_DEFINES
#include <cmath>
to be the first thing in my file (I don't use PCHs so if you are you will have to have it after the #include "stdafx.h"
) and suddenly it compile perfectly.
成为我文件中的第一件事(我不使用 PCH,所以如果你是,你必须在 之后使用它#include "stdafx.h"
),突然它编译完美。
Try moving it higher up the page. Totally unsure as to why this would cause issues though.
尝试将其向上移动到页面上方。完全不确定为什么这会导致问题。
Edit: Figured it out. The #include <math.h>
occurs within cmath's header guards. This means that something higher up the list of #includes is including cmath
without the #define
specified. math.h
is specifically designed so that you can include it again with that define now changed to add M_PI
etc. This is NOT the case with cmath
. So you need to make sure you #define _USE_MATH_DEFINES
before you include anything else. Hope that clears it up for you :)
编辑:想通了。该#include <math.h>
CMATH的头球后卫内发生。这意味着在 #includes 列表中更高的部分包含cmath
没有#define
指定的内容。 math.h
是专门设计的,以便您可以再次包含它,现在定义更改为添加M_PI
等cmath
。 .情况并非如此。因此,#define _USE_MATH_DEFINES
在包含其他任何内容之前,您需要先确定自己。希望可以为您解决问题:)
Failing that just include math.h
you are using non-standard C/C++ as already pointed out :)
math.h
如已经指出的那样,仅包括您正在使用非标准 C/C++ 失败:)
Edit 2: Or as David points out in the comments just make yourself a constant that defines the value and you have something more portable anyway :)
编辑 2:或者正如大卫在评论中指出的那样,让自己成为一个定义值的常量,无论如何你都有更便携的东西:)
回答by Thinkeye
Consider adding the switch /D_USE_MATH_DEFINES to your compilation command line, or to define the macro in the project settings. This will drag the symbol to all reachable dark corners of include and source files leaving your source clean for multiple platforms. If you set it globally for the whole project, you will not forget it later in a new file(s).
考虑将开关 /D_USE_MATH_DEFINES 添加到编译命令行,或在项目设置中定义宏。这会将符号拖动到包含和源文件的所有可到达的暗角,使您的源代码在多个平台上保持干净。如果您为整个项目全局设置它,您以后不会在新文件中忘记它。
回答by rubenvb
This works for me:
这对我有用:
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << M_PI << endl;
return 0;
}
Compiles and prints pi
like is should: cl /O2 main.cpp /link /out:test.exe
.
pi
像 should: 一样编译和打印cl /O2 main.cpp /link /out:test.exe
。
There must be a mismatch in the code you have posted and the one you're trying to compile.
您发布的代码与您尝试编译的代码之间一定存在不匹配。
Be sure there are no precompiled headers being pulled in before your #define
.
确保在您的#define
.
回答by user3533658
This is still an issue in VS Community 2015 and 2017 when building either console or windows apps. If the project is created with precompiled headers, the precompiled headers are apparently loaded beforeany of the #includes, so even if the #define _USE_MATH_DEFINES is the first line, it won't compile. #including math.h instead of cmath does not make a difference.
在 VS Community 2015 和 2017 中构建控制台或 Windows 应用程序时,这仍然是一个问题。如果项目是用预编译头创建的,预编译头显然是在任何 #includes之前加载的,所以即使 #define _USE_MATH_DEFINES 是第一行,它也不会编译。#include math.h 而不是 cmath 没有区别。
The only solutions I can find are either to start from an empty project (for simple console or embedded system apps) or to add /Y- to the command line arguments, which turns off the loading of precompiled headers.
我能找到的唯一解决方案是从一个空项目(对于简单的控制台或嵌入式系统应用程序)开始,或者将 /Y- 添加到命令行参数,这会关闭预编译头的加载。
For information on disabling precompiled headers, see for example https://msdn.microsoft.com/en-us/library/1hy7a92h.aspx
有关禁用预编译头的信息,请参见例如 https://msdn.microsoft.com/en-us/library/1hy7a92h.aspx
It would be nice if MS would change/fix this. I teach introductory programming courses at a large university, and explaining this to newbies never sinks in until they've made the mistake and struggled with it for an afternoon or so.
如果 MS 会更改/修复此问题,那就太好了。我在一所大型大学教授入门编程课程,向新手解释这一点,直到他们犯了错误并为此苦苦挣扎了一个下午左右之后,才开始理解。
回答by αλεχολυτ
According to Microsoft documentation about Math Constants:
根据微软关于数学常数的文档:
The file
ATLComTime.h
includesmath.h
when your project is built in Release mode. If you use one or more of the math constants in a project that also includesATLComTime.h
, you must define_USE_MATH_DEFINES
before you includeATLComTime.h
.
该文件
ATLComTime.h
包括math.h
在发布模式下构建项目的时间。如果在一个还包含 的项目中使用一个或多个数学常量ATLComTime.h
,则必须_USE_MATH_DEFINES
在包含之前定义ATLComTime.h
。
File ATLComTime.h
may be included indirectly in your project. In my case one possible order of including was the following:
文件ATLComTime.h
可能会间接包含在您的项目中。在我的情况下,一种可能的包含顺序如下:
project's
"stdafx.h"
→<afxdtctl.h>
→<afxdisp.h>
→<ATLComTime.h>
→<math.h>
项目的
"stdafx.h"
→<afxdtctl.h>
→<afxdisp.h>
→<ATLComTime.h>
→<math.h>
回答by Den-Jason
As suggested by user7860670, right-click on the project, select properties, navigate to C/C++ -> Preprocessor and add _USE_MATH_DEFINES
to the Preprocessor Definitions.
按照 user7860670 的建议,右键单击项目,选择属性,导航到 C/C++ -> Preprocessor 并添加_USE_MATH_DEFINES
到 Preprocessor Definitions。
That's what worked for me.
这就是对我有用的东西。