当前位置: 首页 > 编程文章 >

Ubuntu 13.10 编译安装 Mono 环境及解决 WinForm 依赖问题

Ubuntu 13.10 编译安装 Mono 环境及解决 WinForm 依赖问题

作者:苏宁时间:2026-04-08 13:57:18浏览:252

在 Ubuntu 13.10 环境中使用 Mono 运行 Windows Forms 应用时,常因缺少图形库支持而报错。本文记录了从下载 libgdiplus 源码到解决 libglib2.0-dev、libpng-dev、libx11-dev 等依赖包缺失的过程,以及处理 configure 和 make 阶段特定错误的详细步骤,最终实现 Mono 环境的完整配置与应用验证。

本文目录展开

准备工作与初步运行测试

准备工作

首先在 Visual Studio 2012 中构建一个 WinForm 应用程序,相关代码片段如下:


using System;
using System.Windows.Forms;
namespace FormsTest
{
static class Program
{
/// 
/// 应用程序的主入口点。
/// 
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form());
}
}
}

随后尝试在 Ubuntu 系统中利用 Mono 执行该程序:


nike@NIKE-PC:~$ cd Desktop/
nike@NIKE-PC:~/Desktop$ ls
FormsTest.exe
nike@NIKE-PC:~/Desktop$ mono FormsTest.exe
Unhandled Exception:
System.TypeInitializationException: An exception was thrown by the type initializer for System.Windows.Forms.XplatUI ---> System.TypeInitializationException: An exception was thrown by the type initializer for System.Drawing.GDIPlus ---> System.DllNotFoundException: /opt/mono-3.2.8/lib/libgdiplus.so
at (wrapper managed-to-native) System.Drawing.GDIPlus:GdiplusStartup (ulong&,System.Drawing.GdiplusStartupInput&,System.Drawing.GdiplusStartupOutput&)
at System.Drawing.GDIPlus..cctor () [0x00000] in :0
--- End of inner exception stack trace ---
at System.Drawing.Graphics.FromHdcInternal (IntPtr hdc) [0x00000] in :0
at System.Windows.Forms.XplatUIX11.SetDisplay (IntPtr display_handle) [0x00000] in :0
at System.Windows.Forms.XplatUIX11..ctor () [0x00000] in :0
at System.Windows.Forms.XplatUIX11.GetInstance () [0x00000] in :0
at System.Windows.Forms.XplatUI..cctor () [0x00000] in :0
--- End of inner exception stack trace ---
at System.Windows.Forms.Application.EnableVisualStyles () [0x00000] in :0
at FormsTest.Program.Main () [0x00000] in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for System.Windows.Forms.XplatUI ---> System.TypeInitializationException: An exception was thrown by the type initializer for System.Drawing.GDIPlus ---> System.DllNotFoundException: /opt/mono-3.2.8/lib/libgdiplus.so
at (wrapper managed-to-native) System.Drawing.GDIPlus:GdiplusStartup (ulong&,System.Drawing.GdiplusStartupInput&,System.Drawing.GdiplusStartupOutput&)
at System.Drawing.GDIPlus..cctor () [0x00000] in :0
--- End of inner exception stack trace ---
at System.Drawing.Graphics.FromHdcInternal (IntPtr hdc) [0x00000] in :0
at System.Windows.Forms.XplatUIX11.SetDisplay (IntPtr display_handle) [0x00000] in :0
at System.Windows.Forms.XplatUIX11..ctor () [0x00000] in :0
at System.Windows.Forms.XplatUIX11.GetInstance () [0x00000] in :0
at System.Windows.Forms.XplatUI..cctor () [0x00000] in :0
--- End of inner exception stack trace ---
at System.Windows.Forms.Application.EnableVisualStyles () [0x00000] in :0
at FormsTest.Program.Main () [0x00000] in :0

执行过程中出现异常 System.DllNotFoundException: /opt/mono-3.2.8/lib/libgdiplus.so。这表明若需在 Ubuntu 上成功运行 WinForm 应用,必须预先安装 libgdiplus 库。

编译安装 libgdiplus 及依赖处理

获取 libgdiplus 源码包并进行编译操作。


nike@NIKE-PC:~$ cd src
nike@NIKE-PC:~/src$ wget http://download.mono-project.com/sources/libgdiplus/libgdiplus-2.10.9.tar.bz2
--2014-03-27 22:46:40-- http://download.mono-project.com/sources/libgdiplus/libgdiplus-2.10.9.tar.bz2
Resolving download.mono-project.com (download.mono-project.com)... 54.230.157.116, 54.230.157.204, 54.230.158.216, ...
Connecting to download.mono-project.com (download.mono-project.com)|54.230.157.116|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2074317 (2.0M) [application/x-bzip2]
Saving to: ‘libgdiplus-2.10.9.tar.bz2'
 
