Makefile suggestion

Camel

Well-known member
Hello,

I have recently purchased a Teensy 3.1 and am currently porting my Arduino Due program over to it. When I compile my program for the Due, it ends up around 100Kb in size. When I compiled for Teensy using a Makefile based on the one found in hardware/teensy/cores/teensy3, the binary is nearly 300Kb which is too big!

I added this to the compiler options on line 11

-ffunction-sections -fdata-sections

Now it is only 80Kb, which is fantastic. Perhaps the makefile shipped with teensyduino should have these options added? It might save someone some premature disappointment.

Cheers, Camel
 
I've been using those options myself in my non-IDE makefile. Not sure where I encountered them first.
 
What line are you adding these to? I have looked at the MakeFile and Your Stated Line 11 does not tally with compiler options.
Should it go in lines for "C and C++", "C++ only", or "C only"?

I have found the following from a Google search:-

You can use -ffunction-sections and -fdata-sections on static libraries, which will increase the size of the static library, as each function and global data variable will be put in a separate section.

And then use -Wl,--gc-sections on the program linking with this static library, which will remove unused sections.

Thus, the final binary will be smaller thant without those flags.

Be careful though, as -Wl,--gc-sections can break things
.


The Linker line mentioned above already exists in theMakeFile.
 
What line are you adding these to? I have looked at the MakeFile and Your Stated Line 11 does not tally with compiler options.
Should it go in lines for "C and C++", "C++ only", or "C only"?

Ahh, sorry for the confusion. Some of the modifications I did to the makefile messed up the line numbers. I should have said the OPTIONS line.

Anyway, this is the first 25 lines of my makefile.

Code:
# The name of your project (used to name the compiled .hex file)
TARGET = main

TEENSYPATH = /home/camel/Programming/Teensy/IDE

# configurable options
OPTIONS = -DF_CPU=96000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH

# options needed by many Arduino libraries to configure for Teensy 3.0
OPTIONS += -D__MK20DX256__ -DARDUIO=104 -ffunction-sections -fdata-sections


#************************************************************************
# Location of Teensyduino utilities, Toolchain, and Arduino Libraries.
# To use this makefile without Arduino, copy the resources from these
# locations and edit the pathnames.  The rest of Arduino is not needed.
#************************************************************************

# path location for Teensy Loader, teensy_post_compile and teensy_reboot
TOOLSPATH = $(TEENSYPATH)/hardware/tools   # on Linux
#TOOLSPATH = ../../../tools/avr/bin   # on Mac or Windows

# path location for the arm-none-eabi compiler
COMPILERPATH = $(TEENSYPATH)/hardware/tools/arm-none-eabi/bin
 
Be careful though, as -Wl,--gc-sections can break things.

Absolutely true

But in my experience if you wrote something from scratch, and added functionality little by little, and tested each addition, your final project will still work.

If you finished your project and then enabled garbage collection after, things can break, and it becomes a pain to figure out what broke, because the GC will throw away things for you and you gotta check each one of those things...
 
Back
Top