title: Gfortran and Undefined reference to '__[module_name]_MOD_[function_name]'
body: <p>i am writing a program in Fortran which uses .mod files, .dll libraries and .h headers.</p>
  
  <p>I must be forgetting something when i call the compiler, because i get the error:
  undefined reference to '__[module_name]_MOD_[function_name]', where [module_name] is the name of one of the modules used by the main program and [function_name] is the name of a function contained in the module.</p>
  
  <p>The only source file is called MAIN.f90:</p>
  
  <pre><code>! WORHP workspace access macros
  #include "worhp/macros.h"
  
  MODULE WORHP_INTERFACE
  
    USE std
    USE problem_definition_tools
  
  CONTAINS
  
    SUBROUTINE OBJ(N,X,F) BIND(C)
      INTEGER(WORHP_INT)  :: N
      REAL(WORHP_DOUBLE)  :: X(N),F
      INTENT(in)          :: N, X
      INTENT(out)         :: F
      F = X(1)
    END SUBROUTINE OBJ
  
    SUBROUTINE CON(N,M,X,G) BIND(C)
      INTEGER(WORHP_INT)  :: N,M
      REAL(WORHP_DOUBLE)  :: X(N),G(M)
      INTENT(in)          :: N, M, X
      INTENT(out)         :: G
      TYPE (orbit)        :: O
      O = final_orbit(X)
      G = [O%Lp, O%La]
    END SUBROUTINE CON
  
    SUBROUTINE DOBJ(N,dfnnz,DFROW,X,DF) BIND(C)
      INTEGER(WORHP_INT)  :: N,dfnnz,DFROW(DFnnz)
      REAL(WORHP_DOUBLE)  :: X(N),DF(DFnnz)
      INTENT(in)          :: N, DFnnz, DFrow, X
      INTENT(out)         :: DF
      DF = [1, 0, 0, 0, 0, 0]
    END SUBROUTINE DOBJ
  
    SUBROUTINE DCON(N,M,DGnnz,DGROW,DGCOL,X,DG) BIND(C)
      INTEGER(WORHP_INT)  :: N,M,DGnnz,DGROW(DGnnz),DGCOL(DGnnz)
      REAL(WORHP_DOUBLE)  :: X(N),DG(DGnnz)
      INTENT(in)          :: N,M,DGnnz,DGrow,DGcol,X
      INTENT(out)         :: DG
      ! Dummy
    END SUBROUTINE DCON
  
    SUBROUTINE HESS(N,M,HMnnz,HMrow,HMcol,X,Mu,HM) BIND(C)
      INTEGER(WORHP_INT)  :: N, M, HMnnz, HMrow(HMnnz), HMcol(HMnnz)
      REAL(WORHP_DOUBLE)  :: X(N),Mu(M),HM(HMnnz)
      INTENT(in)          :: N, M, HMnnz, HMrow, HMcol, X, Mu
      INTENT(out)         :: HM
      ! Dummy
    END SUBROUTINE HESS
  
  END MODULE WORHP_INTERFACE
  
  program MAIN
  
    USE WORHP_INTERFACE
    USE Worhp_User
  
    INTEGER (WORHP_INT) :: Mode, N, M, DFnnz, DGnnz, HMnnz
    PARAMETER (N=6, M=2, DFnnz=1, DGnnz=12, HMnnz=12)
    INTEGER (WORHP_INT) :: DFrow(DFnnz), DGrow(DGnnz), DGcol(DGnnz)
    INTEGER (WORHP_INT) :: HMrow(HMnnz), HMcol(HMnnz)
    INTEGER (WORHP_INT) :: Iparam(10)
    REAL (WORHP_DOUBLE) :: X(N), L(N+M), U(N+M), Dparam(10)
    REAL (WORHP_DOUBLE) :: Infty = 1d20
  
    ! Check Version of library and header files
    CHECK_WORHP_VERSION
  
    L = [9000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 200000.0, 35786000.0]
    U = [10000.0, 5.0, 180.0, 0.15, 180.0, 0.15, 200000.0, 35786000.0]
  
    X = [9520.00, 1.47490136, 71.50755639, 0.09948622, 97.00248532, 0.09296147]
  
    DFrow = [6]
    DGrow = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
    DGcol = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
    HMrow = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
    HMcol = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
  
    ! Get default parameter values
    Mode = 0
  
    CALL WorhpSimple(Mode, N, M, X, L, U, Dparam, Iparam, &amp;
         DFnnz, DFrow, DGnnz, DGrow, DGcol, HMnnz, HMrow, HMcol, &amp;
         OBJ, CON, DOBJ, DCON, HESS)
  
    ! User-defined derivatives are available
    Iparam(1) = 1
    Iparam(2) = 0
    Iparam(3) = 0
  
    ! Run the solver
    Mode = 1
  
    CALL WorhpSimple(Mode, N, M, X, L, U, Dparam, Iparam, &amp;
         DFnnz, DFrow, DGnnz, DGrow, DGcol, HMnnz, HMrow, HMcol, &amp;
         OBJ, CON, DOBJ, DCON, HESS)
  
  end
  </code></pre>
  
  <p>The function <code>final_orbit</code> called by the subroutine <code>CON</code> is contained in the module "problem_definition_tools".</p>
  
  <p>For the compilation i use the following windows batch file called "compile.bat"</p>
  
  <p><code>x86_64-w64-mingw32-gfortran src\MAIN.f90 -cpp -Iinclude -Ifinclude\worhp -Ifinclude\aerospace -Jobj -Lbin -lworhp -o bin\megaceppa.exe</code></p>
  
  <p>which gives the error</p>
  
  <p><code>undefined reference to '__problem_definition_tools_MOD_final_orbit'</code></p>
  
  <p>I run the command from the directory 'Test' which has the following structure</p>
  
  <ul>
  <li>\bin\
  <ul>
  <li>libworhp.dll</li>
  </ul></li>
  <li>\finclude
  <ul>
  <li>\worhp\
  <ul>
  <li>std.mod</li>
  </ul></li>
  <li>\aerospace\
  <ul>
  <li>problem_definition_tools.mod</li>
  </ul></li>
  </ul></li>
  <li>\include
  <ul>
  <li>\worhp\
  <ul>
  <li>macros.h</li>
  </ul></li>
  </ul></li>
  <li>\lib
  <ul>
  <li>libworhp.lib</li>
  </ul></li>
  <li>\obj
  <ul>
  <li>worhp_interface.mod</li>
  </ul></li>
  <li>\src
  <ul>
  <li>MAIN.f90</li>
  </ul></li>
  <li>compile.bat</li>
  </ul>
  
  <p>I would like to specify that the module "problem_definition_tools" is written by me, and i know for sure that it works because i already used it in another program. Moreover, i re-compiled it with x86_64-w64-mingw32-gfortran in order to avoid any compatibility issues with this program.</p>
  
  <p>I am sure that there is something wrong in the way i call the compiler, but i can't figure it out because of my limited experience with Fortran. I also triend to search for similar questions on this website, but i couldn't find anything closely related to my problem.</p>