100%[======================================>] 2,074,317 224KB/s in 9.5s
 
2014-03-27 22:46:54 (213 KB/s) - ‘libgdiplus-2.10.9.tar.bz2' saved [2074317/2074317]
 
nike@NIKE-PC:~/src$ ls
libgdiplus-2.10.9.tar.bz2 mono-3.2.8 mono-3.2.8.tar.bz2
nike@NIKE-PC:~/src$ tar -xvjf libgdiplus-2.10.9.tar.bz2
nike@NIKE-PC:~/src$ cd libgdiplus-2.10.9/
nike@NIKE-PC:~/src/libgdiplus-2.10.9$ ./configure --prefix=/opt/mono-3.2.8
==============>省略了很多<==============
checking pkg-config is at least version 0.9.0... yes
checking for BASE_DEPENDENCIES... no
configure: error: Package requirements (glib-2.0 >= 2.2.3) were not met:
No package 'glib-2.0' found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
 
Alternatively, you may set the environment variables BASE_DEPENDENCIES_CFLAGS
and BASE_DEPENDENCIES_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

编译配置阶段报错,提示需要安装 libglib2.0-dev 开发包:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ sudo apt-get install libglib2.0-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libglib2.0-0 libglib2.0-bin libpcre3-dev libpcrecpp0 zlib1g-dev
Suggested packages:
libglib2.0-doc
The following NEW packages will be installed:
libglib2.0-dev libpcre3-dev libpcrecpp0 zlib1g-dev
The following packages will be upgraded:
libglib2.0-0 libglib2.0-bin
2 upgraded, 4 newly installed, 0 to remove and 273 not upgraded.
Need to get 2,728 kB of archives.
After this operation, 9,208 kB of additional disk space will be used.
Do you want to continue [Y/n]?
==============>省略了很多<==============

安装依赖后,重新初始化 libgdiplus 的安装配置文件:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ ./configure --prefix=/opt/mono-3.2.8
==============>省略了很多<==============
checking for libpng14... checking for libpng12... no
checking for png_read_info in -lpng... no
configure: error: *** libpng12 not found. See http://www.libpng.org/pub/png/libpng.html.

再次遭遇错误,系统提示需安装 libpng-dev:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ sudo apt-get install libpng-dev

补充 libpng-dev 后,重新执行 libgdiplus 的配置脚本:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ ./configure --prefix=/opt/mono-3.2.8
==============>省略了很多<==============
./configure: line 13371: test: too many arguments
configure: error: "Failed to compile with X11/Xlib.h include. You must fix your compiler paths"

继续报错,指出缺少 libx11-dev 组件:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ sudo apt-get install libx11-dev

安装 libx11-dev 后,再次重新初始化 libgdiplus 安装配置文件:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ ./configure --prefix=/opt/mono-3.2.8
==============>省略了很多<==============
configure: error: Cairo requires at least one font backend.
Please install freetype and fontconfig, then try again:
http://freetype.org/ http://fontconfig.org/

configure: error: ./configure failed for cairo

此外还需安装 freetype 和 fontconfig 相关库:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ sudo apt-get install libfreetype6-dev
nike@NIKE-PC:~/src/libgdiplus-2.10.9$ sudo apt-get install fontconfig
nike@NIKE-PC:~/src/libgdiplus-2.10.9$ sudo apt-get install libfontconfig1-dev

完成上述依赖安装后,重新初始化 libgdiplus 安装配置文件:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ ./configure --prefix=/opt/mono-3.2.8
==============>省略了很多<==============
Configuration summary
 
* Installation prefix = /opt/mono-3.2.8
* Cairo = 1.6.4 (internal)
* Text = cairo
* EXIF tags = No. Get it from http://libexif.sourceforge.net/
* Codecs supported:
 
