简体   繁体   English

将我自己的C#项目配置添加到MSBuild / Visual Studio中

[英]Adding my own configuration for a C# project into MSBuild / Visual Studio

I could probably hack some stupid solution to my problem, but I would love to do it right and save myself future headaches. 我可能会破解一些愚蠢的解决方案来解决我的问题,但是我很乐意做对并节省自己将来的麻烦。 I can write simple .vcxproj files, but they usually end up being quite beginner-like and poor quality. 我可以编写简单的.vcxproj文件,但它们通常最终会变得像初学者一样,而且质量很差。

I have a large-ish project, which I would like to build with two options. 我有一个大型项目,我想用两个选项来构建。 The two options differ from each other simply by the choice of one particular source file (.cs). 仅通过选择一个特定的源文件(.cs),这两个选项就彼此不同。 The source directory contains both source files, each with its own unique file-name. 源目录包含两个源文件,每个文件都有自己的唯一文件名。 The configuration needs to build, and later initialize the right file at runtime. 需要构建配置,然后在运行时初始化正确的文件。 It is ok to build the two versions separately, but the switch must be clean. 可以分别构建两个版本,但是开关必须干净。 I do not really care whether the initialization functions have the same name, or have different names. 我并不在乎初始化函数是否具有相同的名称或名称。

I would love to get advice how to formulate that project's vcxproj file, so I can easily switch between building one version or the other version of my project. 我很想获得有关如何制定该项目的vcxproj文件的建议,因此我可以轻松地在构建项目的一个版本或另一个版本之间进行切换。

Thanks 谢谢

Using MsBuild's Condition you can enable/disable compilation based on a property like Configuration. 使用MsBuild的Condition您可以基于诸如Configuration之类的属性来启用/禁用编译。 This might be appropriate for your situation: suppose you now have 'Debug' and 'Release' configurations, you could add 'Debug_OptionB' and 'Release_OptionB' configurations. 这可能适合您的情况:假设您现在具有“ Debug”和“ Release”配置,则可以添加“ Debug_OptionB”和“ Release_OptionB”配置。 In the project file you create a boolean property based which is true if a configuration contains the 'OptionB' string: 在项目文件中,创建一个基于boolean的属性,如果配置包含'OptionB'字符串,则为true:

<PropertyGroup>
  <CompilingWithOptionB>$(Configuration.Contains('_OptionB'))</CompilingWithOptionB>
</PropertyGroup>

Now use this property for conditional compilation: 现在使用此属性进行条件编译:

<ClCompile Condition="'$(CompilingWithOptionB)'!='True'" Include="src_a.cpp" />
<ClCompile Condition="'$(CompilingWithOptionB)'=='True'" Include="src_b.cpp" />

If there are multiple source files to be included/excluded you'd put them in a seperate ItemGroup which then has the Condition, or even in seperate property sheets. 如果要包含/排除多个源文件,则将它们放在一个单独的ItemGroup中,然后再具有Condition,甚至放在单独的属性表中。

 I can write simple .vcxproj files, but they usually end up being quite beginner-like and poor quality

I'd advise not writing them yourself, but have VS create them for you and then add modifications. 我建议不要自己编写它们,而是让VS为您创建它们,然后添加修改。 This is easier and also assures that the integration with VS stays intact. 这更容易,并且可以确保与VS的集成保持完整。 Especially it makes adding/removing/ordering/inspecting property sheets easier (you are using them to set common configuration options, right?). 尤其是,它使添加/删除/订购/检查属性表变得更加容易(您正在使用它们来设置通用配置选项,对吗?)。

If you don't have strong reason to include/exclude files consider compiling all files in single set of binaries and using feature toggles or other configuration mechanism (like DI container configuration) to enable/disable particular features. 如果没有充分的理由要包含/排除文件,请考虑将所有文件编译为一组二进制文件,并使用功能切换或其他配置机制(例如DI容器配置)来启用/禁用特定功能。

Stijn's answer provides good steps to achieve conditional includes - have conditions based either on configuration names or better yet conditional symbols (like "DEBUG") and enable/disable individual items or groups with Condition=.... attribute in project file. Stijn的答案提供了实现条件包括的良好步骤-具有基于配置名称或更好的条件符号(例如“ DEBUG”)的Condition=.... ,并使用项目文件中的Condition=....属性启用/禁用单个项或组。

But be prepared that many VS tools and plugins will be at least useless (and sometimes very confused) about conditionally included code. 但是要做好准备,许多VS工具和插件对于有条件包含的代码至少是无用的(有时会很困惑)。 Testing such code in VS would also be harder as you'd be able to run test only against single flavor of the binaries at the time potentially leaving other set of files untested. 在VS中测试这样的代码也将更加困难,因为您当时只能针对单一风格的二进制文件运行测试,从而可能会使其他文件集未经测试。

Following are reasons I heard why one would like to have such "pick different files" behavior with possible alternative approaches: 以下是我听说过为什么希望使用可能的替代方法具有这种“选择不同文件”行为的原因:

  • have "full"/"limited" versions of software - this is probably only real reason why to do that. 具有“完整” /“有限”版本的软件-这可能是这样做的唯一真实原因。 Note that C#/.Net generally is not that protected from reverse engineering, it may be too easy to overcome such limitation by copying full version anyway. 请注意,C#/。Net通常不受逆向工程保护,反正通过复制完整版本来克服这种限制可能太容易了。 Make sure to weight against complexity of testing/supporting multiple versions 确保权衡测试/支持多个版本的复杂性
  • debug only code/tracing/... - #if and conditional attributes likely cover most of the cases so files can be included all the time and code enabled/disabled with regular conditional symbols (maybe even default DEBUG build configuration is enough). 仅调试代码/跟踪/ ...- #if和条件属性可能覆盖大多数情况,因此可以始终包含文件,并使用常规条件符号启用/禁用代码(甚至默认的DEBUG构建配置也足够)。 All tracing/logging libraries come with enough configuration settings so such file-level exclusions are not necessary 所有跟踪/日志库都具有足够的配置设置,因此不需要此类文件级排除
  • Wrappers for flavor specific libraries - like 32/64 bit interop. 包装针对特定风味的库-例如32/64位互操作。 This may be handled by separate assemblies included at run-time and/or carefully designed classes that pick correct implementation at run-time. 这可以通过运行时包含的单独程序集和/或精心设计的类(在运行时选择正确的实现)来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM