fortran - gfortran: multiple definitions of... first defined here -


i have code includes main program , many modules in separate files linking. have makefile creates .o files each module (one on separate line) , put them together, such here:

mpif90 - modutils mpif90 -c modvarsym mpif90 -c s1_phi.f90 mpif90 -c s2_lambda.f90  mpif90 maincode.f90 modutils.o modvarsym.o  s1_phi.o s2_lambda.o -o maincode  

the above compiles fine , runs ok - except tat suspect suspect array bound problems in variables. include -fbounds-check maincode statement such here:

mpif90 maincode.f90 modutils.o modvarsym.o  s1_phi.o s2_lambda.o -o -fbounds-check maincode  

that's when numerous "multiple definition" errors appear, , code no longer compile. imagine because of -fbounds-check: rather enabling checking array bounds, additional checks. suspect error in way enter files in make file. not find way work. in these files, both modvarsym , modutils used main code other 2 modules. main code uses 4 modules. there no include statement in these files. maincode file program statement, variables declared once in modvarsym. overall, code compiles , runs without -fbounds-check. want use -fbounds-check make sure arrays not overrun. able put me on right track? thank you.

this answer @dave_thompson_085 gave in comments, seems solve problem.

first, assume first command meant have -c, , first 2 meant have .f90 (or .f95 or similar) suffix otherwise compiler shouldn't them. second, -o -fbounds-check maincode (in absence of -c) means put linked output in file -fbounds-check , include maincode (if exists) among files linked. since have linked routines maincode, linking same routines again plus maincode produces duplicates.

move -fbounds-check before -o @ least; better, usual style (though not required) put options affect parsing , code generation before source file(s) well, , in example maincode.f90. note generates bound checks routines in maincode; if there subscripting errors in other routines won't caught. when have bug in compiled language place problem detected may not actual origin, , best apply debugging options can.