

FAQ [1] によると、Mono はスペイン語で猿の意味で、We like monkeys とあります。ロゴはゴリラをデザインしたもののようです。
Mono を利用すると、クロスプラットフォーム間(ここでは Linux と Windows)で簡単に同一の GUI アプリケーションを動作させることができそうです。
自宅では Linux、会社では Windows(しかもいまだに Windows XP)を使っているので、手軽に双方で動作するアプリケーションを Linux で開発するのであれば、Mono はまさにピッタリの開発環境です。MinGW クロスコンパイル環境よりもお手軽な感すらあります。
そこで、(C# 使いの Windows ユーザの方にとっては「今更」な感があるでしょうが)Windows 上で動作させることを意識した Mono の GUI プログラミングについて、調べた結果をまとめていきたいと思います。
最初は Hello World
まずは、おきまりの Hello World プログラムで動作確認です。当面は C# を使います。以下のソースを hello.cs として適当な場所に保存しておきます。
using System;
using System.Windows.Forms;
class HelloWorldApp {
public static void Main() {
MessageBox.Show("こんにちは、世界!", "mono");
}
}
mono のインストール
乱暴な方法ですが、私は以下のように mono- で始まるパッケージ(と関連するパッケージ)を全部インストールしてしまっています。使用している Linux は Fedora 13、mono のバージョンは 2.6.4 です。
$ su
パスワード:
# yum install mono-*
読み込んだプラグイン:presto, refresh-packagekit
...
(以下省略)
C# コンパイラとビルド
Mono 2.6.4 で利用できる C# コンパイラは以下の 4 種類です。[2]
mcs: compiler to target 1.1 runtime (to be deprecated with Mono 2.8).
gmcs: compiler to target the 2.0 runtime.
smcs: compiler to target the 2.1 runtime, to build Moonlight applications.
dmcs: the C# 4.0 compiler, and references the 4.0 runtime.
ここでは gmcs を使い、次のように hello.cs をビルド・実行します。
$ gmcs -pkg:dotnet -target:winexe hello.cs
$ mono hello.exe

コンパイルの際、-target:winexe を省略すると、Windows 上で実行した時に、コマンドプロンプトのウィンドウも表示されます。
同じく Linux 上の Wine で実行した結果を以下に示しました。
$ wine hello.exe

Wine には .Net Framework 2.0 をインストールしています。

Windows XP へ hello.exe をコピーして実行した結果は以下の様になります。

ちなみに XP では .Net Framework 3.5 までインストールされています。そのため、今のところ gmcs を使っていれば十分のようです。

参考サイト
[1] FAQ: General - Mono
[2] CSharp Compiler - Mono
【注記】情報が古くなりましたので、下記で内容を更新しました。
0 件のコメント:
コメントを投稿