title: gfortran: compile source codes from hierarchically dependent subroutine files
body: <p>I want to compile <code>main.f90</code>, and it depends on three subroutines in separate files. For the third subroutine, it also has three sub_subroutines.</p>
  
  <ol start="2">
  <li><code>sub1.f</code> </li>
  <li><code>sub2.f</code></li>
  <li><code>sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f</code></li>
  </ol>
  
  <p>Run the following code would produce a long error message as below.</p>
  
  <p><code>gfortran main.f90 sub1.f sub2.f sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f -o- test.exe</code></p>
  
  <p>I searched and found that I may need flag <code>-c</code>, but I am not sure about the order of compiling, and then how to link object files into a standalone program. Or it is related to this question: <a href="https://stackoverflow.com/questions/18463947/how-to-compile-multi-folder-fortran-project-having-interfaces-modules-and-subro">how to compile multi-folder Fortran Project having interfaces, modules and subroutines</a>.</p>
  
  <p>Thanks in advance!</p>
  
  <pre><code>test.exe: In function `_fini':
  (.fini+0x0): multiple definition of `_fini'
  /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here
  test.exe: In function `__data_start':
  (.data+0x0): multiple definition of `__data_start'
  /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.data+0x0): first defined here
  test.exe: In function `__data_start':
  (.data+0x4): multiple definition of `__dso_handle'
  /usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
  test.exe:(.rodata+0x4): multiple definition of `_IO_stdin_used'
  /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
  test.exe: In function `_start':
  (.text+0x0): multiple definition of `_start'
  /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.text+0x0): first defined here
  test.exe:(.rodata+0x0): multiple definition of `_fp_hw'
  /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata+0x0): first defined here
  test.exe: In function `main':
  (.text+0x399): multiple definition of `main'
  /tmp/ccwQ3UVQ.o:main.f90:(.text+0x20ee): first defined here
  test.exe: In function `_init':
  (.init+0x0): multiple definition of `_init'
  /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
  /usr/lib/gcc/i686-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
  test.exe:(.dtors+0x4): first defined here
  /usr/bin/ld: error in test.exe(.eh_frame); no .eh_frame_hdr table will be created.
  collect2: ld returned 1 exit status
  </code></pre>