Am working on a project which, on my first attempt, needs me to integrate some existing c++ lib NOT written specifically for Arduino.
The problem is stemming from the existing code using...
#include <math.h>
#include <cmath>
...to define "generic" math functions in the std namespace (I'm not experienced in C++, but as I understand it, it makes explicit non-overloadable functions e.g. std::min, std::max, etc. which is what I understand the #include cmath to be responsible for)
Trying to verify/compile anything with the above code for Arduino results in "fatal error: cmath: No such file or directory"
This can be replicated in a bare sketch with just the #includes:
Can anybody help me with what's going wrong and why, so I can go about changing this code to be suitable for Arduino? Maybe I've either not grasped something fundamental about the difference between "Arduino" and "C++", or I've got a corrupted install perhaps...
No hits for cmath in the Arduino install directory or the Manage Libraries tool....
Thanks for the response. I can't speak for the original programmer's intent, but as I wrote in the original post I understand it to define math functions in the standard namespace e.g. std::max, std::min, etc. which are used throughout the existing code I'm trying to integrate. Whether this is correct I have no idea...
An #include statement expects to have a filename as its parameter such as math.h but as you have no file named math on your system, as pointed out by the error message, then the code will not compile
Where did the code come from ?
In the Arduino environment you do not need to #include math.h to use functions such as max() and min), the pre-processor takes care of it for you
and therefore it is not finding the cmath header file presumably because it's not part of the stock Arduino install. There is no apparent issue with #include <math.h>. As I understand it there is no requirement for a file extension to #include a header in C/C++
Somebody that knows C++ and not used Arduino before. It's not in the public domain, we're working together (live) on a project. I'd described Arduino as basically C++ so he should be able to write C++ and it will work.
Looks like I was at least a bit wrong so I'm trying to figure out where I went wrong and what we should be doing instead
I can see exactly how this sketch works and have no issue understanding it. I'm trying to figure out why the (apparently) "normal" c++ #includes in the original post are not Arduino compatible (seemingly)
Can you expand on that? You seem certain, but as far as I can tell it's very common. for e.g. if you install minGW/GCC it's in there, completely stock. If you can send a link or reference that would be super helpful as I'm trying to understand but not following this assertion.
Thanks for the reply. Out of interest is it concisely explainable why an AVR processor can't handle whatever's going on with whatever cmath does? Or a reference? My searches are turning up nothing so I'm fairly sure I'm plugging garbage in...
I assume instead you mean chuck the Arduino and use a different board if I'm stuck with using cmath functions? If that's the case we can certainly change it. I'm mainly puzzled as to how the common phrase "Arduino language is C++" can be reconciled with the apparent fact the first code my C++ literate friend writes being incompatible with Arduino. (slow I could understand, but an unusable C++ standard lib seems like I've missed the plot somewhere!)
I saw it thanks - your response made more sense to me but UKHeliBob made an assertion that I'm assuming there is some substance behind, and I've no idea how to arbitrate this one
I assume it's memory restrictions of the AVRs .... same reason the STL isn't supported.
There are many boards in the Arduino Ecosystem besides AVR-based ones. Some are manufactured by Arduino proper and some by third parties.
Simple. As I suggested, try compiling the code for a ESP8266/32 or ARM-based board. That should clear the cmath problem and expose whatever the next issue may be.
I follow you but I'm not just trying to clear the cmath issue to find the next issue, I'm trying to understand where the cmath gremlin leapt out from and how many more gremlins are in there (including STL thanks for the heads up). I see that I think UKHeliBob is in agreement cmath is probably standard so I'm clear on that front now.
I have discovered this which suggests cmath should work on at least some/one AVR based processors, but could be a red herring all the same. I think we're just going to #include <Arduino.h> and use the Arduino math functions and hope some deeper understanding manifests along the way
@timnbd, why do you refuse to try this? Install one of those board packages into the Arduino IDE, select an appropriate board under the Tools menu, and give it a shot.
Oh it's not that I don't believe you, I do, I'm just trying to figure out how I could have pre-empted this and refine my understanding of "Arduino language is C++". Also I don't have a non-AVR board for this project, I have only AVR boards, so it'd be tangential at best
I would say, there is no such thing as a Arduino language. The Arduino IDE does some preprocessing but at the end of the day it uses a standard c/c++ compiler suitable for the target processor. For the AVR boards this is avr-gcc which (AFAIK) only supports a subset of modern c++. For the ARM boards the Arduino IDE uses the normal gcc in various versions. All of them (again AFAIK) support c++ in language versions >= c++11.
In c++ the c-headers math.h stdint.h, stdlib.h .... are depreciated and are replaced by the corresponding 'cxxx' headers. GCC still provides the old headers for compatibility with legacy code. Here some more information about this: http://www.cplusplus.com/reference/clibrary/
I would say, if you look for compatibility you are best off using the old c-headers. If you don't use AVR processors and want to be compliant to c++ standards the 'cxxxx' headers might be a better fit.