views:

521

answers:

5

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....

+1  A: 

Did your forget the simple

#include <cmath>

and/or the -lm linker option?

Dirk Eddelbuettel
`<cmath>` and not `<cmath.h>`. :D
Jonathan Leffler
Ooops. Thank you.
Dirk Eddelbuettel
-lm is not needed in C++
David Rodríguez - dribeas
The compiler error is IN the cmath header.
trikker
A: 

Have you added the compiler flags/paths to these libraries?

Charles Ma
A: 

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 the acos() 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.


The program is able to access the cmath header, the error is in the cmath header itself.

In that case, you will probably need to provide a global acos() function (declaration at least, possibly definition too) that calls onto std::acos():

double acos(double x) { return std::acos(x); }

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:

extern double std::acos(double);
double acos(double x) { return std::acos(x); }
#include <cmath>

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?

Jonathan Leffler
The program is able to access the cmath header, the error is in the cmath header itself.
trikker
Also, there is `<math.h>` that could be used to declare global `acos()` - perhaps...
Jonathan Leffler
+3  A: 

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)

#include <cmath>
#include <iostream>

int main()
{
   std::cout << std::fabs( -10.5 ) << std::endl;
}
David Rodríguez - dribeas
A: 

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?

Partial
Code::Blocks. There is an error for pretty much every function in cmath so I can't post them all. The errors all say that they haven't been declared.
trikker
*the functions haven't been declared.
trikker
Where are you using them?
Partial
The simple_window and graph header use fltk headers which in turn use cmath.
trikker
What IDE/compiler are you using and what version of FLTK are you using?
Partial
Code::Blocks. I downloaded the FLTK from here. http://www.stroustrup.com/Programming/FLTK/
trikker