Console.WriteLine

Monoは、オープンソースの.NET互換環境。

GitHub - mono/mono: Mono open source ECMA CLI, C# and .NET implementation.
Home | Mono

LinuxC#などの.NETのプログラムを動かすには、これを使う。
mono-develのインストール。

$ sudo yum install mono-devel
[sudo] bg1 のパスワード:
メタデータの期限切れの最終確認: xx:xx:xx 時間前の xxxx年xx月xx日 xx時xx分xx秒 に実施しました。
依存関係が解決しました。
================================================================================
 パッケージ              アーキテクチャー
                                       バージョン           リポジトリ    サイズ
================================================================================
インストール:
 mono-devel              x86_64        4.8.0-12.fc28        fedora        5.3 M
依存関係をインストール中:
 libgdiplus              x86_64        5.6-2.fc29           fedora        176 k
 mono-core               x86_64        4.8.0-12.fc28        fedora         18 M
 mono-data               x86_64        4.8.0-12.fc28        fedora        4.4 M
 mono-data-sqlite        x86_64        4.8.0-12.fc28        fedora        103 k
 mono-extras             x86_64        4.8.0-12.fc28        fedora        465 k
 mono-mvc                x86_64        4.8.0-12.fc28        fedora        484 k
 mono-wcf                x86_64        4.8.0-12.fc28        fedora        946 k
 mono-web                x86_64        4.8.0-12.fc28        fedora        2.5 M
 mono-winforms           x86_64        4.8.0-12.fc28        fedora        1.6 M
 monodoc                 x86_64        4.8.0-12.fc28        fedora         19 M

トランザクションの概要
================================================================================
インストール  11 パッケージ

ダウンロードサイズの合計: 53 M
インストール済みのサイズ: 129 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード中です:
(1/11): libgdiplus-5.6-2.fc29.x86_64.rpm         90 kB/s | 176 kB     00:01
(2/11): mono-data-sqlite-4.8.0-12.fc28.x86_64.r  67 kB/s | 103 kB     00:01
(3/11): mono-data-4.8.0-12.fc28.x86_64.rpm      602 kB/s | 4.4 MB     00:07
(4/11): mono-extras-4.8.0-12.fc28.x86_64.rpm     83 kB/s | 465 kB     00:05
(5/11): mono-devel-4.8.0-12.fc28.x86_64.rpm     478 kB/s | 5.3 MB     00:11
(6/11): mono-mvc-4.8.0-12.fc28.x86_64.rpm       125 kB/s | 484 kB     00:03
(7/11): mono-core-4.8.0-12.fc28.x86_64.rpm      1.1 MB/s |  18 MB     00:17
(8/11): mono-wcf-4.8.0-12.fc28.x86_64.rpm       230 kB/s | 946 kB     00:04
(9/11): mono-winforms-4.8.0-12.fc28.x86_64.rpm  523 kB/s | 1.6 MB     00:03
(10/11): mono-web-4.8.0-12.fc28.x86_64.rpm      573 kB/s | 2.5 MB     00:04
(11/11): monodoc-4.8.0-12.fc28.x86_64.rpm       1.7 MB/s |  19 MB     00:11
--------------------------------------------------------------------------------
合計                                            1.7 MB/s |  53 MB     00:31
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                        1/1
Installed: libgdiplus-5.6-2.fc29.x86_64
  インストール中   : libgdiplus-5.6-2.fc29.x86_64                          1/11
  scriptletの実行中: libgdiplus-5.6-2.fc29.x86_64                          1/11
Installed: libgdiplus-5.6-2.fc29.x86_64
Installed: mono-data-sqlite-4.8.0-12.fc28.x86_64
  インストール中   : mono-data-sqlite-4.8.0-12.fc28.x86_64                 2/11
Installed: mono-data-sqlite-4.8.0-12.fc28.x86_64
Installed: mono-extras-4.8.0-12.fc28.x86_64
  インストール中   : mono-extras-4.8.0-12.fc28.x86_64                      3/11
