You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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:

  1. Go to Tools > Build System > New Build System...
  2. 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, and ode_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
}
  1. Save this file (e.g., Fortran_Module_Build.sublime-build) in the directory Sublime suggests (usually Packages/User).
  2. Select your new build system from Tools > Build System and press Ctrl+B to compile and run.

What this does:

  • The -c flag compiles each module into a .o object file without linking.
  • The second gfortran command compiles the main program and links all .o files 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:

  1. Create a file named Makefile in 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
  1. 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"]
        }
    ]
}
  1. Save it as Fortran_Makefile_Build.sublime-build, select it from the Build System menu, and press Ctrl+B. You can also run make clean via Tools > Build With... to delete build artifacts.

Key Notes:

  • Module Compile Order: If one module depends on another (e.g., ode_solver uses derivatives), list the dependency first in the MODULES list (or Makefile) to ensure it compiles before the module that needs it.
  • .mod Files: When you compile modules, Fortran generates .mod files (interface definitions). These are required to compile the main program, so don't delete them until you clean your build.

内容的提问来源于stack exchange,提问作者vikash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:07:39