Solution-wide #define

前端 未结 11 2267
予麋鹿
予麋鹿 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:35

    There is one more workaround, correct me if i'm wrong:

    For example, ProjectA references ProjectB, and ProjectsCommon contains base static variable. Both projects refernece ProjectsCommon.

    Code in ProjectsCommon:

    [assembly: InternalsVisibleTo("ProjectA")]
    
    ------------------------------------------
    
    public class ConditionVariables
    {
        public static bool IsABC { get; internal set; } = false;
    }
    

    Code in ProjectA:

    #if ABC
        ProjectsCommon.ConditionVariables.IsABC = true;
    #endif
    

    Code in ProjectB:

    if (ProjectsCommon.ConditionVariables.IsABC )
    {
        // first scenario
    }
    else
    {
        // second one
    }
    

    However, code in ProjectA should run first. This is probably good when ProjectA is a bootstarapper project.

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

    To expand on the answers @Murtago and @Mark Cidade to make it solution wide:

    Open the configuration manager for the solution and create a new configuration (copied from the closest match) then change conditional symbols for the projects that need different ones.

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

    I don't know of a solution wide definition, but I was able to quickly add a preprocessor definition to all projects by doing the following:

    1. hold ctrl and select each project
    2. right click on any selected project and choose Properties
    3. go to C/C++ --> Preprocessor
    4. click the little down arrow beside Preprocessor Definitions
    5. choose <Edit...>
    6. add my definition after <different options>
    7. OK out of the menus

    I'm using Visual Studio Community 2019 with a C code base.

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

    I know solution for C# projects (I don't tested it for any other projects)

    For example you have:

    Project1\
    Project2\
    Solution1\Solution1.sln
    Solution2\Solution2.sln
    

    Create SolutionDefines.targets file in solution directory

    Project1\
    Project2\
    Solution1\Solution1.sln
    Solution1\SolutionDefines.targets
    Solution2\Solution2.sln
    Solution2\SolutionDefines.targets
    Solution3\Solution2.sln
    Solution3\|no target file|
    

    in each project file add:

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

    In Solution1\SolutionDefines.targets add:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <DefineConstants>$(DefineConstants);TRACING_BUILD</DefineConstants>
        </PropertyGroup>
    </Project>
    

    In Solution2\SolutionDefines.targets add:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <DefineConstants>$(DefineConstants);ANOTHER_DEFINE</DefineConstants>
        </PropertyGroup>
    </Project>
    

    In this case you have:

    For Solution1 - all projects have TRACING_BUILD define added

    For Solution2 - all projects have ANOTHER_DEFINE define added

    For Solution3 - all projects - no defines added

    In this approach you must store all solutions with solution wide defines in separate directories

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

    Create a new solution configuration in Configuration Manager and make sure that the box is checked to create new project configurations as well. And then in the Build properties of each project enter MONO into the Conditional compilation symbols box.

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

    The proper place for a pre-processor directive is the VS build configuration. If you are using a command line to build your solution you can also pass it using /define option.

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