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


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>
#include <SPI.h>

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.


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>
void setup() {

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


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.