【问题标题】:cmake build x64 using whichever visual studio version is installedcmake 使用安装的任何 Visual Studio 版本构建 x64
【发布时间】:2014-03-07 12:44:24
【问题描述】:

我有一个包含两个平台的 C# 项目:x86 和 x64。此 C# 项目依赖于一个原生 dll,该 dll 也必须构建为 x86 或 x64。

到目前为止,我已经成功地将cmake作为预构建事件添加到C#项目的.csproj中:

<Target Name="BeforeBuild">
    <Message Text="Building native library" />
    <Exec Command="cmake -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
    <Exec Command="cmake --build . --target install" />
</Target>

这会构建原生 dll 并将其复制到项目的输出目录,匹配所选配置(例如 bin/x86/Debug 或 bin/x64/Release)。

如果我在 Visual Studio 中使用 x86 配置,那么一切都很好。但是,如果我使用 x64 配置,我会失败,因为本机库仍然构建为 x86。换句话说,我需要找到一种方法来通知 cmake 在 $(Platform) 为 'x64' 时构建 x64 二进制文件。

是否有命令行开关来指示 cmake 构建 x64 二进制文件?我尝试了 -G 和 -T 选项,但它们似乎不支持这一点(或者我无法找到使用它们的正确方法)。

欢迎提出想法!

编辑:通过将 $(VisualStudioVersion) 属性传递给 cmake,我设法更接近了一点。

<PropertyGroup>
    <CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'x86'">-G"Visual Studio $(VisualStudioVersion)"</CMakeGenerator>
    <CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'AnyCPU'">-G"Visual Studio $(VisualStudioVersion) Win64"</CMakeGenerator>
    <CMakeGenerator Condition="'$(OS)' != 'Windows_NT'"></CMakeGenerator>
</PropertyGroup>
<Target Name="BeforeBuild">
    <Message Text="Building native library" />
    <Exec Command="cmake $(CMakeGenerator) -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
    <Exec Command="cmake --build . --target install" />
</Target>

不幸的是,$(VisualStudioVersion) 返回一个十进制数(例如 VS2013 的 12.0),而 cmake 需要一个整数(例如 VS2013 的 12)。如果我可以将 $(VisualStudioVersion) 转换为整数,那么这应该可以!这个可以吗?

编辑 2:已解决!

MSBuild 4.0/4.5 添加了property functions,可用于修改属性。在这种情况下,以下操作完美:

<PropertyGroup>
    <CMakeVSVersion>$(VisualStudioVersion.Substring(0, $(VisualStudioVersion).IndexOf(".")))</CMakeVSVersion>
    <CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'x86'">-G"Visual Studio $(CMakeVSVersion)"</CMakeGenerator>
    <CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'AnyCPU'">-G"Visual Studio $(CMakeVSVersion) Win64"</CMakeGenerator>
    <CMakeGenerator Condition="'$(OS)' != 'Windows_NT'"></CMakeGenerator>
</PropertyGroup>
<Target Name="BeforeBuild">
    <Message Text="Building native library" />
    <Exec Command="cmake $(CMakeGenerator) -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
    <Exec Command="cmake --build . --target install" />
</Target>

希望将来有人会发现这很有用!

【问题讨论】:

  • 请将您的答案写成答案,并在您满意时接受它是最好的。

标签: c# c++ visual-studio msbuild cmake


【解决方案1】:

不幸的是,Visual Studio 生成器需要为 x64 构建显式指定 Visual Studio 版本。但是,您应该能够使用 NMake 生成器来完成这项工作:

<Target Name="BeforeBuild">
    <Message Text="Build native library" />
    <Exec Command="cmake -G&quot;NMake Makefiles&quot; -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
    <Exec Command="cmake --build . --target install" />
</Target>

NMake 生成器希望找到在命令行上正确设置的所有内容(包括相应平台编译器的路径),但这由驱动 VS 中选择的目标平台负责。

我现在没有办法对其进行测试,因此项目文件的实际语法可能会有所不同,但这个想法应该可行。

【讨论】:

  • 我刚试过这个,我遇到了compiler cl not found 问题。看来 Visual Studio 生成的 cmd 实例在其路径中没有 cl,这意味着这将不起作用...
  • @TheFiddler :-( 我用 VS2008 试过这个(使用set &gt; vars.txt 作为预构建命令),cl 肯定在PATH 中(但那是一个 C++ 项目)。也许您可以使用cmd /c "path/to/vcvars.bat" &amp; cmake ... 作为命令,并在path/to 部分中使用适当的VS 宏(或环境变量)?
  • 谢谢,我的 msbuild 脚本可能是 C# 项目,而不是 C++ 项目。我现在正在尝试一种稍微不同的方法(检查我的编辑),但如果这不起作用,我将尝试使用 vcvars.bat。
  • 我接受了这个答案,因为它是一个有效的解决方案(只要 nmake 在你的路径中)并且它指向我现在正在使用的解决方案(使用 -G"Visual Studio $(VisualStudioVersion)").
猜你喜欢
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多