I scratched my head on this one for awhile but finally figured out
where the "extern USBVirtualSerial Serial" object was instantiated --
it wasn't! I was really stumped when I added a private default
constructor to USBVirtualSerial and things still worked. Turns out
that for the LUFAduino example, the calls to members of Serial were
all inlined (and essentially static), so the actual need for an
instance was optimized away -- this is my guess at least.
When I tried deriving USBVirtualSerial from the Print class for my
code:
[WiringVirtualSerial.h]
...
class USBVirtualSerial : public Print
...
[EOF]
this cause a undefined reference to Serial when linking. This is
consistent with the above theory since my code couldn't get away with
inlining all its calls to members of Serial. I then tried to add a
declaration for the USBVirtualSerial Serial object to a new .cpp file:
[new file USBVirtualSerial.cpp]
#include "WiringVirtualSerial.h"
// Preinstantiate
Objects //////////////////////////////////////////////////////
USBVirtualSerial Serial;
[EOF]
I noticed that the above file was referenced from the makefile
anwyay. This didn't work as expected either. There's a file called
USBVirtualSerial.c, and my guess is that make decides it can generate
USBVirtualSerial.o from the USBVirtualSerial.c file, and so doesn't
bother trying to compile the USBVirtualSerial.cpp file. I suppose
the .o would collide even if it did. My final attempt was the
following:
[new file WiringVirtualSerial.cpp]
#include "WiringVirtualSerial.h"
// Preinstantiate
Objects //////////////////////////////////////////////////////
USBVirtualSerial Serial;
[EOF]
I modified the makefile accordingly and the above seems to compile
just fine (with USBVirtualSerial now derived from Print).
-ojas