Installed: mono-extras-4.8.0-12.fc28.x86_64
Installed: mono-data-4.8.0-12.fc28.x86_64
  インストール中   : mono-data-4.8.0-12.fc28.x86_64                        4/11
Installed: mono-data-4.8.0-12.fc28.x86_64
Installed: mono-mvc-4.8.0-12.fc28.x86_64
  インストール中   : mono-mvc-4.8.0-12.fc28.x86_64                         5/11
Installed: mono-mvc-4.8.0-12.fc28.x86_64
Installed: mono-wcf-4.8.0-12.fc28.x86_64
  インストール中   : mono-wcf-4.8.0-12.fc28.x86_64                         6/11
Installed: mono-wcf-4.8.0-12.fc28.x86_64
Installed: mono-web-4.8.0-12.fc28.x86_64
  インストール中   : mono-web-4.8.0-12.fc28.x86_64                         7/11
Installed: mono-web-4.8.0-12.fc28.x86_64
Installed: mono-core-4.8.0-12.fc28.x86_64
  インストール中   : mono-core-4.8.0-12.fc28.x86_64                        8/11
Installed: mono-core-4.8.0-12.fc28.x86_64
Installed: mono-winforms-4.8.0-12.fc28.x86_64
  インストール中   : mono-winforms-4.8.0-12.fc28.x86_64                    9/11
Installed: mono-winforms-4.8.0-12.fc28.x86_64
Installed: monodoc-4.8.0-12.fc28.x86_64
  インストール中   : monodoc-4.8.0-12.fc28.x86_64                         10/11
Installed: monodoc-4.8.0-12.fc28.x86_64
Installed: mono-devel-4.8.0-12.fc28.x86_64
  インストール中   : mono-devel-4.8.0-12.fc28.x86_64                      11/11
  scriptletの実行中: mono-devel-4.8.0-12.fc28.x86_64                      11/11
Installed: mono-devel-4.8.0-12.fc28.x86_64
  検証             : libgdiplus-5.6-2.fc29.x86_64                          1/11
  検証             : mono-core-4.8.0-12.fc28.x86_64                        2/11
  検証             : mono-data-4.8.0-12.fc28.x86_64                        3/11
  検証             : mono-data-sqlite-4.8.0-12.fc28.x86_64                 4/11
  検証             : mono-devel-4.8.0-12.fc28.x86_64                       5/11
  検証             : mono-extras-4.8.0-12.fc28.x86_64                      6/11
  検証             : mono-mvc-4.8.0-12.fc28.x86_64                         7/11
  検証             : mono-wcf-4.8.0-12.fc28.x86_64                         8/11
  検証             : mono-web-4.8.0-12.fc28.x86_64                         9/11
  検証             : mono-winforms-4.8.0-12.fc28.x86_64                   10/11
  検証             : monodoc-4.8.0-12.fc28.x86_64                         11/11

インストール済み:
  mono-devel-4.8.0-12.fc28.x86_64          libgdiplus-5.6-2.fc29.x86_64
  mono-core-4.8.0-12.fc28.x86_64           mono-data-4.8.0-12.fc28.x86_64
  mono-data-sqlite-4.8.0-12.fc28.x86_64    mono-extras-4.8.0-12.fc28.x86_64
  mono-mvc-4.8.0-12.fc28.x86_64            mono-wcf-4.8.0-12.fc28.x86_64
  mono-web-4.8.0-12.fc28.x86_64            mono-winforms-4.8.0-12.fc28.x86_64
  monodoc-4.8.0-12.fc28.x86_64

完了しました!
$

MainClass.csで、

Console.WriteLineで文字列出力だけ。

mono - Mono Documentation
(HTTP500エラーでリンク切れてる?)
mono/Console.cs at master · mono/mono · GitHub

で、Mono製コンパイラmcsでコンパイルするのだが、

$ vi MainClass.cs
$ mcs MainClass.cs

