Is There anyway to #define Constant
on a Visual Studio Solution Basis?
One can define a constant on a csproject basis, and one can put #define constant
in cs file, but I wonder whether one can define it on a vs sln basis?
Is There anyway to #define Constant
on a Visual Studio Solution Basis?
One can define a constant on a csproject basis, and one can put #define constant
in cs file, but I wonder whether one can define it on a vs sln basis?
You can actually use a variation on Ritch's approach with common project settings. Essentially you have to make a single change to the end of each project file in your solution:
<PropertyGroup Condition="'$(SolutionDir)' == '' or
'$(SolutionDir)' == '*undefined*'">
<SolutionDir>..\..\</SolutionDir>
</PropertyGroup>
<Import Project="$(SolutionDir)CommonSettings.targets" />
</Project>
Then you can define CommonSettings.targets to contain the solution wide settings.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="3.5">
<PropertyGroup>
<TacoBuild>true</TacoBuild>
</PropertyGroup>
</Project>
That's basically it unless you want to override or merge project settings already defined in each project. The link below discusses how to do this in some detail.
http://www.ademiller.com/blogs/tech/2007/12/common-project-settings-for-your-visual-studio-solution/
I have another approach for doing this:
Edit global config file. For example in my case it's .netcf2.0 so it's $(MSBuildBinPath)\microsoft.compactframework.csharp.targets
.
Add the following line:
<Import Project="$(SolutionDir)CommonSettings.targets" Condition="exists('$(SolutionDir)CommonSettings.targets')" />
This is to tell MSBuild import the CommonSettings.targets if it's existed in your solution folder.
Create CommonSettings.targets under your solution folder.
For example to define ABC symbol:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DefineConstants>$(DefineConstants);ABC</DefineConstants>
</PropertyGroup>
</Project>
Indirectly there is. If you set an environment variable with a flag and run VS:
set TacoBuild=true
devenv.eve bigproject.sln
or launch MSBuild from the command line:
msbuild /p:TacoBuild=true
In your .csproj files you need to add a PropertyGroup:
<PropertyGroup Condition="'$(TacoBuild)'=='true'">
<DefineConstants>$(DefineConstants);TacoBuild</DefineConstants>
</PropertyGroup>
In your code you need a preprocessor def:
#if TacoBuild
const int myConst = 42;
#endif
A VS only alternative is to define a preprocessor definition in the build settings for a new project configuration and use that configuration in your build.
A non MSBuild solution is to create a constants.cs file and write a custom settings tool that updates the cs file. I wish there were a better solution, but I haven't found one.
This is possible for Unity projects.
Creating a file called
mcs.rsp (targeting .Net 3.5 Equivalent) or
csc.rsp (targeting .Net 4.0 Equivalent )
in /Assets/ folder allows you to do that.
Example : /Assets/csc.rsp file includes:
-define:THIS_IS_MY_GLOBAL_PREPROCESSOR_CONSTANT
-define:ANOTHER_GLOBAL_PREPROCESSOR_CONSTANT
In the project, in any player or editor c# script:
#if THIS_IS_MY_GLOBAL_PREPROCESSOR_CONSTANT
Debug.Log("THIS_IS_MY_GLOBAL_PREPROCESSOR_CONSTANT is defined"); // compiles
#else
Debug.Log("THIS_IS_MY_GLOBAL_PREPROCESSOR_CONSTANT is not defined");
#endif
Tested on Unity 2019.1.2
https://docs.unity3d.com/Manual/PlatformDependentCompilation.html