我正在使用.NET Core构建跨平台类库。根据为使用.csproj文件而构建的C#.NET Core项目的操作系统,我需要将本机库复制到项目的输出目录。例如,对于OS XI,要复制.dylib文件,对于Windows,我要复制.DLL文件,对于Linux,我要复制.so文件。
I am using .NET Core to build a cross platform class library. Depending on the operating system that the C# .NET Core project is built for using a .csproj file, I need to copy a native library to the project's output directory. E.g., for OS X I want to copy a .dylib file, for Windows I want to copy a .DLL file, for Linux I want to copy a .so file.
如何在.csproj ItemGroup中使用Condition子句来做到这一点?
How can I do this with a Condition clause in a .csproj ItemGroup?
<ItemGroup> <Content Include="libNative.dylib" Condition=" '$(Configuration)|$(Platform)' == 'Debug|OSX' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>$(Platform)工作。我可以使用其他变量吗?
$(Platform) does not seem to work. Is there a different variable I can use?
推荐答案用于区分Windows和Windows。在Mac / Linux中,您可以使用 $(os)属性:这将为Windows提供 Windows_NT 和用于Mac / Linux的UNIX 。
For differentiating between Windows & Mac/Linux you can use the $(os) property: this gives you Windows_NT for Windows and UNIX for Mac/Linux.
用于区分Mac和Linux,至少在最新版本的MSBuild上,您可以使用 RuntimeInformation.IsOSPlatform 。
For differentiating between Mac & Linux, at least on recent versions of MSBuild, you can use RuntimeInformation.IsOSPlatform.
因此, csproj可以具有以下内容:
So, combined, your csproj can have something like this:
<ItemGroup> <Content Include="libNative.dll" Condition=" '$(OS)' == 'Windows_NT' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="libNative.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="libNative.dylib" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>据我所知,这应该适用于所有最新版本的.Net Core,Mono和 Framework。
To the best of my knowledge, this should work in all recent versions of .Net Core, Mono and .Net Framework.
在较旧的版本中,您需要采取恶作剧的手段来区分Mac和Linux:
In older versions, you'd need to resort to evil trickery for differentiating between Mac & Linux:
对于Linux-> Condition ='$(OS)'=='Unix'和!$([System.IO。 File] :: Exists('/ usr / lib / libc.dylib'))
For Linux -> Condition=" '$(OS)' == 'Unix' and ! $([System.IO.File]::Exists('/usr/lib/libc.dylib')) "
对于Mac-> Condition ='$(OS)'=='Unix'和$([System.IO.File] :: Exists('/ usr / lib / libc.dylib'))
来源:
- github/Microsoft/msbuild/issues/539
- github/Microsoft/msbuild/issues/2468
- github/Microsoft/msbuild/issues/539
- github/Microsoft/msbuild/issues/2468
更多推荐
csproj复制文件取决于操作系统
发布评论