I am trying to write a library, portable and not limited to Arduino.
In order to insert appropriate pre-processor commands,
I would like to know a macro definition I can check weather I am running in the Arduino environment or not.
I don't see anything defined that would permit this.
Why do you think you need it, though? Assuming that your library code has a .c or .cpp extension instead of .pde, the "Arduino environment" doesn't DO anything special; it's just compiled as a normal C or C++ binary and linked into the users sketch. And for that matter, all the arduino environment does for a .pde "sketch" is a bit of prototype generation and code inclusion; it still ends up being compiled in a very generic fashion.
For instance, here's the line used to compile a menu library I've been working on (the source is menu_lib.cpp, just copied into the sketch directory that uses it.)
There would be nothing about the way it was compiled that would prevent me from using the resulting .o file in a non-arduino context. (well, in this case, it uses Serial.print, so I might have link-time issues. But not compile issues!)
(OTOH, having a "-D__ARDUINOVERSION=12" or similar on the compile line seems like a worthwhile idea. And on the third hand I've seen such "reasonable" ideas expand into a bewildering array of defines, among which it is nearly impossible to pick a set that actually works...)
When running on an Arduino, the function is called by the interrupt.
However, to confirm that process_func is working as expected, I am debugging on Linux by manually calling process_func byte by byte.
It would be convenient if I could #ifndef out the entire ISR block when building on other systems.
Do it the other way around - use a preprocessor directive (#define equivalent from the compiler command line is one technique) to detect when you're not on the Arduino, e.g.
#ifdef LINUX_TEST
ISR()
{
//linux code here
}
#else
ISR()
// Arduino code goes here
}
It would be convenient if I could #ifndef out the entire ISR block when building on other systems.
I rather like kg4wsv's suggestion; making things very explicit and manual.
However, if you only need to distinguish between Arduino and Linux, that is comparatively easy, because the respective compilers define assorted preprocessor signals. It's only "hard" to distinguish Arduino environment from non-Arduino AVR compiles. The avrfreaks posting describes how to figure these out, but it looks like the end result will be like:
#if defined(__AVR__)
/* ISR code for AVR */
#endif
#if defined(__unix__)
/* non-ISR code for linux here */
#endif
(huh. My Mac's compiler doesn't seem to pre-define unix, so at first glance it looks tough to find a single symbol that would be set for linux and both macos cpus... How ... annoying. Of course, it would be easy to add the explicit "-D__unix__")
I'd like to propose that the IDE be modified to add two additional -D options to the avr-gcc compiler command line. I believe the place to do it is in compiler.java where the baseCommandCompiler is defined. I've added the proposed code in the excerpt below where it defines ARDUINO_CORE and ARDUINO_VARIANT.
List baseCommandCompiler = new ArrayList(Arrays.asList(new String[] {
avrBasePath + "avr-gcc",
"-c", // compile, don't link
"-g", // include debugging info (so errors include line numbers)
"-Os", // optimize for size
"-w", // surpress all warnings
"-mmcu=" + Preferences.get("boards." + Preferences.get("board") + ".build.mcu"),
"-DF_CPU=" + Preferences.get("boards." + Preferences.get("board") + ".build.f_cpu"),
"-DARDUINO_CORE=" + Preferences.get("boards." + Preferences.get("board") + ".build.core"),
"-DARDUINO_VARIANT=" + Preferences.get("boards." + Preferences.get("board") + ".build.variant"),
}));
The presence of the ARDUINO_CORE definition could be used to detect when code is being compiled for any Arduino core and its specific value could be used to enable some core-specific code. The ARDUINO_VARIANT definition could be used to distinguish between slightly different but otherwise quite similar cores. Note that the value for ARDUINO_CORE is taken from an existing entry in the boards.txt file while that for ARDUINO_VARIANT is taken from a currently non-existent entry.
I'd like to propose that the IDE be modified to add two additional -D options to the avr-gcc compiler command line. I believe the place to do it is in compiler.java where the baseCommandCompiler is defined. I've added the proposed code in the excerpt below where it defines ARDUINO_CORE and ARDUINO_VARIANT.
which worked ok.
The later is there to guarantee it is not using compilers other than avr-gcc.
For now, it's done well to achieve my goal of shutting out codes unrelated to AVR while letting me test routines on PC. I mentioned Linux as my platform but it is nice to have the library working as generic C code while tests.
Anyway, if the conclusion for now is that, as a matter of fact there are no predefines unique to Arduino IDE, I would also like to add a vote to the feature being added.
Are keywords such as "feature request" in posts crawled by maintainers, or else where would be the best place to post requests?
I am still relatively new to this community...
Note that the current value of .build.core is a STRING like "arduino", which makes it not very suitable for using as the value for a symbol defined in the gcc command line... I don't think you can do string compares in the preprocessor...
I don't think you can do string compares in the preprocessor.
True. I suppose that I was thinking about how I use such symbols in a makefile. The symbol could be something like ARDUINO_CORE_ followed by the core name. The preprocessor conditional would then check for existence. The same stragegy could be used for the variant as well.