Ubuntu上Fortran程序进行图形界面开发的方法
在Ubuntu上开发Fortran图形界面程序前,需先安装以下核心工具:
gfortran
(GNU Fortran Compiler),它是Ubuntu下最流行的Fortran编译器,支持现代Fortran标准(如Fortran 2003及以上)。通过以下命令安装:sudo apt update
sudo apt install gfortran
build-essential
(包含GCC、Make等基础编译工具),用于编译和链接程序:sudo apt install build-essential
GTK+是Ubuntu桌面环境的原生GUI工具包,支持跨平台开发,且有多个Fortran绑定库(如gtk-fortran
)简化开发流程。
sudo apt install libgtk-3-dev # GTK+ 3开发库
sudo apt install gtk-fortran # Fortran绑定库(封装GTK+的Fortran接口)
gtk-fortran
提供的模块(如gtk
、gtk_main
)创建窗口、按钮等控件。示例如下(main.f90
):program simple_gui
use gtk, only: gtk_init, gtk_window_new, gtk_window_set_title, &
gtk_window_set_default_size, gtk_widget_show_all, &
gtk_main, gtk_main_quit
use gtk_sup, only: gtk_container_add, gtk_button_new_with_label
implicit none
type(c_ptr) :: window, button
! 初始化GTK
call gtk_init()
! 创建主窗口
window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
call gtk_window_set_title(GTK_WINDOW(window), "Fortran GTK Demo")
call gtk_window_set_default_size(GTK_WINDOW(window), 300, 200)
! 创建按钮
button = gtk_button_new_with_label("Click Me!")
call gtk_container_add(GTK_CONTAINER(window), button)
! 连接按钮点击事件(退出程序)
call g_signal_connect(button, "clicked", c_funloc(gtk_main_quit), c_null_ptr)
! 显示所有控件
call gtk_widget_show_all(window)
! 进入GTK主循环
call gtk_main()
end program simple_gui
(2)编译代码:使用gfortran
链接GTK+库和gtk-fortran
模块。gtk-fortran
会自动处理GTK+的C接口绑定,无需手动编写C包装器:gfortran -o simple_gui main.f90 `pkg-config --cflags --libs gtk+-3.0` -lgtk-fortran
(3)运行程序:./simple_gui
执行后会弹出一个包含“Click Me!”按钮的窗口,点击按钮可关闭程序。Qt是另一款流行的跨平台GUI框架,支持C++,可通过Fortran与C++的混合编程(iso_c_binding
)实现集成。
sudo apt install qt5-default # Qt 5开发环境
sudo apt install g++ # C++编译器(Qt需要)
extern "C"
导出函数(避免名称修饰):// qt_wrapper.cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
extern "C" {
void create_qt_window() {
QApplication app(0, nullptr);
QWidget window;
window.setWindowTitle("Fortran Qt Demo");
window.resize(300, 200);
QPushButton button("Click Me!", &window);
QObject::connect(&button, &QPushButton::clicked, [&app]() { app.quit(); });
window.show();
app.exec();
}
}
(2)编写Fortran代码:使用iso_c_binding
调用C++包装器函数:! main.f90
program qt_gui
use iso_c_binding
implicit none
interface
subroutine create_qt_window() bind(c)
end subroutine create_qt_window
end interface
call create_qt_window()
end program qt_gui
(3)编译代码:先编译C++包装器为目标文件,再用gfortran
链接Qt库和Fortran代码:g++ -c qt_wrapper.cpp -o qt_wrapper.o `pkg-config --cflags --libs qt5core qt5gui qt5widgets`
gfortran -o qt_gui main.f90 qt_wrapper.o `pkg-config --cflags --libs qt5core qt5gui qt5widgets`
(4)运行程序:./qt_gui
效果与GTK+示例类似,弹出一个包含按钮的窗口。FLTK(Fast Light Toolkit)是跨平台的C++ GUI库,以轻量、快速著称,提供Fortran绑定(fltk-fortran
)。
sudo apt install libfltk1.3-dev # FLTK开发库
sudo apt install fltk-fortran # Fortran绑定库
fltk-fortran
的示例代码),编写Fortran代码创建窗口和控件,使用gfortran
链接FLTK库编译:gfortran -o fltk_demo main.f90 -lfltk -lfltk_fortran
运行生成的可执行文件即可显示图形界面。IUP(Portable User Interface)是轻量级跨平台GUI工具包,支持Fortran绑定,适合快速开发简单界面。
sudo apt install libiup-dev # IUP开发库
iup.f90
)创建窗口和控件,编译时链接IUP库:gfortran -o iup_demo main.f90 -liup
运行程序即可显示界面。wxWidgets是跨平台的C++ GUI库,支持原生外观,提供Fortran绑定(wxWidgets-fortran
)。
sudo apt install libwxgtk3.0-dev # wxWidgets开发库
sudo apt install libwxgtk3.0-fortran-dev # Fortran绑定库
若不想直接处理Fortran与C++的混合编程,可通过Python的PyQt
库创建GUI,再调用Fortran编写的动态链接库(.so
文件)实现计算功能。
sudo apt install python3 python3-pyqt5
.so
文件):! libfortran.f90
subroutine add_numbers(a, b, result) bind(c)
use iso_c_binding
real(c_double), intent(in) :: a, b
real(c_double), intent(out) :: result
result = a + b
end subroutine add_numbers
编译命令:gfortran -shared -fPIC -o libfortran.so libfortran.f90
(2)编写Python代码(使用PyQt5
创建GUI,调用libfortran.so
):# gui.py
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel
import ctypes
# 加载Fortran动态库
lib = ctypes.CDLL('./libfortran.so')
lib.add_numbers.argtypes = [ctypes.c_double, ctypes.c_double]
lib.add_numbers.restype = ctypes.c_double
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
self.label = QLabel("Result: ")
self.button = QPushButton("Add 3 + 5")
self.button.clicked.connect(self.on_click)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.setLayout(layout)
self.setWindowTitle("Fortran via Python")
def on_click(self):
result = lib.add_numbers(3.0, 5.0)
self.label.setText(f"Result: {result}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
(3)运行Python脚本:python3 gui.py
点击按钮会调用Fortran编写的add_numbers
函数,显示结果。iso_c_binding
模块的使用(如bind(c)
声明、数据类型转换),确保Fortran与C/C++代码的兼容性。gtk-fortran
支持GTK+ 3,需对应安装libgtk-3-dev
)。gtk-fortran
的GitHub页面、Qt的Fortran绑定指南),以及Ubuntu社区论坛的示例代码,解决开发中的具体问题。