6

I neeed to create multiple versions of my project using configuration just like we do with #define, #if, #endif.

The down side of using these preprocessor directives is that I need to define symbols in almost every file of the project but I want to handle this thing by my build configurations.

I am not even sure if Build Configurations will help me to do this.

What I want is if I create a configuration with name "Development" and other with name "QA", my code would look like:

if #Development or if $QA

Kindly guide me towards achieving this.

bluish
  • 26,356
  • 27
  • 122
  • 180
Manvinder
  • 4,495
  • 16
  • 53
  • 100

2 Answers2

10

Configuration Manager exist for this reason.

  • Go to the Configuration Manager and create a New Configuration copying from the predefined DEBUG configuration
  • Name the configuration DEVELOPMENT and apply to all projects
  • Select as Active Configuration the DEVELOPMENT configuration (should already be the active one)
  • Go to the properties page of each project requiring #if DEVELOPMENT conditional compile and insert the DEVELOPMENT symbol in first textbox of the BUILD tab

Now each of your projects can use the #if DEVELOPMENT preprocessor directive

If you need this also for RELEASE repeat the above steps but copy from predefined RELEASE configuration and give a different NAME

Now switching from a configuration with or without the DEVELOPMENT symbol defined could be done directly from the Solution Configurations combo Tool present in the Standard Toolbar of Visual Studio without editing each project.

You can also view MSDN article How to: Create and Edit Configurations

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks Steve, why should i need to copy my configurations from predefined configurations – Manvinder Apr 10 '12 at 10:13
  • Because the DEBUG configuration contains settings needed during DEBUG session, so for the RELEASE configuration (for example the DEBUG & TRACE symbols). If you start from scratch you risk losing functionality provided by the missing symbols – Steve Apr 10 '12 at 10:18
  • I am not sure about the 3rd step, kindly make it more clear. I am not able to find the COMPILE tab in project properties – Manvinder Apr 10 '12 at 10:30
  • Sorry, translated from italian VS, the tab should be named 'Build' (the second from top) and the textbox to insert your symbol should be labeled 'Conditional Symbols' or something like that. Is the first textbox in the page. – Steve Apr 10 '12 at 10:35
  • 3
    I am so overwhelmed with your answer, not because it solves my problem, but because i've been working so long in Visual Studio and never bother to know the use of this textbox. BIG THANKS – Manvinder Apr 10 '12 at 10:43
  • Hi, I still can't find the BUILD tab in bullet point 4. I'm in VS 2012. – Chro May 23 '13 at 18:08
  • Also, I'm trying to use this feature for a C++ project, if that makes a difference. – Chro May 23 '13 at 18:15
  • @Chro Not sure (I don't use C++ since the days before NET), but I think you could reach the same result using Project Properties, Configuration Properties, C/C++, Preprocessor, Preprocesssor Definitions and then add to the already defined symbols. Let me know if it works so I could complete to the answer – Steve May 23 '13 at 18:53
  • Yep, that does it, thanks so much! I had made a SO question about it but added your answer and gave you credit. – Chro May 23 '13 at 19:00
  • This works, but can be quite time consuming to implement and can require a lot of useless extra work. I explain my point: there are 8 different define symbols to activate 8 different options. One needs to write 256 different release configurations and 256 different debug configurations (and maybe repeat for every target processor). Then, when one wants to select a combination of options has to find the right one among them. Is there any other way? – Angelo Mascaro Sep 30 '16 at 10:44
  • As far as I know, no. But 256 different combinations suggest that, perhaps, you need to divide your work in different assemblies and distribute conditionally the required ones. But of course I know nothing of your situation. This could be matter of a specific new question. – Steve Sep 30 '16 at 11:08
  • Thank you Steve, I appreciate your suggestion and the way you offered it to me. It's not a case. This package is meant to configure something in other packages (also the protection dongle) and the most of the flags are enable/disable one button or one feature, embed serial numbers of some dongles or let the SW read the serials, set values of some constants to different values or avoid to embed these constants... It would be nice to have just one build config with a property page showing all the define symbols. Maybe I'm going to make another small program to configure the symbols :-) – Angelo Mascaro Oct 03 '16 at 06:36
0

In addition to Michael Freidgeim´s solution you can use attribute conditional for central initalisation operations or other void functions:

[Conditional("DEVELOPMENT")]
public static void InitDemo()
{
      Models.LogFile.ErrorLog("This is a Development Version!");
      // init settings
}

Found here: http://msdn.microsoft.com/de-de/library/4xssyw96%28v=vs.80%29.aspx

Simon
  • 4,157
  • 2
  • 46
  • 87