Can't get System.Numerics to work with command

2019-02-25 07:22发布

问题:

We're trying to run a simple Mono script on the command line on OS X. Most scripts work fine for us, but as soon as we try to use System.Numerics, we get "error CS0234: The type or namespace name `Numerics' does not exist in the namespace System."

This isn't too surprising, and should be fixable with an appropriate command-line option to mcs, plus properly set up PKG_CONFIG_PATH... but this is where we get stumped. First, here's the script so you can follow along at home:

using System;
using System.Numerics;

public static class MainProgram {
    public static void Main(string[] args) {
        Console.WriteLine("Hello world!");
    }
}

So next we tried "mcs -r:System.Numerics Test.cs". This produces "error CS0006: Metadata file `System.Numerics' could not be found".

"man mcs" suggests that we can get the other system packages by adding "-pkg:dotnet" to the command line. But that produces:

Package dotnet was not found in the pkg-config search path. Perhaps you should add the directory containing `dotnet.pc' to the PKG_CONFIG_PATH environment variable No package 'dotnet' found error CS8027: Error running pkg-config. Check the above output.

OK then, we had no PKG_CONFIG_PATH, so we tried defining one:

export PKG_CONFIG_PATH=/Library/Frameworks/Mono.framework/Versions/Current/lib/pkgconfig/

This fixes the CS8027; but we still get the CS0234 we started with. And if I combine the -pkg and the -r, e.g. "mcs -pkg:dotnet -r:System.Numerics Test.cs", I get "error CS0006: Metadata file `System.Numerics' could not be found".

I'm stumped at this point... any idea what incantation I'm missing to make System.Numerics work with mcs?

回答1:

If you're using Mono 2.10.x, you will have to compile with dmcs rather than mcs to enable the 4.0 profile (System.Numerics is a C# 4.0+ feature only).

If you're using Mono 2.11.x or 3.0.x, then mcs by default should select the 4.5 profile. mcs -help should show 2, 4, and 4.5 as possible values for the -sdk option. If it doesn't, then the framework isn't properly installed; I had that once, where I think that /Library/Frameworks/Mono.framework/Versions/Current pointed to the wrong directory; installing a second time fixed that.

Manipulating pkg-config should be unnecessary.