I'm designing a library that is supposed to support Arduino as well as a couple of other platforms. I'd like to let users use Arduino IDE for a quick start. Unfortunately as far as I could find from Arduino documentation it looks like when you give Arduino IDE a library, it just tries to compile everything in the 'src' directory, there is no way to tell it not to compile certain files, or, vice versa compile something outside 'src'.
Is there any way of having a more fine-grained control on how the library is built? Any success stories for building cross-platform libraries with Arduino IDE?
OYTIS:
I'm designing a library that is supposed to support Arduino as well as a couple of other platforms. I'd like to let users use Arduino IDE for a quick start.
Thank you!
OYTIS:
when you give Arduino IDE a library, it just tries to compile everything in the 'src' directory
Correct
OYTIS:
there is no way to tell it not to compile certain files
You can use preprocessor directives. The ARDUINO macro will be defined when compiling using the Arduino IDE or arduino-cli: #if !defined(ARDUINO)
// non-Arduino code here #endif
pert:
You can use preprocessor directives. The ARDUINO macro will be defined when compiling using the Arduino IDE or arduino-cli:
#if !defined(ARDUINO)
// non-Arduino code here #endif
I've seen that done in some libraries. IMHO it makes the code very difficult to decipher because it is not easy to see which parts belong to the Arduino and which to Linux (for example).
Just out of curiosity is it possible to do it like this
Robin2:
I've seen that done in some libraries. IMHO it makes the code very difficult to decipher because it is not easy to see which parts belong to the Arduino and which to Linux (for example).
It might help to put the platform-specific code in a separate source file for each platform and wrap each file in an #ifdef for the platform identifying macro. You can see an example of this sort of thing in the Arduino Servo library:
At a glance, you might think that there is some IDE magic that makes it so that only the source files in the subfolder that matches the architecture of the board you're compiling for is compiled, but actually all the subfolders are compiled and it's just the #ifdefs in each .cpp file that make it work.
I think this approach is best for when the platform-specific code is almost completely different from one platform to the other. In cases where most of the code is the same with only small differences, splitting it into multiple files will lead to a lot of code duplication.
Robin2:
Just out of curiosity is it possible to do it like this
That will only work if the code you want to control is all in the .h files. It will not have any effect on which source files (.cpp/.c) in the library are compiled. We're used to #include directives having an effect on which files are compiled because the Arduino IDE does only compile the libraries for which there is an #include directive in the code. However, #include directives don't affect which individual files within the library are compiled.