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

Calling C DLL from Fortran in Visual Studio 2017 / Fortran Interface Definition

FloBa
Novice
1,231 Views

Dear Intel Community, 

I am using Intel Parallel Studio XE 2019 Composer Edition for Fortran and Visual Studio 2017 for the development of a simulation software. In this context, I received a Dynamic Link Library written in C, which I need to integrate in my Fortran project. Also, a header file to the DLL was provided. 

First of all, I know there are a lot of similar topics to be found in the web and this forum, however, I cannot really make head nor tail of them, as I'm not a professional programmer. 

 

What I figured out so far is that there are two options for linking the DLL, dynamic run time and load time. In order to do the latter, as it seemed less complicated to me to aim for load time linking, I created a .lib-file according to  the instructions given here:

https://asawicki.info/news_1420_generating_lib_file_for_dll_library

Now, I need to know how exactly  I can relate the three files (lib,dll,header) to my Fortran project? 

 

Furthermore, I'm not sure how to correctly define the interface for the Fortran call to a C function from the DLL. I found some examples in the Development Reference Guide under Mixed Language Programming (and standard-fortran-and-c-interoperability and standard-tools-for-interoperability/procedures ) but to be honest I cannot transfer it to my use case.  Would it be possible for someone here to generate an example Fortran interface definition for one of the functions contained in the DLL?  I attached all the files in the zip file.

 

Thank you very much in advance, any help is much appreciated!

 

Regards,

Florian

Labels (1)
0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
1,076 Views

The Intel compiler is now free for everyone, so compiler licenses are no longer an issue. But what you're doing is unrelated to compiler or VS versions.

Intel keeps moving things around online, which is annoying and problematic. The most recent samples are here as linked from the Intel Fortran FAQ.

View solution in original post

5 Replies
JNichols
New Contributor I
1,193 Views

There are samples available for most things including this method.  

Follow the path down from INTELSW and you will find the samples, sorry but I am only using one-api.  

I suggest you move to VS2019 or 2022 and oneapi as most people here are more likely to be using the combination. 

0 Kudos
FloBa
Novice
1,094 Views

Dear J.,

 

thanks for your reply. 

 

I'm using VS 2017 because any VS update that comes after the latest version of Intel compiler available at that time is not supported. As I'm working from a company account, I have some limitations regarding available compiler licenses.

 

I've already checked the samples folder before posting in the forum but for me it only contains a link to the Intel Documentation Library. 

 

Regards,

Florian

 

 

0 Kudos
Steve_Lionel
Honored Contributor III
1,110 Views

The .lib file is best just added to your project as if it were a source file. The .dll you don't do anything with other than put it somewhere that Windows will find when the application is run. It could be in the same folder as the EXE, for example.

As for the header file, you have to translate that to Fortran. I've made a start at this for you - you would create a new source file propa_mod.f90 with this text (and interfaces for the other routines), add the .f90 to your project, and put "use propa_mod" in program units where you need to call these routines.

module propa_mod
    use, intrinsic :: iso_c_binding
    implicit none
    
    interface
        function NWET (lat, lon) bind(C,name="NWET")
        import
        real(C_DOUBLE) :: NWET
        real(C_DOUBLE), value :: lat, lon
        end function NWET

        function rain_height (lat, lon) bind(C,name="rain_height")
        import
        real(C_DOUBLE) :: rain_height
        real(C_DOUBLE), value :: lat, lon
        end function rain_height
    end interface
end module propa_mod

The key points are to use the case-sensitive name of the function in the name= value (technically, you don't need to do that when the name is all lowercase, but it doesn't hurt), to use the proper real kind for the function return and arguments, and add the "value" attribute for the arguments as these are all pass-by-value.

Now, I assume someone is going to jump in and ask "What about the DLLIMPORT directives?". The answer is that these aren't necessary for procedure calls. They do improve performance a bit, but not enough to matter.

 

0 Kudos
Steve_Lionel
Honored Contributor III
1,077 Views

The Intel compiler is now free for everyone, so compiler licenses are no longer an issue. But what you're doing is unrelated to compiler or VS versions.

Intel keeps moving things around online, which is annoying and problematic. The most recent samples are here as linked from the Intel Fortran FAQ.

FloBa
Novice
1,067 Views

Steve,

 

I still owe you an answer and a big thank you.

 

Thanks to your help and instructions, I could set my programm code today and all is running as expected. 

 

Thanks again, regards,

Florian

Reply