c# - cmake build x64 使用安装的任何 visual studio 版本

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

我有一个包含两个平台的 C# 项目:x86 和 x64。此 C# 项目依赖于 native 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>

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

如果我在 Visual Studio 中使用 x86 配置,那么一切都很好。但是,如果我使用 x64 配置,我会失败,因为 native 库仍然构建为 x86。换句话说,当 $(Platform) 为“x64”时,我需要找到一种方法来通知 cmake 构建 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>

希望将来有人会觉得这有用!

最佳答案

不幸的是,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 中选择的目标平台处理。

我现在没有办法测试它,所以项目文件的实际语法可能不同,但这个想法应该可行。

关于c# - cmake build x64 使用安装的任何 visual studio 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22250443/

相关文章:

c# - Xamarin Plugin.BLE 为什么数据读取没有改变?

c++ - 缺少 QLayout 构造函数来实现卡片布局

c++ - std::map 一键,二值

c++ - VC++ 2010 中的 C++ Boost 安装问题(找不到文件)

node.js - 将 Dust.js 与 Visual Studio 的 Node.js 工具结合使用

c# - .NET 标准 : assembly's manifest definition does not match assembly reference

c# - 空的序列化 XML 文件

c# - Container.Register(Type, Assembly[]) 不注册泛型实现

c# - .NET 2.0 应用程序可以使用 .NET 3.5 的哪些功能?

c++ - 删除类成员映射时在映射析构函数 STL_map.h 中发生崩溃