博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
测试 Mono 安装
阅读量:4080 次
发布时间:2019-05-25

本文共 1801 字,大约阅读时间需要 6 分钟。

测试 Mono 安装

为了测试核心编译器(mcs)和运行时(mono),应该创建一个简单的程序并编译它。可以在喜欢的任何文本编辑器中创建程序。这里采用一种快速而简陋的方法创建该文件(虽然没有任何格式化),从终端提示符中运行下列命令(都在一行中):

$ echo 'class X { static void Main () { System.Console.Write("My first          mono app worked!\n");} }' > example.cs

该命令创建一个名为 example.cs 的 C# 源文件(也可从下面的 部分下载该文件 example.cs 和可执行文件 example.exe。(注意,如果使用 Linux 可直接使用 bash 提示符,如果使用 Windows 则需要从开始菜单中调用 Mono 命令提示符。)

要测试编译器创建可执行文件的能力,可输入下面的命令:

$ mcs example.cs

这样将生成名为 example.exe 的二进制文件。要运行它来测试运行时,可使用该命令:

$ mono example.exe

如果一切正常,就会在控制台中看到“My first mono app worked!”字样。

图 1. 正常运行的结果

 

事实上,可以将得到的可执行文件复制到其他系统上,比如运行 Windows 的系统上,无需修改就可以执行。

 

使用非 Mono 库的代码

使用 Mono 平台更有说服力的原因是能够使用已有的、可能不属于 C# 库的 C# 代码。下面是这样的一个例子(也可以从下面的 部分下载清单 2 PInvokeExample.cs 和可执行文件 PInvokeExample.exe)。

提供这种能力的机制是 Platform Invocation Facility(缩写为 pinvoke)。

清单 2. 使用 Platform Invocation Facility
/* Platform Invocation Facility Example   This code is intended to illustrate P/Invoke.   For out purposes we will access a c shared library.*/using System;// This is a lot of the magic!using System.Runtime.InteropServices;/* Class illustrates pinvoking existing c library code */public class PInvokeExample{   /* Notifying the runtime that we need an          additional mathematics library, libm.so */   [DllImport("libm")]   /* Here we define the external function we will           be using from the library,           You must declare these methods to be static.      The function signature you provide must match the           signature in the external library!   */        static extern double sqrt ( double element );   public static void Main ()   {      Console.WriteLine("The square root of 100.0 is {0}.", sqrt(100.0));   }}

从上述简化的代码中可以看出,只需要告诉 Mono 编译器使用什么库(在 DLLImport 一行中完成)并提供要使用的函数的原型。如果在 Linux 系统上编译这个类,控制台将显示正确的结果。

posted on 2014-10-09 13:04 阅读(...) 评论(...)

转载地址:http://jfvni.baihongyu.com/

你可能感兴趣的文章
星环后台研发实习面经
查看>>
大数相乘不能用自带大数类型
查看>>
字节跳动后端开发一面
查看>>
CentOS Tensorflow 基础环境配置
查看>>
centOS7安装FTP
查看>>
FTP的命令
查看>>
CentOS操作系统下安装yum的方法
查看>>
ping 报name or service not known
查看>>
FTP 常见问题
查看>>
zookeeper单机集群安装
查看>>
do_generic_file_read()函数
查看>>
Python学习笔记之数据类型
查看>>
Python学习笔记之特点
查看>>
shell 快捷键
查看>>
VIM滚屏操作
查看>>
EMC 2014存储布局及十大新技术要点
查看>>
linux内核内存管理(zone_dma zone_normal zone_highmem)
查看>>
将file文件内容转成字符串
查看>>
循环队列---数据结构和算法
查看>>
优先级队列-数据结构和算法
查看>>