- TIFF: no (Get it from http://www.libtiff.org/)
- JPEG: no (Get it from http://freshmeat.net/projects/libjpeg)
- GIF: no (See http://sourceforge.net/projects/libgif)
- PNG: yes
 
NOTE: if any of the above say 'no' you may install the
corresponding development packages for them, rerun
autogen.sh to include them in the build.

nike@NIKE-PC:

此时 ./configure 命令顺利通过检查。

解决编译错误与完成安装配置

开始对源代码进行编译与安装。


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ make
make all-recursive
make[1]: Entering directory `/home/nike/src/libgdiplus-2.10.9'
Making all in pixman
make[2]: Entering directory `/home/nike/src/libgdiplus-2.10.9/pixman'
make all-recursive
make[3]: Entering directory `/home/nike/src/libgdiplus-2.10.9/pixman'
Making all in pixman
make[4]: Entering directory `/home/nike/src/libgdiplus-2.10.9/pixman/pixman'
/bin/bash ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -Wall -fvisibility=hidden -MT pixman-access.lo -MD -MP -MF .deps/pixman-access.Tpo -c -o pixman-access.lo pixman-access.c
../libtool: line 852: X--tag=CC: command not found

编译期间遇到错误,经排查发现项目目录下生成的 libtool 脚本定义了 $ECHO 变量,但底层脚本 ltmain.sh 中使用的是 $echo(因 libtool 版本过旧导致)。解决方法为执行 export echo=echo 即可规避此问题:


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ export echo=echo
nike@NIKE-PC:~/src/libgdiplus-2.10.9$ make
==============>省略了很多<==============
make[2]: Entering directory `/home/nike/src/libgdiplus-2.10.9/tests'
/bin/bash ../libtool --tag=CC --mode=link gcc -g -O2 -pthread -o testgdi testgdi.o ../src/libgdiplus.la -lpthread -lfontconfig
libtool: link: gcc -g -O2 -pthread -o .libs/testgdi testgdi.o ../src/.libs/libgdiplus.so -lpthread -lfontconfig -pthread -Wl,-rpath -Wl,/opt/mono-3.2.8/lib
/usr/bin/ld: testgdi.o: undefined reference to symbol 'g_print'
/lib/i386-linux-gnu/libglib-2.0.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [testgdi] Error 1
make[2]: Leaving directory `/home/nike/src/libgdiplus-2.10.9/tests'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/nike/src/libgdiplus-2.10.9'
make: *** [all] Error 2

编译时又出错了,解决办法为: 1.运行./configure后,编辑tests/Makefile文件 2.在Makefile文件130行位置,将LIBS = -lpthread -lfontconfig改为LIBS = -lpthread -lfontconfig -lglib-2.0 -lX11 3.再次运行make即可


nike@NIKE-PC:~/src/libgdiplus-2.10.9$ vim tests/Makefile
nike@NIKE-PC:~/src/libgdiplus-2.10.9$ make
==============>编译很快<==============
nike@NIKE-PC:~/src/libgdiplus-2.10.9$ sudo make install
==============>省略了很多<==============

至此,软件安装完毕。

配置 PATH 环境变量。


nike@NIKE-PC:~/src/mono-3.2.8$ cd ~
nike@NIKE-PC:~$ vim .bashrc

在配置文件末尾添加以下语句以设置动态链接库路径:export LD_LIBRARY_PATH=/opt/mono-3.2.8/lib。随后执行:


nike@NIKE-PC:~$ . .bashrc
nike@NIKE-PC:~$ echo $LD_LIBRARY_PATH
/opt/mono-3.2.8/lib
nike@NIKE-PC:~$

检验 libgdiplus 是否安装成功。


nike@NIKE-PC:~$ cd Desktop/
nike@NIKE-PC:~/Desktop$ mono FormsTest.exe

程序运行正常,无报错信息。

上一篇
Web服务器集群Session同步与共享的三种解决方案

在Web集群环境中,负载均衡会导致同一用户请求分发至不同服务器,若Session不同步将引发登录状态异常。本文介绍三种解决思路:利用独立数据库或集群节点存储Session,通过Cookie中转实现客户端同步,以及使用Memcache构建分布式内存池。其中,数据库方案增加负载,Cookie方案存在安全与禁用风险,而Memcache凭借高性能和高安全性成为推荐方案。

下一篇
Ubuntu 20.04下CLion 2026.1.3安装与ROS环境配置详解

本文介绍在Ubuntu 20.04系统中配置CLion 2026.1.3以支持ROS开发的完整流程。主要步骤包括将CLion加入系统PATH环境变量、修改.desktop文件实现启动时自动source ROS环境、设置本地CMake工具链,以及在CMake选项中指定ROS工作空间的devel和build路径。通过优化启动方式,避免每次手动执行setup.bash的繁琐操作,实现高效编译运行。

相关文章

更多
Windows下Nginx安装配置与Mono部署指南

本文介绍在Windows环境下部署Nginx服务器的完整流程,涵盖从官网下载、解压启动到核心配置文件nginx.conf的详细解析。内容包含常用命令操作、进程管理及以Windows服务形式运行的方法。此外,还演示了如何结合Mono和FastCGI技术,在Nginx上托管ASP.NET应用程序,实现脱离IIS的轻量级Web服务方案。

2025.11.16 818
CentOS 配置 Nginx 与 Mono 运行 ASP.NET 环境指南

本文介绍在 CentOS 系统中通过源码编译安装 Nginx、Mono 及 libgdiplus,并配置 FastCGI 以支持 ASP.NET 应用的完整流程。主要步骤包括使用 yum 安装依赖库、下载并编译 Nginx 和 Mono 组件、修改 nginx.conf 启用 fastcgi_pass 指令,以及启动 fastcgi-mono-server2 服务。最终通过上传 Web 项目至指定目录验证环境是否正常运行。

2025.06.01 472

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。