Unable to include std::functional

Hi.

I tried to include stl::functional to a project and it do not compile with Arduino DUE programing port.

I tried with the arduino IDE and it compile. On platformIO I got a really long list of error…

I was tried with an empty sketch with arduio.h include, setup and loop definition…

Here the error given:

Thanks

platformio core 5.0.1

n file included from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\char_traits.h:39:0,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\string:40,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\stdexcept:39,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\array:39,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\tuple:39,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\functional:54,
             from src\main.cpp:3:
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\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)
                                                    ^
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\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)
                                                    ^
In file included from C:\Users\o_for\.platformio\packages\framework-arduino-sam\system\libsam/chip.h:66:0,
             from C:\Users\o_for\.platformio\packages\framework-arduino-sam\cores\arduino/Arduino.h:42,
             from src\main.cpp:1:
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:195:5: error: expected unqualified-id before 'const'
 min(const _Tp& __a, const _Tp& __b)
 ^
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:195:5: error: expected ')' before 'const'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:195:5: error: expected ')' before 'const'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:195:5: error: expected ')' before 'const'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:195:5: error: expected initializer before 'const'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:219:5: error: expected unqualified-id before 'const'
 max(const _Tp& __a, const _Tp& __b)
 ^
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:219:5: error: expected ')' before 'const'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:219:5: error: expected ')' before 'const'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:219:5: error: expected ')' before 'const'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:219:5: error: expected initializer before 'const'
In file included from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\char_traits.h:39:0,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\string:40,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\stdexcept:39,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\array:39,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\tuple:39,
             from c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\functional:54,
             from src\main.cpp:3:
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:246:7: error: expected primary-expression before 'if'
   if (__comp(__b, __a))
   ^~
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:246:7: error: expected '}' before 'if'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:246:7: error: expected ';' before 'if'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:248:7: error: expected unqualified-id before 'return'
   return __a;
   ^~~~~~
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:268:7: error: expected primary-expression before 'if'
   if (__comp(__a, __b))
   ^~
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:268:7: error: expected '}' before 'if'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:268:7: error: expected ';' before 'if'
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:270:7: error: expected unqualified-id before 'return'
   return __a;
   ^~~~~~
c:\users\o_for\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\c++\7.2.1\bits\stl_algobase.h:271:5: error: expected declaration before '}' token
 }
 ^
*** [.pio\build\due\src\main.cpp.o] Error 1

Please show the contents of your platformio.ini file, the full build log and at least the list of includes of main.cpp.

One of the includes (a library or the specific Arduino framework implementation) defines macros min and max and thereby breaks the use of min and max in std::functional.

main:
#include <Arduino.h>

#include <functional>

void setup() {

}

void loop() {

}

pio.ini:

[env:due]
platform = atmelsam
board = due
framework = arduino
lib_extra_dirs = ~/OneDrive/Documents/Arduino/libraries

Min(a,b) and Max(a,b) arde define in /system/libsam/include/USB_device.h.
But functionnal works correctly in Arduino IDE… they are define the same on both sam version.

Find a hack!!!:

If I include functional before arduino.h, then it compile…

The conflicting use of min and max is part of the Arduino Core for SAM3X CPUs:

I haven’t investigated why it works in the Arduino IDE. Did you also select the Arduino Due board?

The easiest workaround is to undefine the macros:

#include <Arduino.h>
#undef min
#undef max
#include <functional>

void setup() {
...
}

void loop() {
...
}
1 Like