简体   繁体   English

如何在 .NET Core 上直接调用 F# 编译器?

[英]How to directly invoke F# compiler on .NET Core?

UPD.:升级版:

I want to invoke F# compiler (ie fsc) from .NET Core SDK directly.我想直接从 .NET Core SDK 调用 F# 编译器(即 fsc)。

I know about dotnet build & co, but I don't want to involve them when I only need to compile a simple problem, ie in cases when fsc file.fs would be enough.我知道 dotnet build & co,但是当我只需要编译一个简单的问题时,我不想让他们参与进来,即在 fsc file.fs 就足够的情况下。

I've tried to search in .NET Core SDK (on my Mac, it was in /usr/local/share/dotnet/sdk/2.2.102/FSharp) and found a fsc.exe file there.我尝试在 .NET Core SDK 中搜索(在我的 Mac 上,它位于 /usr/local/share/dotnet/sdk/2.2.102/FSharp 中)并在那里找到了一个 fsc.exe 文件。 Unfortunately, when I try to start it with dotnet /usr/local/share/dotnet/sdk/2.2.102/FSharp/fsc.exe, it gives me an error:不幸的是,当我尝试使用 dotnet /usr/local/share/dotnet/sdk/2.2.102/FSharp/fsc.exe 启动它时,它给了我一个错误:

error FS0193: Could not load file or assembly 'Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Env.: macOS, .NET Core 2.2环境:macOS、.NET Core 2.2

After install dotnet core ( https://dotnet.microsoft.com/download ) you should be able to build a F# project from the command line.安装 dotnet core ( https://dotnet.microsoft.com/download ) 后,您应该能够从命令行构建 F# 项目。

cd my-fs-project
dotnet build
dotnet run

You probably don't want to call fsc directly, since dotnet build and fsproj is the preferred way of building projects.可能不想直接调用fsc ,因为dotnet buildfsproj是构建项目的首选方式。

If you must though, then it can be found in the SDK files:如果你必须,那么它可以在 SDK 文件中找到:

dotnet /usr/share/dotnet/sdk/5.0.100/FSharp/fsc.exe

Use dotnet --list-sdks to find the full path to your SDK.使用dotnet --list-sdks查找 SDK 的完整路径。

I compile using fsc on Linux in following way.我通过以下方式在 Linux 上使用fsc进行编译。

fsc test.fs --targetprofile:netcore --target:exe --out:test.exe

Then I manually create test.runtimeconfig.json with following content然后我用以下内容手动创建test.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "5.0.0"
    },
    "rollForwardOnNoCandidateFx": 2
  }
}

Then I was able to run application using dotnet test.exe .然后我能够使用dotnet test.exe运行应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM