Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26739 Discussions

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

boehmc
Novice
297 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
Black Belt Retired Employee
250 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

4 Replies
Steve_Lionel
Black Belt Retired Employee
284 Views

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

boehmc
Novice
264 Views

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

Steve_Lionel
Black Belt Retired Employee
251 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

boehmc
Novice
241 Views

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

Reply