Shan-Hung Hsu
Shan-Hung Hsu

Reputation: 137

How to control behavior of makefile by a macro definition in a header?

In my project, I define a macro called ACT_STATE in a header file config.h to make different blocks of code be compiled. Macro ACT_STATE could be ACT_DEVELOP(value 0) or ACT_DEPLOY(value 1), I want source files in a folder called User could be compiled at state ACT_DEPLOY and leave those source files alone at state ACT_DEVELOP. I know I can pass macro definition to makefile while calling make, but by this way I need to define ACT_STATE twice. Therefore, I think if it is possible to let make read macro definition from head files, so it could compile different files depending on macro definition in a header file?

Upvotes: 1

Views: 610

Answers (1)

Neil
Neil

Reputation: 1922

Preprocessor options,

gcc -D name=definition

However, ACT_DEVELOP 0 and ACT_DEPLOY 1 is not the simplest way to make a binary variable. There is no reason why one can't write #define ACT_STATE 7 or #undef ACT_STATE, leading to complicated range checks. A preferable way is #ifdef or #if defined(...).

gcc -D name

Unless you have a good reason to do this, consider the C standard assert.h. With C11, one can do static_assert that is really useful in some cases. The assert.h trigger is NDEBUG, which, when defined, makes assert arguments compile to (void)0.

Also see passing additional variables to make to edit one's Makefile.

Upvotes: 2

Related Questions