簡體   English   中英

如何使用nuget的包在Mono中構建C#程序?

[英]How to build C# program in Mono with package from nuget?

我在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)。

正是這樣的任務是構建系統的用途 - >要么創建自己的構建腳本,為您完成工作,要么使用已經實現它們的構建腳本,如已經提到的MonoDevelop。
如果你想留在控制台上只是看看.NET Core,因為有一個命令行構建工具/系統: https//docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build ?標簽= netcore2x

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM