Teensy LC builds much larger than on Arduino

Hello,

I’ve discovered that builds for Teensy LC are much bigger than when I build the same code in the Arduino IDE.

I have tried looking up the small code optimization flags in the Arduino board def and bringing those over to build_flags in platformio.ini, with no luck. As an example, my current test code that brings in libraries my project needs builds in 48K in the Arduino IDE, but overflows flash by 1.5K when compiled in PlatformIO.

Anybody else encountered this and have suggestions for getting my code size down to what Arduino IDE puts out?

1 Like

We build with math and stdc++ libraries by default. Try in platformio.ini

[env:myenv]
board = ...
build_unflags = -lstdc++ -larm_cortexM4l_math

This has no effect on the build size.

1 Like

@e3b0c442 were you able to solve the problem?

How does it differ by size in your case?

Almost the same 1.5 kB.
I tried this but didn’t help.

I started this topic with more details.

This is ARM, not AVR.

Is there something similar for AVR?

Teensy LC is ARM based board.

Вань, я про флаги спрашиваю, есть какие-нибудь похожие для AVR? У меня впечатление, что ардуинка немного по-другому это все собирает. Либо в 5-й версии тулчейна компилятор умнее и делает сборку оптимальнее.

Let’s back to

1 Like

Hi there,
Sorry for the delayed followup, I had to put this project down for a bit.

The answer is – no, I have not been able to resolve this issue.

Here’s an easily-duplicable example to hopefully demonstrate.

The following code snippet:
#include <ArduinoJson.h> // version 5.13.4

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial)
    ;
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  StaticJsonBuffer<128> jbuf;
  char tester[64];
  strcpy(tester, "{\"key\": \"value\"}");

  JsonObject &obj = jbuf.parseObject((const char *)tester);
  obj.printTo(Serial);
  Serial.printf("'%s'", tester);
}

void loop()
{
  // put your main code here, to run repeatedly:
}

When built with Arduino/Teensyduino, the sketch uses 17008 (of 63488) bytes of program storage and 2316 (of 8192) bytes of dynamic memory.

The exact same code compiled in PlatformIO with the default settings comes out to 41636 (of 63488) bytes of program storage and 3516 (of 8192) bytes of dynamic memory. This is utilizing the “smallest code” board setting in Arduino.

Here are the board build settings for teensyLC in Arduino/Teensyduino:

teensyLC.build.board=TEENSYLC
teensyLC.build.core=teensy3
teensyLC.build.mcu=mkl26z64
teensyLC.build.warn_data_percentage=88
teensyLC.build.toolchain=arm/bin/
teensyLC.build.command.gcc=arm-none-eabi-gcc
teensyLC.build.command.g++=arm-none-eabi-g++
teensyLC.build.command.ar=arm-none-eabi-gcc-ar
teensyLC.build.command.objcopy=arm-none-eabi-objcopy
teensyLC.build.command.objdump=arm-none-eabi-objdump
teensyLC.build.command.size=arm-none-eabi-size
teensyLC.build.flags.common=-g -Wall -ffunction-sections -fdata-sections -nostdlib
teensyLC.build.flags.dep=-MMD
teensyLC.build.flags.cpu=-mthumb -mcpu=cortex-m0plus -fsingle-precision-constant
teensyLC.build.flags.defs=-D__MKL26Z64__ -DTEENSYDUINO=145
teensyLC.build.flags.cpp=-fno-exceptions -felide-constructors -std=gnu++14 -Wno-error=narrowing -fno-rtti
teensyLC.build.flags.c=
teensyLC.build.flags.S=-x assembler-with-cpp
teensyLC.build.flags.ld=-Wl,--gc-sections,--relax,--defsym=__rtc_localtime={extra.time.local} "-T{build.core.path}/mkl26z64.ld" -lstdc++
teensyLC.build.flags.libs=-larm_cortexM0l_math -lm
teensyLC.menu.opt.osstd=Smallest Code
teensyLC.menu.opt.osstd.build.flags.optimize=-Os --specs=nano.specs
teensyLC.menu.opt.osstd.build.flags.ldspecs=

I’ve tried applying different combinations of these settings using build_flags and unbuild_flags in platformio.ini to no apparent effect.

Really hoping that we can figure this out because I really hate the Arduino UI, but this also makes PlatformIO unusable.

Please report issue to Issues · platformio/platform-teensy · GitHub

I just did that while also discovering a workaround. From my followup on the issue:


I did a little research and discovered two things:

The GNU ARM docs specify --specs=nano.specs as a link time option
adding --specs=nano.specs to the build_flags setting did not apply it to the linking step.
I opened up the board file and explicitly added --specs=nano.specs to the LINKFLAGS and… (using the ADC example above)

Before:
DATA: [==== ] 41.8% (used 3424 bytes from 8192 bytes)
PROGRAM: [====== ] 60.8% (used 38608 bytes from 63488 bytes)

After:
DATA: [= ] 12.3% (used 1008 bytes from 8192 bytes)
PROGRAM: [== ] 21.5% (used 13636 bytes from 63488 bytes)

Is there a way without editing the board framework definition to explicitly change the linker flags?


I did find a way in the Advanced Scripting documentation and am using that now.

Care to share the final solution details?

I have looked in ~/.platformio/packages/framework-arduinoteensy/cores/teensy31.json and
~/.platformio/packages/framework-arduinoteensy/cores/teensy3
without seeing any obvious LINKFLAGS.

Trying the python extra_script.py method in the platformio.ini extra_scripts section.

Import("env")
env.Append(
LINKFLAGS=[ "--specs=nano.specs" ] )