元高専生のロボット作り

元高専生のロボット作り

主にプログラミング, 電子系について書きます。たまに機械系もやります。メモ代わりの記事ばっか書きます

WSL2環境のPlatformIOでeigenを使用すると,Teensyのabsとroundが衝突する

以下のmainファイルをコンパイルすると,Teensyフレームワークのabsマクロと,Eigenが内部で使用しているstd::abs関数との間で名前の衝突が発生する.
同様にroundも衝突するようだ.

WSL2でのみ起きる現象で,Ubuntuやwindows10では発生しない.

#include <Arduino.h>
#include <Eigen/Core>

void setup()
{
}

void loop()
{
}
Compiling .pio/build/main/src/main.cpp.o                                                                                                                                                                                                     In file included from /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/WProgram.h:46,                                                                                                                                                  from /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/Arduino.h:6,                                                                                                                                                    from src/main.cpp:1:                                                                                                                                                                                                        /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/wiring.h:164:17: error: expected unqualified-id before '{' token                                                                                                       164 | #define abs(x) ({ \                                                                                                                                                                                                                        |                 ^                                                                                                                                                                                                                    /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/wiring.h:164:17: error: expected ')' before '{' token                                                                                                                  164 | #define abs(x) ({ \                                                                                                                                                                                                                        |                ~^                                                                                                                                                                                                                    /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/wiring.h:167:2: error: expected unqualified-id before ')' token                                                                                                        167 | })                                                                                                                                                                                                                                         |  ^                                                                                                                                                                                                                                   /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/wiring.h:164:17: error: expected unqualified-id before '{' token                                                                                                       164 | #define abs(x) ({ \                                                                                                                                                                                                                        |                 ^                                                                                                                                                                                                                    /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/wiring.h:164:17: error: expected ')' before '{' token                                                                                                                  164 | #define abs(x) ({ \                                                                                                                                                                                                                        |                ~^                                                                                                                                                                                                                    /home/hidaka/.platformio/packages/framework-arduinoteensy/cores/teensy4/wiring.h:167:2: error: expected unqualified-id before ')' token                                                                                                        167 | })                                                                                                                                                                                                                                         |  ^                                                                                                                                                                                                                                   In file included from .pio/libdeps/main/eigen/Eigen/Core:78,                                                                                                                                                                                                  from src/main.cpp:2:                                                                                                                                                                                                        /home/hidaka/.platformio/packages/toolchain-gccarmnoneeabi-teensy/arm-none-eabi/include/c++/11.3.1/complex: In function 'std::complex<_Tp> std::__complex_log(const std::complex<_Tp>&)':

対策として,以下のようにEigenをインクルードする前にマクロをundefし,インクルード後にマクロをdefineし直す.

#include <Arduino.h>

#undef abs

#ifdef round
#define ROUND_WAS_DEFINED
#undef round
#endif

#include <Eigen/Core>

#define abs(x) ({ \
  typeof(x) _x = (x); \
  (_x > 0) ? _x : -_x; \
})

#ifdef ROUND_WAS_DEFINED
#define round(x) ((long) __builtin_round(x))
#undef ROUND_WAS_DEFINED
#endif

void setup()
{
}

void loop()
{
}