如何使用来自 nuget 的包在 Mono 中构建 C# 程序?
How to build C# program in Mono with package from nuget?
我正在 Ubuntu 17.10 上使用 Mono 5.4.1.7 编写 C# 代码。
这是我想做的,全部来自命令行:
- 使用 NuGet 安装包(特别是 MathNet.Numerics)。
- 编译一个 C# 程序,该程序使用来自该包的 类。
- 运行 程序。
但是我看不到任何简单的方法来做到这一点,所以我一定是遗漏了什么。
这是我试过的。我为我的程序创建了一个目录 '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)
现在,如果我尝试 运行 生成的可执行文件,它会失败:
$单声道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,因为有一个命令行构建 tool/system: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x
我正在 Ubuntu 17.10 上使用 Mono 5.4.1.7 编写 C# 代码。
这是我想做的,全部来自命令行:
- 使用 NuGet 安装包(特别是 MathNet.Numerics)。
- 编译一个 C# 程序,该程序使用来自该包的 类。
- 运行 程序。
但是我看不到任何简单的方法来做到这一点,所以我一定是遗漏了什么。
这是我试过的。我为我的程序创建了一个目录 '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)
现在,如果我尝试 运行 生成的可执行文件,它会失败:
$单声道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,因为有一个命令行构建 tool/system: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x