Solution-wide #define

前端 未结 11 2268
予麋鹿
予麋鹿 2020-12-05 09:29

Is there a way to globally declare a #define?

Like I want to have a file that has for instance,

#define MONO

and I want all source-

相关标签:
11条回答
  • 2020-12-05 09:46

    Years later, and similar to Alexei's answer but supported innately

    One can make a Directory.Build.props similar to a NuGet.Config file as per

    https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019

    Ours looks like:

    <Project>
        <PropertyGroup>
            <DefineConstants>RC_427</DefineConstants>
        </PropertyGroup>
    </Project>
    

    And it effectively includes this into all CSPROJ files in your SLN. For some reason that particular solution is insanely hard to find via google. Been around since MSBuild 15

    0 讨论(0)
  • 2020-12-05 09:50

    I don't think there is a way to create a solution-wide #define. You can create one for each project/assembly, as the other answers have described, but you'll need to do this for each and every project in the solution if you need all source code files to know of that #define.

    0 讨论(0)
  • 2020-12-05 09:52

    In case you have both C# and C++ projects in the solution and need to share preprocessor definitions among them. Create some SolutionDefines.targets file near your *.sln as follows

    <?xml version="1.0" encoding="utf-8"?>
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <DefineConstants>$(DefineConstants);MY_DEFINITION</DefineConstants>
        </PropertyGroup>
      <ItemDefinitionGroup>
        <ClCompile>
          <PreprocessorDefinitions>MY_DEFINITION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
      </ItemDefinitionGroup>
    </Project>
    

    Then add to C/C++/C# *.vcproj/*.vcxproj/*.csproj project files this line somewhere in the end

    <Import Project="$(SolutionDir)SolutionDefines.targets" Condition="exists('$(SolutionDir)SolutionDefines.targets')" />
    

    Ok, now you can enjoy #if MY_DEFINITION in C# and #ifdef MY_DEFINITION in C/C++. Visual Studio C# editor doesn't like this way and tells there is no MY_DEFINITION defined. But, C# compiler csc.exe gets the symbol properly.

    0 讨论(0)
  • 2020-12-05 09:57

    in vs 2013 you can use /define:x or /d:x http://msdn.microsoft.com/en-us/library/0feaad6z.aspx

    0 讨论(0)
  • 2020-12-05 09:59

    Update: You cannot do a "solution-wide" define afaik, however the answer below is workable on a per-project basis.

    You set them in your Compilation Properties or Build options:

    http://msdn.microsoft.com/en-US/library/76zdzba1(v=VS.80).aspx (VS2008) http://msdn.microsoft.com/en-US/library/76zdzba1(v=VS.100).aspx (VS2010)

    see the "To set a custom constant" heading.

    Update

    Microsoft Documentation on Build Options

    You get to the build options by right-clicking the project and selecting properties from the menu.

    Project Build Options

    0 讨论(0)
提交回复
热议问题