Using Eclipse with Teensy3 with libraries and good code size

Status
Not open for further replies.

nox771

Well-known member
Hello all,

I recently went through the process outlined HERE by Nassarane for setting up Eclipse with Teensy3. That was a very informative post and I managed to get it working as outlined, but ran into three problems. I managed to work through the problems, and I'll describe that below (this all assumes that all the steps in the original post have been completed). The problems were:
  1. the code placement of main(), setup(), and loop()
  2. getting libraries to compile and link (originally wasn't working)
  3. getting the compiled hex size to be correct (originally was huge)
First involves the code placement - I wanted to be able to compile existing sketches with little to no rework on the code. The original method demonstrated using the main.cpp file directly, but doing that makes it cumbersome to go back and forth between Eclipse and Arduino since Arduino hides main(). Instead of doing that change main.cpp to the following:

Code:
#include "WProgram.h"

extern "C" int main(void)
{
    // Arduino's main() function just calls setup() and loop()....
    setup();
    while (1) {
        loop();
        yield();
    }
}

After that take an existing sketch, copy it to the Eclipse project src folder (this assumes a folder layout similar to the one described in the original post), and change it's extension to .cpp. Then do a refresh on the project and edit the sketch .cpp file.

This is where you run into another odd Arduino thing - it hides the requirement of prototyping your functions in your main sketch file. That may work in Arduino but it won't work in Eclipse. To fix that you need to add function prototypes to the top of the sketch .cpp file. Having prototypes there doesn't break Arduino, so you can always copy the sketch file back and change its extension to .ino and it will compile fine in Arduino. As an example, assuming a sketch with setup(), loop(), and two other functions, the function prototypes can be added near the top of the file:

Code:
// Sketch code
#include <Arduino.h>

void function1(int param);   // <-------- add the prototypes here
int function2();

void setup() {
    int param1 = 0;
    function1(param1);
    ...
}

void loop() {
    int param2;
    param2 = function2();
    ...
}

void function1(int param) {
    // function1 code
}

int function2() {
    // function2 code
}


The second problem is getting libraries to work. For this I'll assume the following folder layout:

X:/Eclipse_sketch_path/TeensyCore3 <--- this is the base library created in the original post
X:/Eclipse_sketch_path/MySketch <--- Eclipse project, has src/ and Release/ subdirectories
X:/Eclipse_sketch_path/libraries/MyLib1 <--- unmodified libraries
X:/Eclipse_sketch_path/libraries/MyLib2 <--- unmodified libraries

Now fix some property settings:
  • In Eclipse, right-click on the project and select "Properties". Go to "C/C++ Build" then "Settings". Under "ARM Yagarto Windows GCC C Compiler" select "Preprocessor". Add a "__MK20DX128__" symbol, so the symbol list should have 4 symbols defined:
    1. __MK20DX128__
    2. F_CPU=96000000
    3. USB_SERIAL
    4. LAYOUT_US_ENGLISH
  • Repeat for "ARM Yagarto Windows GCC C++ Compiler" setting
  • In the left-hand panel, go to "C/C++ General" then "Paths and Symbols"
  • On the "Source Location" tab click "Link Folder..." check "Link to folder in the file system", browse to the ROOT library directory, in the above folders it would be "X:/Eclipse_sketch_path/libraries", then click "OK". Eclipse will recursively look through subdirectories for C/C++ files when compiling.
  • The above fixes C/C++ files, but not include files. Include files must be explicitly set. To do this click on "Includes" tab, then click "GNU C++", then for each library the project uses add an explicit path to where the headers are. So if the project used both MyLib1 and MyLib2 click "Add..." then "File System..." then browse to "X:/Eclipse_sketch_path/libraries/MyLib1" (repeat for MyLib2)
  • Repeat again for "GNU C" Language in left column if needed for a C library

Third problem was code size. This involves turning on the right compiler and linker options.
  • In Eclipse, right-click on the project and select "Properties". Go to "C/C++ Build" then "Settings". Under "ARM Yagarto Windows GCC C Compiler" select "Optimization". Check "Function sections" and "Data sections".
  • Repeat for "ARM Yagarto Windows GCC C++ Compiler"
  • Under "ARM Yagarto Windows GCC C++ Linker" then "General" check "Remove unused sections". This will greatly reduce the hex file size.
I would also recommend setting the same compile options on the TeensyCore3 static lib and rebuilding it.

After doing all that my sketch compiled clean with almost no change from the original .ino file (except for the prototypes), and the resulting code came out 5k smaller than Arduino (24k versus 29k, but it seemed to work fine).

EDIT: Just an FYI, I'm a big fan of PortableApps and there is a PA install for Eclipse. I am running Eclipse Portable C/C++ Edition version 4.2 from here: Eclipse Portable 4.2
It works fine for Teensy3 usage, and in combination with the Terminal plugin and Teensy Loader is a complete replacement for Arduino.

Also, the Terminal plugin appears to want a newline + carriage return for linefeeds. Since I usually only code in newlines '\n', I use a different terminal program I came across a long while back. This is a nice terminal program that has lots of features: HTerm 0.8.1beta
It is also good if you are transmitting binary over Serial, since it can display the hex codes directly.
 
Last edited:
Status
Not open for further replies.
Back
Top