Another Quick Question: M_PI not defined when running sketch in applab

When running a sketch that uses a DFROBOT BMM150 library in IDE 2 it will compile and run without issues. However when running the sketch in App Lab the library throws an error message:
Sketch profile configured: FQBN="arduino:zephyr:unoq", Port=""
The library ArxContainer has been automatically added from sketch project.
The library ArxTypeTraits has been automatically added from sketch project.
The library DFRobot_BMM150 has been automatically added from sketch project.
The library DebugLog has been automatically added from sketch project.
The library MsgPack has been automatically added from sketch project.
The library SparkFun BMI270 Arduino Library has been automatically added from sketch project.
/home/arduino/.arduino15/internal/DFRobot_BMM150_1.0.0_98c1a4170799e41f/DFRobot_BMM150/DFRobot_BMM150.cpp: In member function 'float DFRobot_BMM150::getCompassDegree()':
/home/arduino/.arduino15/internal/DFRobot_BMM150_1.0.0_98c1a4170799e41f/DFRobot_BMM150/DFRobot_BMM150.cpp:229:26: error: 'M_PI' was not declared in this scope
229 | return compass * 180 / M_PI;

So question why is it being found under ide2 and not in app lab? think everything is updated.

IDE 2 includes tons of things.

Try to explicitly include #include <math.h> at the top of the sketch or the library, or define M_PI manually with #define M_PI 3.14159265358979323846 before it’s used.

Although the M_PI macro is defined as an extension by some implementations of the C++ standard library (example), it is not part of the official C++ specification:

The C++ standard library that is part of the UNO Q's toolchain does contain a definition of the macro, but conditionally:

The configuration of the "UNO Q Board" platform does not result in the conditions being met for the macro's definition under normal conditions.

I was able to get the definition via adjustment of the sketch code alone using the advice at the issue above in a simple case:

#define _XOPEN_SOURCE 700
#include <Arduino.h>  // Transitively #includes math.h
float foo = M_PI;
void setup() {}
void loop() {}

However, that approach doesn't work in a case like the "DFRobot_BMM150" library, where the macro is referenced via a separate translation unit from that of the sketch .ino files:

#define _XOPEN_SOURCE 700
#include <DFRobot_BMM150.h>
void setup() {}
void loop() {}
c:\Users\per\Documents\Arduino\libraries\DFRobot_BMM150\DFRobot_BMM150.cpp:229:26: error: 'M_PI' was not declared in this scope
  229 |   return compass * 180 / M_PI;
      |                          ^~~~

So I think it would be necessary to either modify the source code of the library, or else add the -D_XOPEN_SOURCE=700 flag to the platform's compilation command patterns.

1 Like

@ptillisch Yep tried a few things and came to the same conclusion that the library would need to be updated. But trying not to that as a lot of libraries that you would install would need to be modified for use with the Q - just a cut feel.

Changing the compile pattern would probably be the way to go. But a quick question is where in linux directory structure is arduino located? For that matter where are libraries installed - was poking around and couldn’t find them

EDIT:

Libs are located: ./home/arduino/.arduino15/internal/. Just do a CD to it

Not sure how to update the compile pattern in linux - located in ~/.arduino15/packages/arduino/hardware/zephyr/0.51.0

@ptillisch

I add -D_XOPEN_SOURCE=700 to platforms.txt and that seemed to resolve the issue.

Just for documentation I edited the platforms.txt .ccp flags line to read:
compiler.cpp.flags=-g -Os -std=gnu++17 -c -D_XOPEN_SOURCE=700 {compiler.warning_flags} {compiler.zephyr.macros} "@{compiler.zephyr.cxxflags_file}" {compiler.zephyr.common_ldflags} {compiler.zephyr.extra_ldflags} {compiler.zephyr.common_cxxflags} {compiler.zephyr.extra_cxxflags} -MMD -mcpu={build.mcu} {build.float-abi} {build.fpu}

Then opened applab and recompiled and now seeing

Acceleration in g's     X: 0.007        Y: -0.012       Z: 0.992        Rotation in deg/sec     X: 0.122        Y: 0.000        Z: 0.122
mag x = -3 uT
mag y = 6 uT
mag z = 0 uT
the angle between the pointing direction and north (counterclockwise) is:333.43

Acceleration in g's     X: 0.006        Y: -0.012       Z: 0.993        Rotation in deg/sec     X: 0.183        Y: 0.244        Z: 0.122
mag x = -4 uT
mag y = 6 uT
mag z = 0 uT
the angle between the pointing direction and north (counterclockwise) is:326.31

Acceleration in g's     X: 0.006        Y: -0.011       Z: 0.994        Rotation in deg/sec     X: 0.000        Y: -0.061       Z: 0.122
mag x = -3 uT
mag y = 6 uT
mag z = 0 uT
the angle between the pointing direction and north (counterclockwise) is:333.43

But not in serial monitor in applab but on Serial through a FTDI connector so still missing something I guess

Thanks for all your help.

1 Like

@ptillisch I added an issue in the arduinocore-zephyr repo but not sure if that is the right place or if there is another place to document it.

Arduino Q: Sketches using libraries with M_PI fail to compile in App Lab · Issue #236 · arduino/ArduinoCore-zephyr

2 Likes

Just by way of status this PR has been incorporated into the core so should be available when 0.52.0 is released

1 Like