tsandmann / freertos-teensy

FreeRTOS port with C++ std::thread support for ARM boards Teensy 3.5, 3.6, 4.0 and 4.1 (cortex-m4f and cortex-m7f)
95 stars 16 forks source link

Can't use std::mutex in PlatformIO library. #18

Closed tomstix closed 1 year ago

tomstix commented 1 year ago

Problem:

When creating source files inside the PlatformIO "lib" folder, std::mutex and also std::condition_variable can not be used. Building a project with the follówing code inside lib/testlib/testlib.cpp

#include "testlib.hpp"

#include <mutex>

void testfunc()
{
    static std::mutex m;
}

fails with lib\testlib\testlib.cpp:8:15: error: 'mutex' in namespace 'std' does not name a type

I'm guessing there is some kind of include path missing? The platformio.ini file looks like this:

[env:teensy40]
platform = https://github.com/tsandmann/platform-teensy.git
board = teensy40
framework = arduino
lib_deps =
    https://github.com/tsandmann/freertos-teensy.git
build_flags = -Wformat=1 -DTEENSY_OPT_FASTER_LTO
build_unflags = --specs=nano.specs
upload_flags = -v
upload_protocol = teensy-cli

PlatformIO's verbose Output shows the following:

Processing teensy40 (platform: https://github.com/tsandmann/platform-teensy.git; board: teensy40; framework: arduino; lib_deps: https://github.com/tsandmann/freertos-teensy.git; build_flags: -Wformat=1 -DTEENSY_OPT_FASTER_LTO; build_unflags: --specs=nano.specs; upload_flags: -v; upload_protocol: teensy-cli)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
PlatformIO running on windows_amd64
CONFIGURATION: https://docs.platformio.org/page/boards/teensy-ts/teensy40.html
PLATFORM: Teensy (4.17.0+sha.b92da4f) (git+https://github.com/tsandmann/platform-teensy.git) > Teensy 4.0
HARDWARE: IMXRT1062 600MHz, 512KB RAM, 1.94MB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES: 
 - framework-arduinoteensy @ 1.157.1+sha.704a666 (git+https://github.com/tsandmann/teensy-cores.git#no_warnings) 
 - framework-arduinoteensy @ 1.157.220801 (1.57) 
 - tool-teensy @ 1.157.0 (1.57)
 - toolchain-arm-cortexm-win64 @ 12.2.0-3+sha.b1c22e4 (git+https://github.com/tsandmann/arm-cortexm-toolchain-win64.git)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 94 compatible libraries
Scanning dependencies...
Dependency Graph
|-- freertos-teensy @ 10.5.1-5+sha.b419f64 (License: MIT, URI: git+https://github.com/tsandmann/freertos-teensy.git, Path: C:\Users\tom\Documents\PlatformIO\Projects\teensy4_libtests\.pio\libdeps\teensy40\freertos-teensy)
|   |-- SdFat @ 2.1.2 (License: MIT, Path: C:\Users\tom\.platformio\packages\framework-arduinoteensy\libraries\SdFat)
|   |   |-- SPI @ 1.0 (License: Unknown, Path: C:\Users\tom\.platformio\packages\framework-arduinoteensy\libraries\SPI)
|   |-- SPI @ 1.0 (License: Unknown, Path: C:\Users\tom\.platformio\packages\framework-arduinoteensy\libraries\SPI)
|   |-- Wire @ 1.0 (License: Unknown, Path: C:\Users\tom\.platformio\packages\framework-arduinoteensy\libraries\Wire)
|-- testlib (License: Unknown, Path: C:\Users\tom\Documents\PlatformIO\Projects\teensy4_libtests\lib\testlib)
Building in release mode
arm-cortexm7f-eabi-g++ -o .pio\build\teensy40\libb4a\testlib\testlib.cpp.o -c -fno-exceptions -fno-non-call-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -felide-constructors -fno-rtti -std=gnu++20 -Wno-error=narrowing -fpermissive -Wformat=1 -Wall -Wextra -ffunction-sections -fdata-sections -mthumb -mcpu=cortex-m7 -fsingle-precision-constant -mfloat-abi=hard -mfpu=fpv5-d16 -O2 -flto -fipa-pta -DPLATFORMIO=60106 -D__IMXRT1062__ -DARDUINO_TEENSY40 -DTEENSY_OPT_FASTER_LTO -DUSB_SERIAL -DARDUINO=10819 -DTEENSYDUINO=157 -DCORE_TEENSY -DF_CPU=600000000 -DLAYOUT_US_ENGLISH -Ilib\testlib -IC:\Users\tom\.platformio\packages\framework-arduinoteensy@src-ec76f8a486a2c31ea130ab62366da5c5\teensy4 lib\testlib\testlib.cpp
lib\testlib\testlib.cpp: In function 'void testfunc()':
lib\testlib\testlib.cpp:7:17: error: 'mutex' in namespace 'std' does not name a type
    7 |     static std::mutex m;
      |                 ^~~~~
lib\testlib\testlib.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
    1 | #include "testlib.hpp"
  +++ |+#include <mutex>
    2 |
*** [.pio\build\teensy40\libb4a\testlib\testlib.cpp.o] Error 1

I need std::thread, std::mutex, etc. support for including an external library so any help is appreciated!

tomstix commented 1 year ago

Okay, I have to #include <arduino_freertos.h> before including <mutex>. Makes sense but is there a way to automatically include this without having to modify the library?

tsandmann commented 1 year ago

Yes, you have to include arduino_freertos.h first. No, there is no way to include it automatically.