0

I have written some wrapper functions in c++ for some old Fortran (g77, I think) code. I created the .dll and .lib using an old Visual Fortran compiler, and have everything linked up and working in visual studio.

The problem I have now is creating an equivalent 64 bit version. I have installed gfortran and created a 64 bit .dll but no .lib was generated along with it, and I can't seem to link to the DLL or call any of the fortran functions without it.

I created my 64 bit .dll with the commands...

  $ gfortran -c {filenames.for}
  $ gfortran -m64 -shared -mrtd -o dll_foo.dll {filenames.o}

And here is a sample subroutine from the fortran...

SUBROUTINE KFACT(TR, RHOL, RHOV, FLIQ, FVAP, XK, IWANT, PROPR)
!DEC$ ATTRIBUTES DLLEXPORT :: KFACT

  IMPLICIT DOUBLE PRECISION (A-H,O-Z)
  INCLUDE 'nprop.cmn'
  DIMENSION PROPR(NPROP)
  DIMENSION IWANT(NPROP)
C
C SET IWANT VECTOR TO RETURN FUGACITY COEFFICIENT
C
  CALL IVZERO(IWANT, NPROP)
  IWANT(13) = 1
C
C CALL PROP2 TO RETURN VAP AND LIQ FUGACITY COEFFICIENTS
C
  CALL PROPS2(IWANT, 0, TR, RHOL, PROPR)
  FLIQ = PROPR(13)
  CALL PROPS2(IWANT, 0, TR, RHOV, PROPR)
  FVAP = PROPR(13)
C
  XK = FLIQ / FVAP
C
  RETURN
  END
C
  SUBROUTINE PDP(DEL, TR, PR, DPRDD, IWANT, PROPR) 

How can I create a .dll and .lib file to be linked to, from my Visual Studio (2005) c++ project?

/////////////////////////////UPDATE/////////////////////////////

I have been able to create a .lib file by using the following commands...

ar -cru libName.lib {filenames.o}
ranlib libName.lib

After adding this lib to my Additional Dependencies in VS, I get LNK errors for all of the fortran functions when they are called. The fortran declarations in the c++ look like...

extern "C" void PSAT(double& TK, double& PMPA, double& RHOL, double& RHOV, int IWORK[], double PROPR[], int& IERR);

and the LNK errors for this function...

error LNK2028: unresolved token (0A00004A) "extern "C" void __cdecl PSAT(double &,double &,double &,double &,int * const,double * const,int &)" (?PSAT@@$$J0YAXAEAN000QEAHQEANAEAH@Z) referenced in function "double __cdecl PFTH(double,double,int)" (?PFTH@@$$FYANNNH@Z)


error LNK2019: unresolved external symbol "extern "C" void __cdecl PSAT(double &,double &,double &,double &,int * const,double * const,int &)" (?PSAT@@$$J0YAXAEAN000QEAHQEANAEAH@Z) referenced in function "double __cdecl PFTH(double,double,int)" (?PFTH@@$$FYANNNH@Z)

I have tried adding different directives in the fortran code to resolve the names like but I have been unsuccessful.

I don't know if the problem lies in the c code, the fortran code, or the creation of the .lib file.

Any help would be greatly appreciated!

Wayne
  • 1
  • 2

2 Answers2

0

Unlike MS based linkers, GNU based linkers do not need a .lib file. All you need to do is prefix the dll with lib. In your case, it would be libdll_foo.dll. To link, just drop the lib. For instance, to link to a program called main

gfortran -o main.exe -ldll_foo main.f95

It needs libdll_foo to be on the path. Alternatively, you could tell it where to find libdll_foo.dll with the -L parameter.

cup
  • 7,589
  • 4
  • 19
  • 42
  • I don't think I was clear in my question. I am not compiling the .exe with gfortran, I need the .dll to link to my c++ project in VS. I edited the question for clarity. – Wayne Jun 11 '13 at 14:24
  • gfortran and VS binaries aren't compatible. If you want to link gfortran to a C++ project, you need to rebuild the C++ code in gcc. Basically, you either go Visual tools all the way or GNU all the way. The object file and DLL formats are different. Unlike the MS DLLs, GNU DLLs don't have a DLLMain. – cup Jun 11 '13 at 20:42
  • Thanks for your information, I ended up going with CMake to create a .lib file. – Wayne Jun 13 '13 at 20:01
0

I ended going with CMake to create my X64 .lib file. Here is the CMakeLists if anyone is interested.

cmake_minimum_required(VERSION 2.8)
enable_language(Fortran)
set(CMAKE_Fortran_CREATE_SHARED_LIBRARY ON)
set(CMAKE_Fortran_COMPILER gfortran)
add_library(Steam64 SHARED AUXPK.o eospk.o INTPK.o PROPPK.o SOLVPK.o coef.cmn     coefig.cmn nprop.cmn wconst.cmn)
SET_TARGET_PROPERTIES(Steam64 PROPERTIES LINKER_LANGUAGE C ARCHIVE_OUTPUT_DIRECTORY        "C:/Users/MYNAH/Desktop/fortranSource/build")

add_executable(testf AUXPK.o eospk.o INTPK.o PROPPK.o SOLVPK.o coef.cmn coefig.cmn     nprop.cmn wconst.cmn)
target_link_libraries(testf Steam64)
Wayne
  • 1
  • 2