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.
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
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")