Tutorial :How to conditionally compile VC6 resources



Question:

depending on a compile switch (values are COMPILE_A or COMPILE_B), which is set in the form of an envorinment variable, I want to compile my application with different settings, like application name and splash screen.

I got this far:

  1. In "Project / Settings / C/C++ / Preprocessor Definitions" I added $(COMPILESWITCH) (results in command line option /D "$(COMPILESWITCH)").

  2. In stdafx.h I can use the following code, which means I correctly defined the preprocessor definition via the command line parameter:

      #if defined COMPILE_A      #   define IDB_SPLASH IDB_SPLASH_A      # elif defined COMPILE_B      #   define IDB_SPLASH IDB_SPLASH_B      # else      #   error Unknown or undefined target compile switch; cannot compile!      # endif  

But I've noticed the "Condition" property under "ResourceView / [right-click] / Properties"... The help text says this:

Condition

Determines the inclusion of the resource. For example, if the condition is _DEBUG, this resource would be included only in debug builds.

This looks like the elegant way of doing it, right?

Specifiying _DEBUG as condition works. So as _DEBUG is specified via /D _DEBUG my $(COMPILESWITCH) should also work, right?
For some reason it doesn't; why?

Or is there even another, better way to achieve what I want?


Solution:1

I guess I just solved my problem...

The resource compiler uses its own preprocessor.
Therefore the same preprocessor definition has to be added under "Project / Settings / Resources / Preprocessor Definitions".

Edit: String Resources

The above doesn't work for string resources as they don't have a "condition" property...

I chose to use the res\<projectname>.rc2 custom resource file which won't be touched by the resource editor. The content looks like this

  #if defined(COMPILE_A)      STRINGTABLE DISCARDABLE       BEGIN          IDR_MAINFRAME           "AppTitle A"      END  #else  #   if defined(COMPILE_B)      STRINGTABLE DISCARDABLE       BEGIN          IDR_MAINFRAME           "AppTitle B"      END  #   else  #       error Compile switch not defined or unknown; cannot compile!  #   endif  





        
Previous
Next Post »