我有一个包含两个平台的 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"NMake Makefiles" -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/