4

I want to load a fortran dll in C++ code and call a function in the fortran dll.

Following is the code

  SUBROUTINE SUB1()
  PRINT *, 'I am a function '
  END

After creation of the foo.dll [fotran dll ] this is the folowing C++ code in visual studio 2012 that I have written to load the fortran dll . and call the function SUB1 in the fortran code

#include <iostream>
#include <fstream>
#include <Windows.h>

using namespace std;  
extern "C" void SUB1();
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);

int main(void)
{
                LoadLibrary(L"foo.dll");

                PGNSI pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("foo.dll")),"SUB1");

                return 0;

}

While running the I am getting the following error:

The program can't start because libgcc_s_dw2-1.dll is missing from your computer. Try reinstalling the program to fix this problem.

Is this the correct way of calling the dll from C++ ? I am very new to this fortran dll . Please help me regarding this.

1
  • 1
    please post your OS, Fortran and C++ compiler. Compiler flags would be useful too. Try to seach for fortran-iso-c-binding to start with Fortran-C interoperability. May 12, 2014 at 14:40

1 Answer 1

3

First of all you need to export the function like this...

!fortcall.f90
subroutine Dll1() BIND(C,NAME="Dll1")
implicit none
!DEC$ ATTRIBUTES DLLEXPORT :: Dll1
PRINT *, 'I am a function'
return
end !subroutine Dll1

Create the dll using following command

gfortran.exe -c fortcall.f90
gfortran.exe -shared -static -o foo.dll fortcall.o

After that, place the libgcc_s_dw2-1.dll, libgfortran-3.dll and libquadmath-0.dll in the application path of VS. OR you can add the PATH to environment.

After that, you can call the FORTRAN exposed function from VS like below...

#include <iostream>
#include <Windows.h>

using namespace std;
extern "C" void Dll1();
typedef void(* LPFNDLLFUNC1)();

int main(void)
{
    HINSTANCE hDLL;
    LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer

    hDLL = LoadLibrary(L"foo.dll");

    if (hDLL != NULL)
    {
        lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"Dll1");
        if (!lpfnDllFunc1)
        {
            // handle the error
            FreeLibrary(hDLL);
            return -1;
        }
        else
        {
            // call the function
            lpfnDllFunc1();
        }
    }
    return 0;
}
2
  • 1
    Is the !DEC$ ATTRIBUTES DLLEXPORT really doing anything? I believe gfortran ignores !DEC$ directives. GCC exports all symbols by default AFAIK. The return statement is not necessary for sure. May 13, 2014 at 7:42
  • Really old issue, but I'm investigating this and found this topic as a top result, but from I could gather !DEC$ ATTRIBUTES DLLEXPORT is for ifort, you then need to compile the source code with the /dll option for the compiler to work
    – ghylander
    Mar 8, 2022 at 13:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.