visual-studio cmath.h 和编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3983482/
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
cmath.h and compile errors
提问by nbonneel
I used to work with math.h without any problem. Now, I use an external library which itself has a file called math.h, but which includes < cmath>.
我曾经使用 math.h 没有任何问题。现在,我使用一个外部库,它本身有一个名为 的文件math.h,但其中包含< cmath>.
Adding this library to my project (or even just adding the include directory, without touching the code) now generates tons of errors from < cmath>:
将此库添加到我的项目(甚至只是添加包含目录,而不涉及代码)现在会产生大量错误< cmath>:
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2039: 'acosf' : is not a member of '`global namespace''
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2873: 'acosf' : symbol cannot be used in a using-declaration
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2039: 'asinf' : is not a member of '`global namespace''
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2873: 'asinf' : symbol cannot be used in a using-declaration
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误 C2039:“acosf”:不是“全局命名空间”的成员
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误 C2873:'acosf':不能在 using 声明中使用符号
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误 C2039:“asinf”:不是“全局命名空间”的成员
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误 C2873:'asinf':不能在 using 声明中使用符号
[etc, etc...]
[等等,等等...]
I don't understand why this happens. I am using Visual Studio 2005 and looking on the internet, it seems that this problem is solved under VS 2008. However, I'd like to stay on VS 2005...
我不明白为什么会发生这种情况。我正在使用 Visual Studio 2005 并在互联网上查看,似乎这个问题在 VS 2008 下解决了。但是,我想留在 VS 2005 上......
Including using namespace std;everywhere, or changing the order of my includes doesn't seem to change anything. Defining _STD_BEGINsolves the error, but produce as many in < xlocinfo>.
包括using namespace std;无处不在,或者改变我的包含顺序似乎没有改变任何东西。定义_STD_BEGIN解决了错误,但在< xlocinfo>.
How can this be solved?
如何解决这个问题?
采纳答案by Alex Jasmin
I'm not sure I read your question correctly but it seems odd that a library would ship it's own math.h file.
我不确定我是否正确阅读了您的问题,但图书馆会发布自己的 math.h 文件似乎很奇怪。
Perhaps you are suppose to put the parent directory in your include path so that <my_lib/math.h>can be included without conflicting with your compiler <math.h>?
也许你想把父目录放在你的包含路径中,这样就<my_lib/math.h>可以在不与编译器冲突的情况下被包含<math.h>?
回答by Knox
Same Problem exists in VC 10. I think, that <cmath>includes itself a math.hbut insted of the correct one, which is shipped with VC it uses the one which is created in the User-Project (with different content of course).
同样的问题存在于 VC 10 中。我认为,这<cmath>包括它自己一个math.h但正确的,它随 VC 一起提供,它使用在用户项目中创建的一个(当然具有不同的内容)。
Solution: Do never use a File named math.hin your Project... (Or correct the std somewhere).
解决方案:永远不要使用math.h在您的项目中命名的文件...(或在某处更正 std)。
回答by Mike Sadler
The problem is probably mixing C libraries with C++ conventions. For instance:
问题可能是将 C 库与 C++ 约定混合在一起。例如:
#include <math.h>
namespace TEST {
}
This compiles fine, whereas:
这编译得很好,而:
namespace TEST {
#include <math.h>
}
This generates a large number of spurious errors.
这会产生大量的虚假错误。
Just to confuse the issue:
只是为了混淆问题:
#include <math.h>
namespace TEST {
#include <math.h>
}
This also compiles as it can only be included once (the first time).
这也可以编译,因为它只能包含一次(第一次)。
Hence also:
因此还有:
#include <math.h>
namespace TEST {
#include "SomethingThatIncludesMath.h"
}
Will work, whereas:
将工作,而:
namespace TEST {
#include "SomethingThatIncludesMath.h"
}
Won't.
惯于。
You can also get similar problems by including C++ headers into a *.c file, rather than a *.cpp file.
您也可以通过将 C++ 头文件包含到 *.c 文件而不是 *.cpp 文件中来解决类似问题。
I am sure that other similar mixing of C and C++ can lead to similar problems.
我确信其他类似的 C 和 C++ 混合会导致类似的问题。
回答by Cloud Cho
(1) According to Microsoft, the C2873 means;
(1) 根据微软的说法,C2873 是指;
'symbol' : symbol cannot be used in a using-declaration A using directive is missing a namespace keyword. This causes the compiler to misinterpret the code as a using declaration rather than a using directive.
'symbol' : 不能在 using 声明中使用符号 using 指令缺少命名空间关键字。这会导致编译器将代码误解为 using 声明而不是 using 指令。
(2) Also when I had C2873 with C2039 (I tried to merge CEF3 and Cinder), somehow I bypassed the both error by changing Properties->Configuration Properties->C/C++->Code Generation;
(2) 同样,当我使用 C2873 和 C2039 时(我尝试合并 CEF3 和 Cinder),我通过更改 Properties->Configuration Properties->C/C++->Code Generation 以某种方式绕过了这两个错误;
Enable Minimal Rebuild: Yes(/Gm), Enable C++ Exception: Yes(/EHsc), Enable Function-Level Linking: empty
启用最小重建:是(/Gm),启用 C++ 异常:是(/EHsc),启用函数级链接:空

