tomstewart89 / BasicLinearAlgebra

A library for using matrices and linear algebra on Arduino
MIT License
185 stars 38 forks source link

Doesn't Compile on Native #75

Closed Dash-L closed 5 months ago

Dash-L commented 5 months ago

I've been trying to setup some unit testing/mocking for my code that will run using PlatformIO's native platform, but this library causes my code not to compile.

Specifically, it is a reference to max in the global namespace, which appears to exist when compiling for arduino (or the rp2040 in my case), but not when compiling for native (see the full error below). I think it would make sense for this library to work on native since it shouldn't require anything hardware specific, and it is reasonable to want to test math heavy parts of the code.

I'm not actually sure if this is a trivial fix, it feels like it would be, but I haven't yet found a way to get it to compile on native without referencing std somewhere (which I presume would make it not work on arduino).

Thanks!

Full error:

.pio/libdeps/native/BasicLinearAlgebra/impl/NotSoBasicLinearAlgebra.h:64:31: error: 'max' was not declared in this scope; did you mean 'std::max'?
   64 |             largest_elem = max(this_elem, largest_elem);
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
      |                            std::max
In file included from /usr/include/c++/13.2/bits/hashtable_policy.h:36,
                 from /usr/include/c++/13.2/bits/hashtable.h:35,
                 from /usr/include/c++/13.2/bits/unordered_map.h:33,
                 from /usr/include/c++/13.2/unordered_map:41,
                 from .pio/libdeps/native/ArduinoFake/src/ArduinoFake.h:8,
                 from .pio/libdeps/native/ArduinoFake/src/Arduino.h:1,
                 from src/EKF/EKF.h:3:
/usr/include/c++/13.2/bits/stl_algobase.h:303:5: note: 'std::max' declared here
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
*** [.pio/build/native/src/EKF/EKF.o] Error 1
Building stage has failed, see errors above. Use `pio test -vvv` option to enable verbose output.
tomstewart89 commented 5 months ago

Hey @Dash-L , thanks for the interest in my library!

Actually all the tests in this repo are run in a standard linux environment so I'm sure you'll be able to set some tests up too. To deal with that pesky max problem I just defined a mock Arduino.h which contains a using std::max;

If you want to run the tests on your local environment just clone this repo and (on linux):

$ mkdir test/build && cd test/build
$ cmake ..
$ make
$ ./test_linear_algebra

Hope that helps!

Dash-L commented 5 months ago

Thanks for the quick reply!

I wasn't quite able to include a mock Arduino.h, since that gave me a lot of name conflicts using PlatformIO's build system and ArduinoFake already defines it's own Arduino.h, but I did find a way of patching the impl/NotSoBasicLinearAlgebra.h file to include the #include <algorithm> and using std::max;

I think this issue can be closed now, thanks again!