Unnecessary stuff from library linked in

arpruss
Sun Feb 04, 2018 3:05 pm
If I include a single header file from a library, as far as I can tell all the code and data from the library gets linked in, even stuff that’s completely irrelevant.

For instance:
#include "usb_generic.h"
void setup() {}
void loop() {}


arpruss
Sun Feb 04, 2018 4:19 pm
OK, I figured out what is happening. When a library defines an instance of a class, the constructor code for the instance is linked in, even if that instance is never referenced by anything else, and this constructor code can pull a bunch of other stuff in. My USBHID library defines a lot of convenience instances (e.g., Keyboard, CompositeSerial, etc.). I am not sure what the best way around this is.

victor_pv
Sun Feb 04, 2018 4:28 pm
Perhaps leave the instances out of the class files, so the user defines in the sketch if they are going to be used?

arpruss
Sun Feb 04, 2018 4:33 pm
[victor_pv – Sun Feb 04, 2018 4:28 pm] –
Perhaps leave the instances out of the class files, so the user defines in the sketch if they are going to be used?

That would work, but I want to make this as friendly as possible to the user, and to mimic how the core works. The core defines lots of such convenience instances like Serial, Serial1, etc.


mrburnette
Sun Feb 04, 2018 4:36 pm
The answer you will not like, use only C for everything.

[arpruss – Sun Feb 04, 2018 4:33 pm] –
That would work, but I want to make this as friendly as possible to the user, and to mimic how the core works. The core defines lots of such convenience instances like Serial, Serial1, etc.

I suspect ease-of-use for end-users of Arduino was why C++ was elected for Arduino, but they still argue about it over on Arduino.cc

If you go searching around the Internet, avoid anything that was pre-Arduino 1.5.8 because the 1.5.8 version fundamentally changed much of the back-end methodology. https://www.arduino.cc/en/Main/OldSoftw … ases#1.5.x October 2014

But, it is not a STM32 issue, even the Teensy dudes/dudettes have issues.

Ray


stevestrong
Sun Feb 04, 2018 5:35 pm
You may try to edit the platform.txt by adding the “-specs” compiler directives:
compiler.c.elf.extra_flags="-L{build.variant.path}/ld" -specs=nosys.specs -specs=nano.specs

arpruss
Sun Feb 04, 2018 5:46 pm
[stevestrong – Sun Feb 04, 2018 5:35 pm] –
You may try to edit the platform.txt by adding the “-specs” compiler directives:

Yeah, that does reduce size.

I still would like to solve the library class instance linking issue. It would be nice if there were some way to define an instance of a class that would only get linked in if the class is referenced. But C++ standards presumably require that if the instance is created, then its constructor is called, and hence at least the constructor needs to get linked in. And then any virtual functions also need to be linked in along with constructor.

Is there a way to split an Arduino library, so depending on which header files you include, only a part of the library gets included?


mrburnette
Mon Feb 05, 2018 4:19 am
[arpruss – Sun Feb 04, 2018 5:46 pm] –

Is there a way to split an Arduino library, so depending on which header files you include, only a part of the library gets included?

You can put any library into you local sketch folder …. .h and .cpp files become tabs. You reference them in #include “./file.xxx” rather than using the braces <…>

Depending on how you do things, the Arduino auto-prototyping may fail, you may need to do a forward declaration.

Ray


arpruss
Mon Feb 05, 2018 4:38 am
It seems that the issue was that the constructor for a class that contains virtual function forces the virtual functions to be linked in even if they are never called by the code. Since the classes that I had convenience instances for had virtual functions, that meant that a lot of unnecessary code was getting linked. I ended up rewriting without virtual functions, and a lot of RAM and flash has been saved.

mrburnette
Mon Feb 05, 2018 1:37 pm
[stevestrong – Sun Feb 04, 2018 5:35 pm] –

I use it in my repo, it reduces considerably the code size with no side issues so far.

NOOB warning:
The GCC compiler and associated software tools are remarkably complex… which is why ArduinoIDE keeps this mess hidden from the average user. Please understand, if you deviate from the defaults, you are (in my opinion) on your own for support. For those inquisitive, a list of options is available here: https://gcc.gnu.org/onlinedocs/gcc/Option-Index.html

Ray


michael_l
Mon Feb 05, 2018 4:51 pm
[stevestrong – Sun Feb 04, 2018 5:35 pm] –
You may try to edit the platform.txt by adding the “-specs” compiler directives:
compiler.c.elf.extra_flags="-L{build.variant.path}/ld" -specs=nosys.specs -specs=nano.specs

Rick Kimball
Mon Feb 05, 2018 9:47 pm
[michael_l – Mon Feb 05, 2018 4:51 pm] –
At least the %f format of printf will not work anymore. But there is a option to include %f, but can’t remember that from the top of my head.

viewtopic.php?f=3&t=1241&start=20#p38392


Leave a Reply

Your email address will not be published. Required fields are marked *