Unhandled Exception:
System.TypeInitializationException: The type initializer for
'System.Console' threw an exception. --->
System.TypeInitializationException: The type initializer for
'System.ConsoleDriver' threw an exception. ---> System.Exception:
Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer,
System.Int32& position) [0x0002b] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoReader..ctor (System.String term, System.String
filename) [0x00065] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term)
[0x00000] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver..cctor () [0x00062] in
<373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding,
System.Text.Encoding outputEncoding) [0x0000a] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.Console..cctor () [0x000a8] in <373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00019] in
<6c6b2f38a076460eacf227054ff6f99a>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException:
The type initializer for 'System.Console' threw an exception. --->
System.TypeInitializationException: The type initializer for
'System.ConsoleDriver' threw an exception. ---> System.Exception:
Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer,
System.Int32& position) [0x0002b] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoReader..ctor (System.String term, System.String
filename) [0x00065] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term)
[0x00000] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver..cctor () [0x00062] in
<373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding,
System.Text.Encoding outputEncoding) [0x0000a] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.Console..cctor () [0x000a8] in <373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00019] in
<6c6b2f38a076460eacf227054ff6f99a>:0
$

意味不明のExceptionが出る。
mcs自体がおかしい模様。

Can't install Mono from Mono repo for Fedora 29 · Issue #11937 · mono/mono · GitHub

これかなあ・・・。
Fedora28のRPM入れてみろってある・・・。

Fedora 29 and Mono 5

ここにも、「compat-giflibが~」とある。
やってみる。

$ pwd
/home/bg1/project/local/fedora29vm/tmp
$ wget
https://rpmfind.net/linux/fedora/linux/releases/28/Everything/x86_64/os/Packages/c/compat-giflib-4.1.6-1.fc28.x86_64.rpm
--2019-06-15 11:44:54--
https://rpmfind.net/linux/fedora/linux/releases/28/Everything/x86_64/os/Packages/c/compat-giflib-4.1.6-1.fc28.x86_64.rpm
rpmfind.net (rpmfind.net) をDNSに問いあわせています... 195.220.108.108
rpmfind.net (rpmfind.net)|195.220.108.108|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 26540 (26K) [application/x-rpm]
`compat-giflib-4.1.6-1.fc28.x86_64.rpm' に保存中

compat-giflib-4.1.6 100%[===================>]  25.92K  93.3KB/s 時間 0.3s

2019-06-15 11:44:56 (93.3 KB/s) -
`compat-giflib-4.1.6-1.fc28.x86_64.rpm' へ保存完了 [26540/26540]

$ sudo dnf install compat-giflib-4.1.6-1.fc28.x86_64.rpm
[sudo] bg1 のパスワード:
Fedora Modular 29 - x86_64                      4.7 kB/s | 7.4 kB     00:01
Fedora Modular 29 - x86_64 - Updates            4.6 kB/s | 7.3 kB     00:01
Fedora 29 - x86_64 - Updates                    5.5 kB/s | 8.0 kB     00:01
Fedora 29 - x86_64                              5.2 kB/s | 7.5 kB     00:01
依存関係が解決しました。
================================================================================
 パッケージ          アーキテクチャー
                                  バージョン           リポジトリ         サイズ
================================================================================
インストール:
 compat-giflib       x86_64       4.1.6-1.fc28         @commandline        26 k

トランザクションの概要
================================================================================
インストール  1 パッケージ

合計サイズ: 26 k
インストール済みのサイズ: 41 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード中です:
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                        1/1
Installed: compat-giflib-4.1.6-1.fc28.x86_64
  インストール中   : compat-giflib-4.1.6-1.fc28.x86_64                      1/1
  scriptletの実行中: compat-giflib-4.1.6-1.fc28.x86_64                      1/1
Installed: compat-giflib-4.1.6-1.fc28.x86_64
  検証             : compat-giflib-4.1.6-1.fc28.x86_64                      1/1

インストール済み:
  compat-giflib-4.1.6-1.fc28.x86_64

完了しました!
$

これでコンパイル

$ mcs MainClass.cs

