2

i am writing a program in Fortran which uses .mod files, .dll libraries and .h headers.

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.

The only source file is called MAIN.f90:

! 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, &
       DFnnz, DFrow, DGnnz, DGrow, DGcol, HMnnz, HMrow, HMcol, &
       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, &
       DFnnz, DFrow, DGnnz, DGrow, DGcol, HMnnz, HMrow, HMcol, &
       OBJ, CON, DOBJ, DCON, HESS)

end

The function final_orbit called by the subroutine CON is contained in the module "problem_definition_tools".

For the compilation i use the following windows batch file called "compile.bat"

x86_64-w64-mingw32-gfortran src\MAIN.f90 -cpp -Iinclude -Ifinclude\worhp -Ifinclude\aerospace -Jobj -Lbin -lworhp -o bin\megaceppa.exe

which gives the error

undefined reference to '__problem_definition_tools_MOD_final_orbit'

I run the command from the directory 'Test' which has the following structure

  • \bin\
    • libworhp.dll
  • \finclude
    • \worhp\
      • std.mod
    • \aerospace\
      • problem_definition_tools.mod
  • \include
    • \worhp\
      • macros.h
  • \lib
    • libworhp.lib
  • \obj
    • worhp_interface.mod
  • \src
    • MAIN.f90
  • compile.bat

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.

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.

7
  • I don't know, they should be in the "obj" directory, right? Commented Jan 15, 2015 at 13:34
  • I just tested a simpler version of the program which doesn't use the "problem_definition_tools" module. The same batch file compiles without any error, but it also doesn't produce any object file in any directory. I don't know why there are no object files even after a correct compilation... Commented Jan 15, 2015 at 14:14
  • After checking again, and searching my whole computer, i can confirm that there's no trace of object files. Could it be that the compiler creates some temporary object files and then deletes them just after the compilation? Commented Jan 15, 2015 at 15:37
  • 1
    It's surprising that only a .mod file is produced. But not impossible. An object file/library needn't always be required when a module is used. So, could you show the compilation of the module? Although it isn't my system or compiler of choice so I may not be much more help. Commented Jan 15, 2015 at 21:43
  • 1
    I solved the probelm by including the source file problem_definition_tools.f90 in the compilation, now everything compiles flawlessly. Strangely enough, even this compilation doesn't produce any .o or .obj file, but i don't care as long as it works! Thanks for the help! Commented Jan 16, 2015 at 8:37

1 Answer 1

0

I think it might be related to some declaration of final_orbit function.

It seems close to the following problem:

Linking fortran module: "undefined reference"

And I think the problem should be located in the code of module problem_definition_tools

Sign up to request clarification or add additional context in comments.

1 Comment

The relateness is completely obvious. The question is where the true priblem is, the OP knows which module is problematic. The linked question is not related, it speaks about undefined reference to an external function, not a module function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.