dcastello
dcastello

Reputation: 35

Compiling a Linux application on OS X - not finding standard library (cmath)

I'm attempting to build an application on OS X that was written on Linux, but I'm running into a slew of errors in a helper file. The helper is a wrapper on cmath of the form:

#include <cmath>

namespace [application name]
{   
  namespace math
  {
    template<typename T>
    inline T Log10(const T& x)
    {
      return T(log10(static_cast<double>(x)));
    }
  }
}

And I'm getting errors of the form:

/pathtofile/common/math/Helpers.t:132:16: error: use of undeclared identifier 'log10'; did you mean 'Log10'?
  return T(log10(static_cast<double>(x)));
           ^
/pathtofile/common/math/Helpers.t:130:14: note: 'Log10' declared here
inline T Log10(const T& x)

I've done some research on this problem and someone on Stack Overflow said the following:

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 between platforms such that the standard library headers are arranged differently.

Is this the problem? If so, how do I solve this (I'm not sure how to follow his advice)?

Edit: If I reference the math functions by namespace std, I get the following:

/pathtofile/common/math/Helpers.t:62:14: error: no member named 'acos' in namespace 'std'; did you mean 'ACos'?
  return std::acos(x);
         ^~~~~
/pathtofile/common/math/Helpers.t:60:14: note: 'ACos' declared here
inline T ACos(const T& x)

Edit2: If it's relevant, I'm attempting to build this project with cmake 3.0. The code itself runs fine in isolation, so the problem would seem to be elsewhere.

Upvotes: 0

Views: 335

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69882

$ cat > chk.cpp
#include <cmath>

namespace app
{
    namespace math
    {
        template<typename T>
        inline T Log10(const T& x)
        {
            return T(log10(static_cast<double>(x)));
        }
    }
}

int main()
{

    return app::math::Log10(100.0);
}
$ c++ -std=c++14 chk.cpp
$ ./a.out
$ echo $?
2

I don't think the problem is here.

Upvotes: 1

Related Questions