我有一个解决方案与2个项目:MSBuildQuestion.csproj
和NativeLibrary.vcxproj
.
MSBuildQuestion.csproj
等于MSBuildQuestion.exe
.NativeLibrary.vcxproj
等于NativeLibrary.dll
.
MSBuildQuestion.csproj
通过ProjectReference
引用NativeLibrary.vcxproj
,还通过CopyToOutputDirectory
:
<ItemGroup>
<ProjectReference Include="..\NativeLibrary\NativeLibrary.vcxproj" />
<Content Include="..\$(Platform)\$(Configuration)\*.dll">
<Link>NativeLibrary.dll</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
当我清理(通过删除所有输出文件夹),然后通过MSBuild从命令行构建解决方案时,它不会将NativeLibrary.dll
复制到MSBuildQuestion.csproj
的输出,尽管CopyToOutputDirectory
明确地告诉它这样做.当我try 运行MSBuildQuestion.exe
时,这当然会导致"Unable to Load Dll‘NativeLibrary.dll’"错误.
我是不是遗漏了什么,而这不是本机DLL在*.csproj
中应该被引用的方式?还是说这是MSBuild中的一个错误?
以下是我清理、构建和运行我的解决方案的方式:
Get-ChildItem . -include bin,obj,x64 -Recurse | ForEach-Object ($_) { Remove-Item $_.FullName -Force -Recurse }
nuget restore
msbuild /p:Platform=x64 /p:Configuration=Release
.\MSBuildQuestion\bin\x64\Release\net6.0\MSBuildQuestion.exe
重现这个问题的项目是here.
有一个观察:如果我运行msbuild /p:Platform=x64 /p:Configuration=Release
两次,NativeLibrary.dll
被复制了,然后一切都正常了.好像MSBuild违反了必须生成项目的顺序.不过,如果我判断MSBuild日志(log),似乎并没有违反顺序,因为接下来的行是按预期顺序记录的:
Done Building Project "...\NativeLibrary.vcxproj"
Done Building Project "...\MSBuildQuestion.csproj"
Done Building Project "...\MSBuildQuestion.sln"