【发布时间】:2018-01-31 16:42:48
【问题描述】:
我正在 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)
现在,如果我尝试运行生成的可执行文件,它会失败:
$ 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)。
【问题讨论】: