In Visual Studio 2017, you can define the conditional compilation symbols at project level by going to Project Properties > Build > General > Conditional compilation symbols.
- If you want to share these symbols across multiple projects, it is not possible using the GUI. Here is what needs to be done. I got the answer specifically from this SO question but it did not list the exact steps so I am giving all the steps here
- Close the Visual Studio Solution
- In solution directory, create a file “CommonSettings.targets” and add following text in that file. Change the MY_CONST_1, MY_CONST_2 and MY_CONST_3 to your constants
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <DefineConstants>$(DefineConstants);MY_CONST_1;MY_CONST_2;MY_CONST_3</DefineConstants> </PropertyGroup> </Project>
- Now go into each project directory and open myproject.csprj or other project file in text editor.
- Locate the “</Project>” at the very bottom.
- Right before this closing xml tag, add following xml tag, save the file in text editor and close the file.
<Import Project="$(SolutionDir)CommonSettings.targets" />
- Now open the solution in Visual Studio and if you look at Project Properties > Build > General > Conditional compilation symbols, you will see MY_CONST_1, MY_CONST_2 and MY_CONST_3.
- If for any reason, Visual Studio does not reflect the defined constants even after Clean/Build/Rebuild, refresh the project.
Vishal Monpara is a full stack Solution Developer/Architect with 13 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1’s and 0’s from geographically dispersed hard disks to geographically dispersed user leveraging geographically dispersed team members.