【发布时间】: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