macro “min” passed 3 arguments, but takes just 2
Affected hardware
This error is not due to ArduinoJson but to a bug in the Arduino core. Many cores fixed this bug, but it remains in the one for SAMD21, so the following boards are affected:
- Arduino Zero
- Arduino MKR1000
- Arduino MKR ZERO
- Arduino MKR WiFi 1010
- Arduino NANO 33 IoT
- Arduino MKR FOX 1200
- Arduino MKR WAN 1300
- Arduino MKR WAN 1310
- Arduino MKR GSM 1400
- Arduino MKR NB 1500
- Arduino MKR Vidor 4000
- Adafruit Circuit Playground Express
- Arduino M0 Pro
- Arduino M0
- Arduino Tian
Other cores are probably affected as well; please let me know.
Reproducing the error
This error can come up when you include ArduinoJson.h
, but can also occur on an empty sketch.
For example, take the following program:
#include <string>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
If you try to compile this sketch for an mkr1000, you’ll get the following output:
In file included from ...\arm-none-eabi\include\c++\7.2.1\bits\char_traits.h:39:0,
from ...\arm-none-eabi\include\c++\7.2.1\string:40,
from mysketch.ino:1:
...\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:243:56: error: macro "min" passed 3 arguments, but takes just 2
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
...\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:265:56: error: macro "max" passed 3 arguments, but takes just 2
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
exit status 1
Error compiling for board Arduino MKR1000.
This error occurs as soon as you include a header from the C++ standard library (also known as “STL”), such as <string>
.
Why does this error occur?
This error comes from a conflict between the min()
macro defined in Arduino.h
and the std::min()
function declared in stl_algobase.h
.
Here is the definition of the macro:
#define min(a,b) ((a)<(b)?(a):(b))
And here is the declaration of the overload of std::min()
that triggers the error:
template< class T, class Compare >
const T& min(const T& a, const T& b, Compare comp);
When the preprocessor parses this declaration, it tries to expand the min()
macro but fails because the macro expects 2 arguments not 3.
A Pull Request should fix this bug someday.
How to fix this error?
ArduinoJson triggers this error because it includes <string>
.
To prevent that, you simply need to disable the support for std::string
in the library by defining ARDUINOJSON_ENABLE_STD_STRING
to 0
.
But it won’t be enough because ArduinoJson.h
also includes <istream>
and <ostream>
to support standard C++ streams.
To disable this feature, you must define ARDUINOJSON_ENABLE_STD_STREAM
to 0
as well.
Finally, your code should look like that:
#define ARDUINOJSON_ENABLE_STD_STRING 0
#define ARDUINOJSON_ENABLE_STD_STREAM 0
#include <ArduinoJson.h>
Depending on your situation, they might be other ways to fix this error.
For example, you could ensure that <string>
is included before Arduino.h
, possibly using the -include
command line option of the compiler.