Unhandled Exception:
System.TypeInitializationException: The type initializer for
'System.Console' threw an exception. --->
System.TypeInitializationException: The type initializer for
'System.ConsoleDriver' threw an exception. ---> System.Exception:
Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer,
System.Int32& position) [0x0002b] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoReader..ctor (System.String term, System.String
filename) [0x00065] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term)
[0x00000] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver..cctor () [0x00062] in
<373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding,
System.Text.Encoding outputEncoding) [0x0000a] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.Console..cctor () [0x000a8] in <373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00019] in
<6c6b2f38a076460eacf227054ff6f99a>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException:
The type initializer for 'System.Console' threw an exception. --->
System.TypeInitializationException: The type initializer for
'System.ConsoleDriver' threw an exception. ---> System.Exception:
Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer,
System.Int32& position) [0x0002b] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoReader..ctor (System.String term, System.String
filename) [0x00065] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term)
[0x00000] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver..cctor () [0x00062] in
<373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding,
System.Text.Encoding outputEncoding) [0x0000a] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.Console..cctor () [0x000a8] in <373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00019] in
<6c6b2f38a076460eacf227054ff6f99a>:0
$

効果なし。
mono-completeとも書いてあったが、

$ sudo dnf install mono-complete
メタデータの期限切れの最終確認: xx:xx:xx 時間前の xxxx年xx月xx日 xx時xx分xx秒 に実施しました。
一致した引数がありません: mono-complete
エラー: 一致するものが見つかりません
$

そもそもFedoraにはmono-completeがない。
ググってもなかなか正解が見つからないので、Twitter検索してたら、

まさかの環境変数TERM・・・。

$ mcs

Unhandled Exception:
System.TypeInitializationException: The type initializer for
'System.Console' threw an exception. --->
System.TypeInitializationException: The type initializer for
'System.ConsoleDriver' threw an exception. ---> System.Exception:
Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer,
System.Int32& position) [0x0002b] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoReader..ctor (System.String term, System.String
filename) [0x00065] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term)
[0x00000] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver..cctor () [0x00062] in
<373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding,
System.Text.Encoding outputEncoding) [0x0000a] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.Console..cctor () [0x000a8] in <373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00019] in
<6c6b2f38a076460eacf227054ff6f99a>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException:
The type initializer for 'System.Console' threw an exception. --->
System.TypeInitializationException: The type initializer for
'System.ConsoleDriver' threw an exception. ---> System.Exception:
Magic number is wrong: 542
  at System.TermInfoReader.ReadHeader (System.Byte[] buffer,
System.Int32& position) [0x0002b] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoReader..ctor (System.String term, System.String
filename) [0x00065] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.TermInfoDriver..ctor (System.String term) [0x00058] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term)
[0x00000] in <373b6e083d6e45e498c9082a8eebd27f>:0
  at System.ConsoleDriver..cctor () [0x00062] in
<373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding,
System.Text.Encoding outputEncoding) [0x0000a] in
<373b6e083d6e45e498c9082a8eebd27f>:0
  at System.Console..cctor () [0x000a8] in <373b6e083d6e45e498c9082a8eebd27f>:0
   --- End of inner exception stack trace ---
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00019] in
<6c6b2f38a076460eacf227054ff6f99a>:0
$ export TERM=xterm
$ echo TERM
TERM
$ mcs
error CS2008: No files to compile were specified
Compilation failed: 1 error(s), 0 warnings
$

"export TERM=xterm"だけで、エラー内容が変わったので、うまくいったっぽい・・・。
あらためて、

$ pwd
/home/bg1/project/cloud/github.com/Sample/mono/Console/WriteLine/src/Console
$ vi MainClass.cs
$ ls
MainClass.cs
$ mcs MainClass.cs
$ ls
MainClass.cs  MainClass.exe
$ mono MainClass.exe
ABCDE
$

環境づくりでつまずいたが、ひとまず動いた。

Sample/mono/Console/WriteLine/src/Console at master · bg1bgst333/Sample · GitHub