cmath 函数生成编译器错误
我编写了一个利用 Fast Light Toolkit 的小程序,由于某种原因,在尝试访问 cmath 标头中的函数时会生成编译器错误。
如报错::acos has not be声明。
几乎它尝试在标头中使用的每个函数都会发生这种情况。 我可能会错过什么?
我包含的头文件都是
Simple_window.h
Graph.h
FLTK 的一部分。
代码是这样的:
#include "Simple_window.h" // get access to our windows library
#include "Graph.h" // get access to graphics library facilities
int main()
{
using namespace Graph_lib; // our graphics facilities are in Graph_lib
Point tl(100,100); // to become top left corner of window
Simple_window win(tl,600,400,"Canvas"); // make a simple window
Polygon poly; // make a shape (a polygon)
poly.add(Point(300,200)); // add a point
poly.add(Point(350,100)); // add another point
poly.add(Point(400,200)); // add a third point
poly.set_color(Color::red); // adjust properties of poly
win.attach(poly); // connect poly to the window
win.wait_for_button(); // give control to display engine
}
编辑:这是生成编译器错误时的示例代码。 这是在 cmath 标头内。
namespace std
{
// Forward declaration of a helper function. This really should be
// an `exported' forward declaration.
template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
inline double
abs(double __x)
{ return __builtin_fabs(__x); }
inline float
abs(float __x)
{ return __builtin_fabsf(__x); }
inline long double
abs(long double __x)
{ return __builtin_fabsl(__x); }
using ::acos; //ERROR HERE
inline float
acos(float __x)
{ return __builtin_acosf(__x); }
inline long double
acos(long double __x)
{ return __builtin_acosl(__x); }
template<typename _Tp>
inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
acos(_Tp __x)
{
return __builtin_acos(__x);
}
编辑:Code::blocks 正在将文件保存为 C 文件....
I've written a small program that utilizes the Fast Light Toolkit and for some reason a compiler error is generated when trying to access the functions in the cmath header.
Such as error ::acos has not been declared.
This goes on for pretty much every function it tries to use in the header. What could I be missing?
The header files I have included are
Simple_window.h
Graph.h
both of which are part of the FLTK.
The code is this:
#include "Simple_window.h" // get access to our windows library
#include "Graph.h" // get access to graphics library facilities
int main()
{
using namespace Graph_lib; // our graphics facilities are in Graph_lib
Point tl(100,100); // to become top left corner of window
Simple_window win(tl,600,400,"Canvas"); // make a simple window
Polygon poly; // make a shape (a polygon)
poly.add(Point(300,200)); // add a point
poly.add(Point(350,100)); // add another point
poly.add(Point(400,200)); // add a third point
poly.set_color(Color::red); // adjust properties of poly
win.attach(poly); // connect poly to the window
win.wait_for_button(); // give control to display engine
}
Edit: Here is example code of when the compiler error is generated. This is inside the cmath header.
namespace std
{
// Forward declaration of a helper function. This really should be
// an `exported' forward declaration.
template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
inline double
abs(double __x)
{ return __builtin_fabs(__x); }
inline float
abs(float __x)
{ return __builtin_fabsf(__x); }
inline long double
abs(long double __x)
{ return __builtin_fabsl(__x); }
using ::acos; //ERROR HERE
inline float
acos(float __x)
{ return __builtin_acosf(__x); }
inline long double
acos(long double __x)
{ return __builtin_acosl(__x); }
template<typename _Tp>
inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
acos(_Tp __x)
{
return __builtin_acos(__x);
}
Edit: Code::blocks is saving files as C files....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您包含标准 C 库的 C++ 版本 () 时,所有符号都在 std 命名空间内定义。 在 C++ 中,您不需要链接数学库(不需要 -lm)
When you include the C++ version (<cXXXX>) of standard C libraries all the symbols are defined within the std namespace. In C++ you do not need to link against the math library (-lm is not required)
我遇到了这个问题 - 它让我发疯,但我找到了原因,它与我所看到的有关此问题的报道有点不同。
在这种情况下,通用 cmath 标头(或 math.h - C++ 或 C 中发生的错误和解决方案)具有架构环境切换以包含特定于架构的数学子标头。 架构开关(环境变量)尚未定义,因此它是双关语,实际上并不包括真正定义数学函数的标头。
所以确实有一个 math.h 或 cmath.h,并且它被包含在内,但这还不足以获得数学函数。 就我而言,我没有定义架构变量,而是找到了正确的子数学标头的位置并将它们添加到我的编译路径中。 然后这个项目就成功了!
当将 Linux 项目移植到 OS-X 时,这似乎是一个经常出现的问题。 我想它可能会发生在项目在平台之间移动的任何时候,这样标准库头的排列方式就会不同。
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.
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!
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.
由于您的代码(如上所示)不直接调用
acos()
,因此您使用的标头之一可能存在错误。 似乎其中一个标头中有一些(内联)代码调用acos()
函数,但未确保该函数已正确声明。 这可能是宏或内联函数。最好的解决方法是确保标头是独立的 - 更改标头。
如果这是不可能的,解决方法是在源代码中包含适当的标头(可能是
#include
)。情况下,您可能需要提供一个调用
std::acos()
的全局acos()
函数(至少是声明,也可能是定义):在这种 确保它不在任何名称空间内 - 即使是匿名名称空间。 (检查在 MacOS X 上使用 G++ 4.0.1 编译的情况,前面有“
#include
”。鉴于您有一个有问题的
标头,你可能需要想一想:这太令人讨厌了 - 你确定你的编译器没有修复错误的版本吗?
你有没有可能得到 '
#include
' 在命名空间内?Since your code as shown above does not directly call
acos()
, there is arguably a bug in one of the headers that you do use. It appears there is some (inline) code in one of the headers that invokes theacos()
function without ensuring that the function is properly declared. This might be a macro or an inline function.The best fix is to ensure that the headers are self-contained - change the headers.
If that is not possible, the hackaround is to include the appropriate header (
#include <cmath>
, probably) in the source code.In that case, you will probably need to provide a global
acos()
function (declaration at least, possibly definition too) that calls ontostd::acos()
:Just make sure this is not inside any namespace - not even the anonymous one. (Check compiled with G++ 4.0.1 on MacOS X, with '
#include <cmath>
' preceding it. Given that you have a problematic<cmath>
header, you might need to get fancy:This is pretty nasty - are you sure there isn't a bug-fixed version of your compiler?
Is there any chance that you've got '
#include <cmath>
' inside a namespace?在 Visual C++ 中,在不使用
cmath
的程序中也会发生这种情况。我发现问题是我使用了
main.c
文件而不是main.cpp
文件。It also happens in Visual C++, in programs that do not sapuse to use
cmath
.I found that the problem is that I used
main.c
file instead ofmain.cpp
file.该错误很可能出现在您的代码中,而不是 cmath 中......除非您更改了 cmath 中的某些内容。 您能否复制错误并告诉我们您用来编程的应用程序是什么?
The error is most likely to be in your code and not in cmath... unless you changed something in cmath. Could you copy the errors and tell us what is the application you are using to program?