【问题标题】:How to build C# program in Mono with package from nuget?如何使用来自 nuget 的包在 Mono 中构建 C# 程序?
【发布时间】:2018-01-31 16:42:48
【问题描述】:

我正在 Ubuntu 17.10 上使用 Mono 5.4.1.7 编写 C# 代码。

这是我想做的,全部来自命令行:

  1. 使用 NuGet 安装包(特别是 MathNet.Numerics)。
  2. 编译一个使用该包中的类的 C# 程序。
  3. 运行程序。

但是我看不到任何简单的方法可以做到这一点,所以我一定是错过了一些东西。

这是我尝试过的。我为我的程序创建了一个目录“foo”。在那个目录中,我跑了

$ nuget install MathNet.Numerics

下载了 MathNet.Numerics 库并将其放在子目录“MathNet.Numerics.3.20.2”中。到目前为止一切顺利。

然后我创建了我的测试程序 foo.cs,如下所示:

using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

class Foo {
  static void Main() {
    Vector<double> A = DenseVector.OfArray(new double[] { 1, 2, 3, 4 });
  }
}

但现在我不能简单地使用 mcs 构建:

$ mcs Foo.cs
foo.cs(1,7): error CS0246: The type or namespace name `MathNet' could not be found. Are you missing an assembly reference?
foo.cs(2,7): error CS0246: The type or namespace name `MathNet' could not be found. Are you missing an assembly reference?

如果我明确指定安装的程序集,它会起作用:

$ mcs -reference:MathNet.Numerics.3.20.2/lib/net40/MathNet.Numerics.dll foo.csfoo.cs(6,20): warning CS0219: The variable `A' is assigned but its value is never used
Compilation succeeded - 1 warning(s)

现在,如果我尝试运行生成的可执行文件,它会失败:

$ mono foo.exe

$ mono foo.exe
Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null'
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'MathNet.Numerics, Version=3.20.2.0, Culture=neutral, PublicKeyToken=null'

仅当我将库 DLL 复制到当前目录时才有效:

$ cp MathNet.Numerics.3.20.2/lib/net40/MathNet.Numerics.dll .
$ mono foo.exe
$

所以,是的,我找到了一种让它工作的方法,但这看起来很尴尬,如果我使用 NuGet 中的许多不同库会更加尴尬。

所以我一定错过了什么。我应该使用一些构建系统来使所有这些自动化吗?请注意,我使用的是 Linux,如果可能的话,我宁愿留在命令行上,也不愿使用大型 IDE(例如 MonoDevelop)。

【问题讨论】:

    标签: c# .net mono nuget


    【解决方案1】:

    这些任务正是构建系统的用途 -> 要么创建您自己的构建脚本来为您完成工作,要么使用已经实现它们的构建脚本,例如已经提到的 MonoDevelop。
    如果您只想留在控制台上,可以查看 .NET Core,因为有一个命令行构建工具/系统:https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x

    【讨论】:

    • 谢谢 - 我刚刚查看了 .NET Core 中的 dotnet 命令行构建工具,它正是我想要的。 (我还看到 Mono 现在带有 msbuild,这是另一个可以从命令行使用的构建工具。但是 dotnet 看起来更适合在没有 IDE 的情况下使用命令行。)
    • 不幸的是,这个答案没有解释如何使用 monodevelop 导入 nuget 包。只是为了保持一致。
    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多