如何在Sublime Text 3中运行含模块的gfortran代码?
Fixing Undefined Reference Errors for Fortran Modules in Sublime Text 3
Those undefined reference errors are happening because Sublime's default Fortran build system only compiles your main program file (problem_main.f95) — it's not including the module files that contain the subroutines/functions your main code calls. Here's how to fix this:
Option 1: Create a Custom Build System for Your Project
Sublime lets you define custom build commands that handle multi-file Fortran projects. Here's how to set it up:
- Go to Tools > Build System > New Build System...
- Replace the default JSON with a configuration that compiles your modules first, then links them with the main program. For example, if your modules are
universal_parameter_declaration.f95,array_creator.f95,derivatives.f95, andode_solver.f95, use this:
{ "cmd": ["bash", "-c", "gfortran -c universal_parameter_declaration.f95 array_creator.f95 derivatives.f95 ode_solver.f95 && gfortran -o problem_main problem_main.f95 *.o && ./problem_main"], "file_regex": "^(.*?)(\\((\\d+),(\\d+)\\))?:\\s*(.*)$", "working_dir": "${file_path}", "selector": "source.fortran", "shell": true }
- Save this file (e.g.,
Fortran_Module_Build.sublime-build) in the directory Sublime suggests (usuallyPackages/User). - Select your new build system from Tools > Build System and press
Ctrl+Bto compile and run.
What this does:
- The
-cflag compiles each module into a.oobject file without linking. - The second
gfortrancommand compiles the main program and links all.ofiles together to create the executable. - Finally, it runs the executable automatically.
Option 2: Use a Makefile (Better for Larger Projects)
If you have more modules or complex dependencies, a Makefile is more maintainable. Here's how:
- Create a file named
Makefilein your project directory with this content:
# Compiler and flags FC = gfortran FCFLAGS = -Wall -Wextra # List all module files (order matters: compile dependencies first!) MODULES = universal_parameter_declaration.f95 array_creator.f95 derivatives.f95 ode_solver.f95 # Convert module filenames to object files MODULE_OBJS = $(MODULES:.f95=.o) # Main program object file MAIN_OBJ = problem_main.o # All object files ALL_OBJS = $(MODULE_OBJS) $(MAIN_OBJ) # Default target: build the executable all: problem_main # Link all object files to create the executable problem_main: $(ALL_OBJS) $(FC) $(FCFLAGS) -o $@ $^ # Compile .f95 files to .o objects %.o: %.f95 $(FC) $(FCFLAGS) -c $< # Clean up build files clean: rm -f *.o *.mod problem_main
- Create a custom Sublime Build System to run
make:- Go to Tools > Build System > New Build System...
- Paste this JSON:
{ "cmd": ["make"], "file_regex": "^(.*?)(\\((\\d+),(\\d+)\\))?:\\s*(.*)$", "working_dir": "${file_path}", "selector": "source.fortran", "variants": [ { "name": "Clean", "cmd": ["make", "clean"] } ] }
- Save it as
Fortran_Makefile_Build.sublime-build, select it from the Build System menu, and pressCtrl+B. You can also runmake cleanvia Tools > Build With... to delete build artifacts.
Key Notes:
- Module Compile Order: If one module depends on another (e.g.,
ode_solverusesderivatives), list the dependency first in theMODULESlist (or Makefile) to ensure it compiles before the module that needs it. - .mod Files: When you compile modules, Fortran generates
.modfiles (interface definitions). These are required to compile the main program, so don't delete them until you clean your build.
内容的提问来源于stack exchange,提问作者vikash
相关产品推荐
相关产品推荐

