libgdiplus 是 Mono/.NET 繪圖 API 的 Linux 實現,編譯安裝常因缺少 freetype 頭文件、glib-2.0 鏈接庫失敗。本文記錄 Ubuntu 14.04 64位下完整編譯排錯過程。
gdiplus-private.h:33:31: fatal error: freetype/tttables.h: No such file or directory
#include
^
compilation terminated.
libfreetype6-dev 但頭文件在 /usr/include/freetype2/freetype//usr/include/freetype/ (無 2 目錄)sudo ln -s /usr/include/freetype2/freetype /usr/include/freetype
# 驗證
ls /usr/include/freetype/tttables.h
/usr/lib64/libglib-2.0.so.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [testgdi] Error 1
test/Makefile 缺少 -lglib-2.0 -lX11 鏈接參數# 1. 重新配置生成 Makefile
./configure --prefix=/usr
# 2. 編輯 test/Makefile
vim test/Makefile
# 找到第 130 行左右 LIBS 定義
# 修改前:
LIBS = -lpthread -lfontconfig
# 修改後:
LIBS = -lpthread -lfontconfig -lglib-2.0 -lX11
# 3. 重新編譯
make clean
make
sudo make install
# 1. 安裝構建依賴
sudo apt-get install build-essential libtool autoconf automake \
libfreetype6-dev libfontconfig1-dev libglib2.0-dev libx11-dev \
libpng-dev libjpeg-dev libtiff-dev libgif-dev libexif-dev
# 2. 下載源碼
wget https://github.com/mono/libgdiplus/archive/refs/tags/2.10.9.tar.gz
tar -xzf 2.10.9.tar.gz
cd libgdiplus-2.10.9
# 3. 配置
./configure --prefix=/usr
# 4. 應用上述兩個修補
sudo ln -s /usr/include/freetype2/freetype /usr/include/freetype
sed -i 's/LIBS = -lpthread -lfontconfig/LIBS = -lpthread -lfontconfig -lglib-2.0 -lX11/' test/Makefile
# 5. 編譯安裝
make -j$(nproc)
sudo make install
# 6. 更新動態庫緩存
sudo ldconfig
# 檢查庫文件
ls -l /usr/lib/libgdiplus*
# libgdiplus.so -> libgdiplus.so.0.0.0
# 測試 Mono 程序
csharp
# Mono C# Shell, type "help;" for help
> using System.Drawing;
> var bmp = new Bitmap(100, 100);
> bmp.Save("/tmp/test.png");
> quit
# 檢查 /tmp/test.png 是否生成
| 問題 | 解決 |
|---|---|
| configure 檢測不到 freetype | 設置 PKG_CONFIG_PATH 或創建符號鏈接 |
| 編譯警告: deprecated API | 正常,不影響功能,可忽略 |
| make install 權限不足 | 使用 sudo 或配置 --prefix=$HOME/.local |
| Mono 運行時找不到庫 | sudo ldconfig 刷新緩存 |
| 版本衝突 (系統自帶舊版) | sudo apt remove libgdiplus 先移除 |
# Ubuntu 16.04+
sudo apt install libgdiplus
# 查看版本
dpkg -l | grep libgdiplus
編譯安裝 libgdiplus 雖繁瑣,但掌握依賴鏈修補技巧,可應對各類源碼編譯缺失頭文件/庫的通用問題。