0

My question is similar to this question, if you do not have too much time to read my question, you can read that shorter one.

My project used a config file

targetName=module01

The config file is parsed by a .exe and compile commands are generated, like this

cl.exe /D "MODULE_NAME=module01" /D BUILDING_module01 

So the targetName is related to two pre-processor macros.

My source file looks like

#ifdef BUILDING_module01 
// do something
#endif

So bascially the //do something part will always get executed if targetName in soucre file match with that in config file. The problem is that I am frequently changing the targetName in config file, and I do not want to change the source file that frequently.

I would like something like, by using pre-processor macros

#ifdef BUILDING_ ## MODULE_NAME
// do something
#endif

I used tricks in here. But the macro substitution will always make BUILDING_ ## MODULE_NAME equal to 1, so the compiler complains

warning C4067: unexpected tokens following preprocessor directive - expected a newline

Is there any way I can achieve this?

  • How about `#if MODULE_NAME == module1`? – Jarod42 Dec 07 '21 at 13:27
  • Sorry, I did not make it clear. In your way, I have to chang module01 to module02 in both config file and source file. I do not want to edit the source file. I want to just edit name in config file, and that new name is automatically transfered to source file, by the use of pre-processor macros. Because the compile command would introduce a macro that is related to new name. – Mark Hayes Dec 07 '21 at 13:35
  • `#ifdef MODULE_NAME` seems simpler in that case, no? – Jarod42 Dec 07 '21 at 13:37
  • @Jarod42 Yes, it will defenitely work and is simpler, once I thought use that. Now I want to know is there any way to concatenate them so it would be the same as orginal `#ifdef BUILDING_module01` – Mark Hayes Dec 07 '21 at 13:45
  • Is it not more common to have `#ifdef DO_SOMETHING` as the guard, and then add or not add that to the config file? That would make the file name irrelevant. – BoP Dec 07 '21 at 14:07
  • @BoP I can not get your points. Would you mind explain it once again? The // do something part inside #ifdef block is some #include, not that #ifdef DO_SOMETHING – Mark Hayes Dec 08 '21 at 01:03
  • @Yann - I'm just suggesting that the config file should decide how the module is built, not the source file. So instead of having `/D BUILDING_module01 ` in the config, you could have `/D DO_SOMETHING`. Then the source doesn't have to know how `BUILDING_module01` is different from `BUILDING_module02`. It just has to `do something` when asked to. – BoP Dec 08 '21 at 12:04
  • @BoP Now I got your points. It does make sense. Unfortunately, I had little control over config file. The only thing I can change in config file is targetName, and all the pre-processor flags `/D BUILDING_XXX` and `/D MODULENAME=XXX` are automatically generated by a .exe through parsing the config file. So I can not `/D DO_SOMETHING` in config file. – Mark Hayes Dec 09 '21 at 03:46

0 Answers0