Jump to content

Writing a multi-plateform library with IF DEFINED

Recommended Posts



I am trying to write a library that could compile with both Arduino and Energia (MSP430).

It should be easy because there are only a few changes involved in the .h header file (calls to different libraries) and the .cpp implementation file. I try to use the following preprocessor directives. 

#if defined (ARDUINO)

#elif defined (ENERGIA)



However this does not work as Energia still try to compile the arduino code.

Am I doing something wrong ?

Did someone experience the same ?





Link to post
Share on other sites

Well, I read a little bit more about GCC preprocessor directives and found that even if the code is not compiled, it has to be valid since it goes through transformations and tokenization.

That poses a problem because the AVR interrupt declaration are not valid for mspgcc...



I will have to dig in the AVR code a little bit more than I though.



Link to post
Share on other sites

Well, finally it is related to the inclusion of legacymsp430.h.

If I put it in the main code, it works. If I put it in a #if defined it does not work.

Please can someone have a look to my code at tell me if I am doing something wrong here :

#include "VirtualWire.h"
#include <util/crc16.h>

#if ARDUINO >= 100		// Recent Arduino IDE versions
#include <Arduino.h>
#elif ARDUINO < 100		// Older
#include <wiring.h>
#include <WProgram.h>
#elif defined(ENERGIA)	// Energia for LaunchPad (not tested on FraunchPad and StellarPad)
#include <Energia.h>
//#include <legacymsp430.h>
#define SIGNAL()
#else // error
#error Platform not supported


It is late now and I must go to sleep. Tomorrow kids get up at 5:30 at go to school at 7...




Link to post
Share on other sites

Energia uses both ARDUINO and ENERGIA variables to compile.


So you need to test ENERGIA first and ARDUINO after:



#if defined(ENERGIA)
#elif defined(ARDUINO) && (ARDUINO>100)



Please refer to this tutorial: Manage Code for Multiple Platforms with two approaches, MCU-based and IDE-based.


If you have a Mac computer, why not to try the template I've developed for Xcode? Have a look at embedXcode, embedded computing on Xcode.

Link to post
Share on other sites

Hi Rei Vilo,


Once again you're a master.

You tutorial is really helping.

At first I took your 5110 lib as example, but going the IDE specific way is not the best since this library (VirtualWire) use Timers that are specific to processors.

Going with the MCU specific macros did the trick.


To be honest, yesterday right after turning off the computer and going downstair heading to the bed I found a solution with more macro def...

I tested it this morning and it was compiling ok. I have not checked it was running ok thought.

Anyhow, it was more a workaround than a neat solution.




Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...