Fortran报错“Error: Two main PROGRAMs at (1) and (2)”调试求助
Hey there, let's get this sorted out right away: passing arrays as function arguments isn't causing this error—this is strictly a problem with having multiple entry points in your codebase. Here's how to track down and fix the issue:
Audit all source files for
PROGRAMdeclarations
Go through every.f90/.ffile in your project and look for lines starting withPROGRAM <program_name>. It's super easy to accidentally leave a strayPROGRAMblock in a module or subroutine file—maybe you copied code from a test script or another project and forgot to strip out the main program declaration.Check included files and module dependencies
If you're usingINCLUDEstatements orUSEto pull in modules, double-check those included/module files. Sometimes a module might have a leftoverPROGRAMblock from testing, or you might be including a file that defines a main program without realizing it.Validate your compilation command
Are you compiling multiple source files that each contain their ownPROGRAMblock? For example, runninggfortran main.f90 helper.f90wherehelper.f90also has aPROGRAMstatement will trigger this error. Only one file in your compilation set should have a mainPROGRAMblock—convert any extra ones toSUBROUTINEorFUNCTION(and wrap them in a module if needed).Watch for implicit main programs
Some Fortran compilers treat the first standalone executable unit (a subroutine/function not wrapped in a module or program) as an implicit main program if there's no explicitPROGRAMblock. If you have two such standalone routines, the compiler might interpret both as main programs. Make sure all helper code is either inside a module or explicitly part of your main program's scope.
A quick test: try compiling just your intended main program file in isolation. If that compiles without errors, you know the problem lives in another file you're including or compiling alongside it.
内容的提问来源于stack exchange,提问作者Rik Ghosh

