Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28741 Discussions

C++ program calling f.90 DLL using shared Memory file(Module)

boehmc
Novice
3,027 Views

I am trying to use a C++ program to call the Fortran DLL. The C++ and Fortran are both using the shared memory file in a module. Why can I not see the global variable in C++ that I have exported from the DLL?

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
2,980 Views

Thanks for the projects. There are several issues I see.

  • The C++ source doesn't declare the shared variable with __declspec(dllimport), so it's not imported
  • The C++ source doesn't declare the shared variable with 'extern "C"', causing name mangling, so the linker can't match the names
  • The Fortran source doesn't declare the shared variable(s) with 'bind(C)', so their exported names are uppercase where the C++ wants lowercase, so the linker can't match the names

An additional issue is that the shared variables are declared in a separate static library project. When you USE a module that has DLLEXPORTs, the compiler turns these into DLLIMPORTs, but that's not what you want. The object module from the library never gets pulled into the DLL, so the DLLEXPORTs are not seen.

The simplest solution to this last issue is to move SharedMemory.f90 into the FDLL project and dispense entirely with the SharedMemory project. An alternative, which I tend to prefer, is to build shared variables into their own DLL which is linked against by executable code in both languages (so FDLL and F_CALLS_C would link to the SharedMemory DLL project.

I have attached a corrected set of projects. Let me know how this works for you.

View solution in original post

0 Kudos
4 Replies
Steve_Lionel
Honored Contributor III
3,014 Views

Can you show us a small but complete example that demonstrates the problem?

0 Kudos
boehmc
Novice
2,994 Views

Yes sorry. The zipped file was not attached somehow.  Here is the complete project.

0 Kudos
Steve_Lionel
Honored Contributor III
2,981 Views

Thanks for the projects. There are several issues I see.

  • The C++ source doesn't declare the shared variable with __declspec(dllimport), so it's not imported
  • The C++ source doesn't declare the shared variable with 'extern "C"', causing name mangling, so the linker can't match the names
  • The Fortran source doesn't declare the shared variable(s) with 'bind(C)', so their exported names are uppercase where the C++ wants lowercase, so the linker can't match the names

An additional issue is that the shared variables are declared in a separate static library project. When you USE a module that has DLLEXPORTs, the compiler turns these into DLLIMPORTs, but that's not what you want. The object module from the library never gets pulled into the DLL, so the DLLEXPORTs are not seen.

The simplest solution to this last issue is to move SharedMemory.f90 into the FDLL project and dispense entirely with the SharedMemory project. An alternative, which I tend to prefer, is to build shared variables into their own DLL which is linked against by executable code in both languages (so FDLL and F_CALLS_C would link to the SharedMemory DLL project.

I have attached a corrected set of projects. Let me know how this works for you.

0 Kudos
boehmc
Novice
2,971 Views

Thank you for all your help Steve. That is enough information to help me on my way!

0 Kudos
Reply