I am having trouble writing this macro. I am using ceedling.
I have a file, "globals.h", which has the following code:
#ifndef globals
#ifndef UNITY
#define STATIC static
#else
#define STATIC
#endif
#define globals 1
#endif
So, what should happen here is that any method which uses STATIC will be static if the code is not being tested with unity, else it will be not static.
My test file includes "unity.h", and then includes "globals.h", and then includes "protocol.h".
The file being tested, "protocol.c", includes "globals.h" and then "protocol.h".
The test file, "test_protocol.c", includes "unity.h", then "globals.h", then "protocol.h".
From my understanding, UNITY should be defined first, then it will go to globals.h, and define STATIC as nothing, since UNITY has been defined already in UNITY.h
However, the behavior I am seeing is that no matter what I do, I cannot get globals.h to enter that #else directive, and so it appears to be impossible to have STATIC be defined as anything other than static.
However, I know that what i am trying to accomplish is possible, because many people recommend this strategy and have implemented it. So, what am I doing that is causing globals.h to think that UNITY is not defined, when it is?
test_protocol.cdoesn't need to includeglobals.hfile. The point of view fromtest_protocol.cis that everything inprotocol.chas external linkage, but itself doesn't need that.Because
protocol.candtest_protocol.care in different translation unit,UNITYdefined inUNITY.his not seen inprotocol.cbecause it doesn't includeUNITY.h.Of course, the code under test must be kept intact. So we must somehow tell
protocol.caboutUNITYflag during the test but not in the normal compilation. That is where the section:defines:in the yaml file shines.The
project.ymlfile has already defined aTESTmacro:You need to define
UNITYin here too or just use theTESTmacro in yourglobals.h. TheTESTmacros are there for this purpose anyway.This ensures the flag
TESTis passed to every translation unit if it is compiled byceedlingfor testing, but not there if you use the normal compiler as usual.