- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Tags:
- Thanls
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you show us a small but complete example that demonstrates the problem?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes sorry. The zipped file was not attached somehow. Here is the complete project.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Tags:
- Thanls
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for all your help Steve. That is enough information to help me on my way!
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page