Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Ubuntu 編譯安裝 libgdiplus 失敗?缺少 freetype 與 glib 依賴解決實錄

編輯:關於 Linux

libgdiplus 是 Mono/.NET 繪圖 API 的 Linux 實現,編譯安裝常因缺少 freetype 頭文件、glib-2.0 鏈接庫失敗。本文記錄 Ubuntu 14.04 64位下完整編譯排錯過程。

錯誤現象與排查

錯誤 1:缺少 freetype/tttables.h

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

錯誤 2:glib-2.0 鏈接失敗

/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 鏈接參數
  • 導致符號解析失敗

解決:修補 Makefile

# 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 雖繁瑣,但掌握依賴鏈修補技巧,可應對各類源碼編譯缺失頭文件/庫的通用問題。

Copyright © Windows教程網 All Rights Reserved