Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

nuget

mono

I'm writing C# code using Mono 5.4.1.7 on Ubuntu 17.10.

Here's what I want to do, all from the command line:

  1. Install a package (specifically MathNet.Numerics) using NuGet.
  2. Compile a C# program that uses classes from that package.
  3. Run the program.

But I can't see any easy way to do this, so I must be missing something.

Here's what I tried. I created a directory 'foo' for my program. In that directory, I ran

$ nuget install MathNet.Numerics

That downloaded the MathNet.Numerics library and put it in a subdirectory 'MathNet.Numerics.3.20.2'. So far so good.

Then I created my test program foo.cs, which looks like this:

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 });
  }
}

But now I can't simply build using 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?

It works if I explicitly specify the installed assembly:

$ 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)

Now if I try to run the generated executable it fails:

$ 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'

It works only if I copy the library DLL to the current directory:

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

So yes, I found a way to get this to work, but this seems awkward and would be even more awkward if I were using many different libraries from NuGet.

So I must be missing something. Is there some build system I should be using that will make all of this automatic? Note that I'm on Linux, and would rather stay on the command line rather than use a big IDE (such as MonoDevelop) if possible.

like image 631
Adam Dingle Avatar asked Jan 31 '18 08:01

Adam Dingle


People also ask

How do I create a C file?

To write a C code file in Notepad, type your C code into a blank page in the text editor, and then save the file with a ". c" file extension if the file consists of a C code page, or the ". h" file extension if the file consists of header code.


1 Answers

Exactly such tasks are what build systems are for -> either create your own build-scripts which does the work for you or use one which already has them implemented, like the already mentioned MonoDevelop.
If you want to stay on the console only maybe look into .NET Core, as there is a command line build tool/system: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x

like image 105
Christoph Fink Avatar answered Sep 20 '22 14:09

Christoph Fink