【问题标题】:Conditionals in Visual Studio 2010 Project filesVisual Studio 2010 项目文件中的条件
【发布时间】:2010-07-29 00:57:26
【问题描述】:

在 Visual Studio 2010 C++ 项目文件中,是否可以使用条件来确定库的存在,并适当地更改预处理器标志等?

更具体地说,假设我们有一个目录C:\libraries\MKL,我想#define MKL 并添加 mkl_dll.lib 作为附加依赖项(如果该目录存在)。

以前我们使用多种解决方案配置来实现这一点,但这很难维护。

【问题讨论】:

    标签: c++ visual-studio-2010 msbuild projects-and-solutions


    【解决方案1】:

    以下内容在粘贴到 F# 项目的底部时具有建议的效果(如果存在 c:\temp\foo.txt,则为 THE_FILE_EXISTS 添加一个 #define)。我希望 C++ 项目只需要稍作修改,因为它们都使用 MSBuild。这可能有点 hacky,这是我开始工作的第一件事。

    <UsingTask TaskName="SeeIfFileExists" TaskFactory="CodeTaskFactory" 
        AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
      <ParameterGroup>
        <Path ParameterType="System.String" Required="true" />
        <ItExists ParameterType="System.Boolean" Output="true" />
      </ParameterGroup>
      <Task>
        <Code Type="Fragment" Language="cs">
          <![CDATA[
    ItExists = System.IO.File.Exists(Path);
    ]]>
        </Code>
      </Task>
    </UsingTask>
    <Target Name="SeeIfFileExistsTarget" BeforeTargets="PrepareForBuild">
      <SeeIfFileExists Path="c:\temp\foo.txt" >
        <Output TaskParameter="ItExists" ItemName="TheFileExists" />
      </SeeIfFileExists>
      <PropertyGroup>
        <DefineConstants Condition="'@(TheFileExists)'=='True'"
            >$(DefineConstants);THE_FILE_EXISTS</DefineConstants>
      </PropertyGroup>
    </Target>
    

    我突然想到

    <PropertyGroup>
        <DefineConstants Condition="Exists('c:\temp\foo.txt')"
            >$(DefineConstants);THE_FILE_EXISTS</DefineConstants>
    </PropertyGroup>
    

    可能就足够了,但没有那么性感。

    【讨论】:

    • 谢谢。我最终无法让第一个版本工作,但第二个版本很有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多