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 10.10 安裝 GNU Fortran 77 (g77) 編譯器:舊版相容性需求下的 apt 源擴展與 aptitude 解決相依

編輯:關於 Linux

Ubuntu 10.10 起預設僅提供 gfortran (Fortran 90/95/2003),舊版 Fortran 77 程式碼可能因語法、內建函數、連結差異無法編譯。需從舊版 Ubuntu (Hardy 8.04) 倉庫引入 g77。本文記錄源擴展、aptitude 解決相依、驗證編譯的完整流程。

g77 安裝示意圖

前置條件:切換至 root

sudo bash

1. 擴展軟體源 (加入 Hardy 8.04 Universe)

cat >> /etc/apt/sources.list <<'EOF'
deb https://hu.archive.ubuntu.com/ubuntu/ hardy universe
deb-src https://hu.archive.ubuntu.com/ubuntu/ hardy universe
deb https://hu.archive.ubuntu.com/ubuntu/ hardy-updates universe
deb-src https://hu.archive.ubuntu.com/ubuntu/ hardy-updates universe
EOF

2. 安裝 aptitude (更強的相依解決器)

apt-get update
apt-get install aptitude

3. 更新索引並安裝 g77

aptitude update
aptitude install g77

為何用 aptitude? 跨版本安裝會產生複雜相依衝突 (如 gcc-3.4, libg2c0 等),aptitude 的解決器比 apt-get 更能自動尋找可行解 (降級/保留/安裝特定版本)。

4. 驗證編譯

cat > test.f <<'EOF'
      PROGRAM HELLO
      PRINT *, 'Hello from g77!'
      END
EOF

g77 test.f -o test
./test
# 輸出: Hello from g77!

常見問題與對策

問題原因解決
相依衝突導致 aptitude 提出移除大量套件 Hardy 套件版本過舊,與 Maverick 核心庫不相容 選擇 n 拒絕,改用 gfortran -std=legacy 編譯 F77 碼
g77: command not found 安裝未成功或路徑未生效 which g77、dpkg -l | grep g77 確認
連結錯誤 libg2c.so.0 not found 缺少 Fortran 77 執行期庫 aptitude install libg2c0 (從 Hardy 源)

現代替代方案 (強烈建議)

# gfortran 支援 Fortran 77 相容模式
gfortran -std=legacy -ffixed-line-length-none test.f -o test
# -std=legacy: 啟用 F77 延伸語法 (如 DO 迴圈共用標號、Hollerith 常數)
# -ffixed-line-length-none: 取消 72 欄固定格式限制
  • 避免跨版本源汙染系統
  • 獲得現代最佳化、診斷、OpenMP/Coarray 支援
  • 絕大多數 F77 程式碼無需修改即可編譯通過

僅在極少數依賴 g77 特有擴展 (如 AUTOMATIC、VIRTUAL、特定 I/O 格式) 的老舊專案中,才建議採用本文跨版本安裝方案;其餘情況諷以 gfortran -std=legacy 為首選。

Copyright © Windows教程網 All Rights Reserved