我正在尝试在 Linux 上运行一个简单的 .Net 程序。 (树莓派)。
我已经在 Raspberry Pi 上安装了 .NET。
我的 Program.cs 只有几行:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Console.WriteLine("Press ENTER to exit");
Console.Read();
由于我正在 Windows 计算机和 Raspberry Pi 上处理该项目,因此我在解决方案文件中添加了两个运行时:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
现在,当我想在 Linux 上构建 Windows 解决方案时,它工作得很好:
pi@raspberrypi:~/Documents/testproject $ dotnet build testproject.csproj
Determining projects to restore...
All projects are up-to-date for restore.
testproject -> /home/pi/Documents/testproject/bin/Debug/net8.0/testproject.dll
但是,当我想为Linux构建它时,在Linux上,它失败了:
pi@raspberrypi:~/Documents/testproject $ dotnet build testproject.csproj --runtime linux-x64
Determining projects to restore...
All projects are up-to-date for restore.
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: The "CreateAppHost" task failed unexpectedly. [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: System.ArgumentException: A positive capacity must be specified for a Memory Mapped File backed by an empty file. [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(FileStream fileStream, String mapName, Int64 capacity, MemoryMappedFileAccess access, HandleInheritability inheritability, Boolean leaveOpen) [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at Microsoft.NET.HostModel.AppHost.HostWriter.<>c__DisplayClass2_0.<CreateAppHost>b__1() [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at Microsoft.NET.HostModel.RetryUtil.RetryOnIOError(Action func) [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface, String assemblyToCopyResourcesFrom, Boolean enableMacOSCodeSign) [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at Microsoft.NET.Build.Tasks.CreateAppHost.ExecuteCore() [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Execute() [/home/pi/Documents/testproject/testproject.csproj]
/home/pi/.dotnet/sdk/8.0.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(709,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(TaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/home/pi/Documents/testproject/testproject.csproj]
Build FAILED.
我无法找到有关此错误的任何有意义的信息,或者为什么在运行时会发生这种情况
dotnet build
。 干杯, 希杰
编辑:
我尝试使用
sudo
运行命令,但这引发了错误:
sudo: dotnet: command not found
。 su
下运行它时,它确实可以编译。
所以这似乎是一个权限问题。
我错误地认为针对 Linux 进行编译会创建不同类型的文件,而不是 dll。然而事实并非如此。 即使在针对 Linux 进行编译时,它也会创建一个我可以使用
dotnet
运行的 DLL。