在 xcode 中包含 cmath 时出现错误:'::acos' 尚未声明等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3939704/
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
In xcode when including cmath get error: '::acos' has not been declared, etc
提问by andrewz
I get the following errors when trying to build a small and simple project that includes <cmath>
in Xcode:
尝试构建包含<cmath>
在 Xcode 中的小而简单的项目时,我收到以下错误:
cmath: '*' has not been declared
'::acos' has not been declared
In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.cp
In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.h
'::acos' has not been declared in /Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.1.sdk/usr/include/c++/4.2.1/cmath
...
cmath: '*' has not been declared
'::acos' has not been declared
In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.cp
In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.h
'::acos' has not been declared in /Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.1.sdk/usr/include/c++/4.2.1/cmath
...
The error log complains about all the other math functions as well, sin
, pow
, etc, not just acos
. I looked inside cmath
source code and it references the globally defined corresponding math functions from math.h
, ie ::acos
etc. Since the root error complains about the non-existance of ::acos
one would assume that math.h
can't be found, but a) it exists, and b) I'd get a different error complaining that math.h can't be found.
错误日志还抱怨所有其他数学函数sin
、pow
、 等,而不仅仅是acos
. 我查看了cmath
源代码,它引用了来自math.h
, ie::acos
等的全局定义的相应数学函数。由于根错误抱怨不存在::acos
一个人会假设math.h
无法找到,但是 a) 它存在,并且 b) 我会得到一个不同的错误,抱怨找不到 math.h。
The source code is as follows:
源代码如下:
libraryLAFMath.cp:
图书馆LAFMath.cp:
#include "libraryLAFMath.h"
libraryLAFMath.h:
图书馆LAFMath.h:
#include <cmath>
struct libraryLAFMath {
void test() {
double a = std::acos(0);
}
};
Now, I have another project from an outside source that uses cmath
and compiles fine. I tried comparing build settings between these two projects but they are pretty much the same. I am using LLVM GCC 4.2 compiler, but get similar result when using GCC 4.2, so it's not a compiler settings issue I believe.
现在,我有另一个来自外部来源的项目,它可以cmath
很好地使用和编译。我尝试比较这两个项目之间的构建设置,但它们几乎相同。我使用的是 LLVM GCC 4.2 编译器,但在使用 GCC 4.2 时得到类似的结果,所以我相信这不是编译器设置问题。
I'm new to Xcode development and any help is appreciated.
我是 Xcode 开发的新手,任何帮助表示赞赏。
回答by andrewz
There is a file I have in my project named Math.h
with a capital M
, and it seems the compiler gets confused and tries to include Math.h
instead of math.h
.
我的项目中Math.h
有一个以大写命名的文件M
,似乎编译器感到困惑并试图包含Math.h
而不是math.h
.
回答by user2465201
I posted this answer on an alternate thread on the topic, but thought it worth including here as well:
我将此答案发布在该主题的备用线程上,但认为也值得包括在此处:
I had this problem - it was driving me crazy but I tracked down the cause, and it was a little different than what I've seen reported on this issue.
我遇到了这个问题 - 它让我发疯,但我找到了原因,这与我在此问题上看到的报告略有不同。
In this case, the general cmath header (or math.h - the error and solution occur in C++ or C) had architectural environment switches to include architecture specific math subheaders. The architecture switch (environment variable) hadn't been defined, so it was punting and not actually including the headers that truly defined the math functions.
在这种情况下,通用 cmath 头文件(或 math.h - 错误和解决方案出现在 C++ 或 C 中)具有架构环境切换以包含架构特定的数学子标题。体系结构开关(环境变量)尚未定义,因此它是无意义的,实际上并不包含真正定义数学函数的头文件。
So there was indeed a single math.h or cmath.h, and it was included, but that wasn't enough to get the math functions. In my case, rather than define the architectural variable, I instead found the location of the correct sub math headers and added them to my compile path. Then the project worked!
所以确实有一个 math.h 或 cmath.h,并且它被包含在内,但这还不足以获得数学函数。在我的例子中,我没有定义架构变量,而是找到了正确的子数学标头的位置并将它们添加到我的编译路径中。然后项目成功了!
This seems to be an issue that comes up a lot when porting Linux projects to OS-X. I'd imagine it might occur anytime a project was moved betwee platforms such that the standard library headers are arranged differently.
在将 Linux 项目移植到 OS-X 时,这似乎是一个经常出现的问题。我想它可能会在项目在平台之间移动时发生,这样标准库头文件的排列方式就会不同。