What is the best way to hardcode the compiler's -D flag into the source files?
I want to put the #define in one of the headers and then import bunch of stuff that should react to that define. Does it work like that? Which header should I put it? Or directly to main.c? Or to all of them?
TMI-details: The environment here is digispark hw-018 (attiny85 with built-in usb programmer) and the #define should replace -DUSE_SOFTWARE_SPI so I can use AVR-CAN library in SPI mode to read stuff from MCP2515.
EDIT: After some annoying banging head to the wall the top of main.c looks now like this and it still does not work.
#define USE_SOFTWARE_SPI
#include "src/can.h"
#define USE_SOFTWARE_SPI
#include <avr/io.h>
#define USE_SOFTWARE_SPI
#include <avr/pgmspace.h>
#define USE_SOFTWARE_SPI
#include <DigiUSB.h>
I am right in saying that there is something that overrides my define somewhere? It works if I hardcode the #define USE_SOFTWARE_SPI just before the #ifndef (or #ifdef)
There are a couple of approaches here.
If it's just a single
main.c
source file, then putting this at the top of the file is fine:This doesn't scale well if this needs to be in multiple source files because it's too easy to miss it in one of the several places.
I suppose that even if there are multiple source files, but only one actually references the headers files that need to be influenced, it's less risky, but this is how mistakes happen down the road.
My preference is to put this kind of thing in the project definition as a whole, either in the
makefile
or in whatever the IDE uses to drive the project, because that insures everybody sees it.So, if you do have multiple source files and you can't or won't put this in the project file, then the way to handle it is with your own custom header that exists only for this purpose:
and then include that in your
.c
programs. Be sure to give the file a name that strongly suggests "local adjustments" rather than something that could be confused with one provided by a library.EDIT I just re-read that this is Arduino, and as far as I know the IDE doesn't have project-wide settings.