3

I've developed a module written in C++ that manages the licenses for my company's product. To prevent DLL replacement, it is our goal to statically link the DLL in the solution. This is very easy to do in C++ but proving to be a bit problematic for part of our codebase that is written in Fortran.

I realize that this could possibly vary from compiler to compiler (We use Intel Fortran 9.1), but is there any universal way to implement static linking of a C++ DLL within Fortran?

Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
  • You cannot statically link a DLL. You either need to link against a lib file (a static library version of the DLL), or the object files used to build the DLL. – Jörgen Sigvardsson Oct 15 '11 at 14:57

2 Answers2

1

If you have access to the source, just compile it to object files and link them into your Fortran project. ISO_C_BINDING should work on many compilers.

bdforbes
  • 1,486
  • 1
  • 13
  • 31
  • Also, use dumpbin on Windows or objdump on Linux to look at the symbol table of a binary. This will tell you if there name mangling issues. With modern compilers I don't think it should be an issue though. – bdforbes Oct 15 '11 at 00:01
  • Could you expand on this further? I am a complete Fortran newbie. – Chuck Callebs Oct 15 '11 at 00:04
  • The Fortran ISO_C_BINDING allows you to exactly specify routine names and overcomes name mangling issues. – M. S. B. Oct 15 '11 at 12:43
  • http://stackoverflow.com/questions/7198837/calling-c-function-from-intel-fortran-11-in-visual-studio-2008-step-by-step-proc/7200089#comment8687362_7200089 – bdforbes Oct 20 '11 at 11:42
1

To get static linking, the usual way is not to use DLL but simple libraries instead (*.lib). This has nothing to do with programming languages : it just depends on the operating system.

Building a library is also simpler than building a DLL. On Unix, a library has the suffix .a whereas a DLL has a suffix .so (for shared object).

Nevertheless, it is often possible to link a DLL statically but this is obtained by a specific option passed to the linker. For instance on Unix, with many compiler suites, the option is either -static or -Bstatic. Look at the keyword "static" in your programming manual of your compilers.

Francois Jacq
  • 1,244
  • 10
  • 18