Community
cancel
Showing results for 
Search instead for 
Did you mean: 
boehmc
Novice
122 Views

Accessing Fortran module DLL from C++ and Fortran

We are trying to create a fortran module DLL that our C++ program can access to set the module variable to a value and then our Fortran program can use that module to get the value of the variable.

We compiled them in 2 separate solutions because that is similar to our actual project setup and to make sure there wasn't any automatic linking being done by the IDE that wouldn't occur in our actual project.

Project FortranModuleDLL is our attempt to create a module (or subroutine that uses the module) to be used by C and Fortran.
FortranModule.f90 is a Fortran module that holds an integer as well as a subroutine to add 1 to an integer passed to it.
entry.f90 was added to test if we needed to dll export from a non-module that uses the module (suggested from stackoverflow).

Project UseFortranModDLL is a Fortran project to use FortranModuleDLL - This worked until we added bind(c) to the module project.

Project C_UseFortranMod is a C++ project to use FortranModuleDLL.

We set FortranModuleDLL.lib in the Linker Properties for both C_UseFortranMod and UseFortranModDLL projects and manually copy the .lib file to the $(SolutionDir)lib folder.

Results:

FortranModuleDLL builds without error.

C_UseFortranMod errors: (C)
Error 1 error LNK2019: unresolved external symbol __imp__getNumber referenced in function _main C_UseFortranMod
Error 2 error LNK2019: unresolved external symbol __imp__setNumber referenced in function _main C_UseFortranMod
Error 3 error LNK2019: unresolved external symbol __imp__addOne referenced in function _main C_UseFortranMod

UseFortranModDLL errors: (Fortran)
Error 1 error LNK2019: unresolved external symbol __imp__SETNUMBER referenced in function _MAIN__ UseFMod.obj
Error 2 error LNK2019: unresolved external symbol __imp__GETNUMBER referenced in function _MAIN__ UseFMod.obj

Tags (2)
0 Kudos
2 Replies
FortranFan
Honored Contributor I
69 Views

Retry after avoiding the name-mangling issue which you can do by employing the NAME= option in the BIND clause in your Fortran code as shown below; then use the same names in your C++ prototype declarations for Fortran procedures with the extern "C" attribute

   ..
   subroutine getNumber(intArg) bind(C, name="getNumber")
   ..
   subroutine setNumber(intArg) bind(C, name="setNumber")
   ..
   subroutine addOne(intArg) bind(C, name="addOne")

 

Gopika_Intel
Moderator
51 Views

Hi,


Thank you for reaching out. We're moving this case to the Intel Fortran compiler forum


Regards

Gopika