chipKIT® Development Platform

Inspired by Arduino™

Using preprocessor macros to decide what to include ignored

Created Sun, 28 Oct 2012 16:13:15 +0000 by majenko


majenko

Sun, 28 Oct 2012 16:13:15 +0000

You cannot optionally include headers using preprocessor macros in the MPIDE.

The following snippet:

#ifdef __PIC32MX__
#include <DSPI.h>
#else
#include <SPI.h>
#endif

always includes SPI.h and tries to link against SPI.cpp.

This same snippet works fine from within a library, but not the master sketch. I guess the IDE doesn't take the preprocessor macros into account when parsing the sketch for which libraries to include in the linkage.


WestfW

Mon, 29 Oct 2012 05:31:16 +0000

I think this came up on Arduino as well, but it turned out that the problem was that the file was NOT being included, but was still being linked...

#if 0
#include <SPI.h>
#endif
void setup() {
  SPI.transfer(22);     
}

will not compile. But if the SPI.transfer is commented out, it does compile, and still links in the SPI library.


majenko

Mon, 29 Oct 2012 10:46:22 +0000

Yeah, that's what I meant.

I have a library which uses either SPI or DSPI, and that side works.

If using DSPI then I create a DSPI0 object called SPI, so it all works pretty much seamlessly.

However, adding the same decision making code to the main sketch results in SPI being a duplicate definition, as it's already defined in the SPI library that gets linked in regardless.