how can I link a dll to fortran (visual studio)?

2019-09-11 15:27发布

I have some DLLs that I want to use in a FORTRAN Project in VISUAL STUDIO but I can't find how.

Here is a simple code I'm using to find out how.

Using visual studio I created a DLL from this

  subroutine printing

  !DEC$ ATTRIBUTES DLLEXPORT::printing
  print*,"dll naimi created"

  end subroutine printing

I added the link of the DLL to project>properties>Linker>General>Additional Library directories

Main program:

program Console11
implicit none

call printing

end program Console11

ERROR : Error 1 error LNK2019: unresolved external symbol _PRINTING referenced in function _MAIN__.

other solutions related to this suggest using the .lib created while generating the DLL, but in my real case I only have the DLLs without their .lib.

So how to use a DLL ... ?

1条回答
We Are One
2楼-- · 2019-09-11 15:42

You appear to be trying to use a DLL as an input file to the linker.

(You also appear to be trying to specify a file for a linker option that takes a directory. To specify an additional input file for the linker, either add the file to the project just like you would attach a source file, or use the "Linker > Input > Additional dependencies" project property. The property you mention in your post then tells the linker where (which directories) to search for those additional dependencies.)

Unlike the unix convention, you do not link against DLLs when building executables and other DLLs on Windows. The DLL typically does not contain the necessary information for the linker - instead that information is contained in an import library (which is just a variation of a typical .lib static library) or equivalent.

If you were able to successfully build a DLL, then you will probably find the import library for that DLL in the same directory as the DLL. Supply that import library as an additional dependency for projects that require the DLL.

When you link an EXE or other DLL using an import library on Windows, the target DLL is automatically loaded by the operating system when your executable code is loaded. This is called load time dynamic linking.

If you do not have the import library for a DLL, then your choices are:

  • Ask the person who built the DLL for the import library.

  • Reference the DLL using run time dynamic linking, rather than load time. This means that you use the Windows API functions LoadLibrary, GetProcAddress and friends in your program to explicitly tell the operating system to load a particular DLL and to obtain the address of a function pointer. If you are using Intel Fortran, then complete examples of this are installed with the compiler - see in the file "C:\Program Files (x86)\IntelSWTools\samples_2016\en\compiler_f\psxe\DLL.zip" or similar.

  • Generate an import library from the minimum information in the DLL, plus other information about the DLL that you may have. One approach to this is to write a module definition file (.def) for the DLL, and then use the LIB utility to turn that def file into an import library. See How to make a .lib file when have a .dll file and a header file for an example.

查看更多
登录 后发表回答