<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calling C dll from FORTRAN in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855917#M67425</link>
    <description>&lt;DIV style="margin:0px;"&gt;Mike,&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Noticed you had got no responses. I;m not a C programmer, so hope I don't mislead you.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;When you build the C DLL, are the entry points exported so that they are visible externally? Try dependency walker (from dependencywalker.com) - use that to open your DLL and check that the entry points are correct.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;In the samples with Intel Fortran, there is one which includes loading a DLL. check to see if that can be applied to your C DLL.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;The only remaining issue then would be getting the arguments correct, as you already mentioned.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Regards.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;David&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Nov 2008 23:38:55 GMT</pubDate>
    <dc:creator>DavidWhite</dc:creator>
    <dc:date>2008-11-03T23:38:55Z</dc:date>
    <item>
      <title>Calling C dll from FORTRAN</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855916#M67424</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I am a FORTRAN user who has a need to make calls and extract data from a C routine. I am not familiar with C (the C routine I need to make calls to &lt;EM&gt;is&lt;/EM&gt; a "C" routine, not C++), nor am I very familiar with mixed language programming in general. The biggest stumbling block I have is trying to get the two codes to "see" and "like" each other if you will.&lt;/P&gt;
&lt;P&gt;Here is what I'm using:&lt;/P&gt;
&lt;P&gt;Intel Fortran Compiler Integration for Microsoft Visual Studio 2005, Version 9.1.3192.2005&lt;/P&gt;
&lt;P&gt;Microsoft Visual Studio 2005 Standard Edition - ENU Service Pack 1 (KB926601)&lt;/P&gt;
&lt;P&gt;I'm using the Visual Studio IDE&lt;/P&gt;
&lt;P&gt;(I have some experience calling FORTRAN DLLs from Exel VBA and passing arrays and strings back and forth from Excel and the FORTRAN DLL, sacrificing some brain cells in the process. And I am aware that there are differences in how C and FORTRAN handle arrays and data and such; when I get that far..but for now I just need to figure out how to get the two to see each other. I've googled and googled and I get a lot of bits and pieces but can't quite put it together).&lt;/P&gt;
&lt;P&gt;I'm not sure if this is the right place to ask this since I think the problem may lie on the C side but this is where I'm going to start.&lt;/P&gt;
&lt;P&gt;I'm going to start out with baby steps first. Below is a C program I got from "C Primer Plus" by Stephen Prata. I am able to build and run it from it's own Project as a WIN32 Console Application.&lt;/P&gt;
&lt;P&gt;
&lt;PRE&gt;[cpp][/cpp]&lt;/PRE&gt;
&lt;PRE&gt;[cpp]#include &lt;STDIO.H&gt;

int testCdll(void)                /* a simple program             */
{
    int num;                  /* define a variable called num */
    num = 1;                  /* assign a value to num        */

    printf("I am a simple "); /* use the printf() function    */
    printf("computer.n");
    printf("My favorite number is %d because it is first.n",num);
   
    return 0;
}[/cpp]&lt;/STDIO.H&gt;&lt;/PRE&gt;
&lt;/P&gt;
&lt;P&gt;I then created another Project where I created a DLL version of the C code above.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I simply want to call this from a simple FORTRAN rountine such as&lt;/P&gt;
&lt;P&gt;program Forcall&lt;BR /&gt; implicit none&lt;BR /&gt; call testCdll&lt;BR /&gt; end program Forcall&lt;/P&gt;
&lt;P&gt;I created this program in its own Project.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;1) What do I need to do to the C code to make it visible to the FORTRAN? I've played a little with an "extern" command but don't know exactly how to implement it.&lt;/P&gt;
&lt;P&gt;2) What settings do I need to set in the IDE of the FORTRAN Project to so that the FORTRAN code knows where to look? So far under Project Properties\Link\General I've put the path of the C DLL in "Additional library directories". This doesn't quite sound right but I can't find anything for "DLL" directories.&lt;/P&gt;
&lt;P&gt;I've also put the name of the C dll in the "Additional Dependencies" box in the INPUT dialog box under "Linker".&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Note: I did manage to somehow build the C DLL using the extern command/directive or whatever without any complaints and when I tried to build the FORTRAN code I got a "cannot open DLL" (with the name I gave the DLL) message. So I think I was able to get the FORTRAN to "see" the dll but it could not open it. Unfortunately in copying and pasting for this e-mail I lost that "extern" addition to the code that I made.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Mike&lt;/P&gt;</description>
      <pubDate>Fri, 31 Oct 2008 17:59:36 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855916#M67424</guid>
      <dc:creator>sabur</dc:creator>
      <dc:date>2008-10-31T17:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calling C dll from FORTRAN</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855917#M67425</link>
      <description>&lt;DIV style="margin:0px;"&gt;Mike,&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Noticed you had got no responses. I;m not a C programmer, so hope I don't mislead you.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;When you build the C DLL, are the entry points exported so that they are visible externally? Try dependency walker (from dependencywalker.com) - use that to open your DLL and check that the entry points are correct.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;In the samples with Intel Fortran, there is one which includes loading a DLL. check to see if that can be applied to your C DLL.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;The only remaining issue then would be getting the arguments correct, as you already mentioned.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Regards.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;David&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Nov 2008 23:38:55 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855917#M67425</guid>
      <dc:creator>DavidWhite</dc:creator>
      <dc:date>2008-11-03T23:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calling C dll from FORTRAN</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855918#M67426</link>
      <description>&lt;DIV style="margin:0px;"&gt;You must add something like&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;extern "C" __declspec(dllimport) void __stdcall testCdll( );&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;to your C program. Then you must add to the Fortran project the .LIB file created when you create the DLL ,because the .LIB file contains the symbols exported from your DLL. Note that the case is important.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Then, wherever you reference your C routine in your Fortran project,you mustinclude an interface block for your C routine 'testCdll' where the calling convention is specified and the correct alias defined for it to match the C-symbol created and exportedin your .LIB file. In Compaq Visual Fortran (which I use), you do this using compiler directives such as&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;!DEC$ ATTRIBUTES DLLIMPORT :: testCdll&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;!DEC$ ATTRIBUTES STDCALL :: testCdll&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;!DEC$ ATTRIBUTES ALIAS:'_testCdll@0':: testCdll&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Depending on the calling convention specified in your C code, the ALIAS name will vary. You may need to look at your C .LIB file using DUMPBIN /exports to list the actual symbol name generated and use that in the ALIAS directive.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Finally, ensure that your C Dll is added to the same directory as your Fortran executable that calls it (or else add it to the C:windowssystem32 folder)&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Nov 2008 10:21:53 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855918#M67426</guid>
      <dc:creator>anthonyrichards</dc:creator>
      <dc:date>2008-11-04T10:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calling C dll from FORTRAN</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855919#M67427</link>
      <description>&lt;DIV style="margin:0px;"&gt;&lt;/DIV&gt;
&lt;P&gt;The compiler ships with sample C / FORTRAN mixed projects, exploring those has been a big help for me.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Nov 2008 18:01:13 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855919#M67427</guid>
      <dc:creator>Steve_Nuchia</dc:creator>
      <dc:date>2008-11-04T18:01:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calling C dll from FORTRAN</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855920#M67428</link>
      <description>&lt;DIV style="margin:0px;"&gt;I managed to figure out what I needed to do. First, discovered from a couple of posts on an MSDN forum that I need to link to a C lib file, not a C dll. Trouble was, building the C code above did not produce a lib file. Turns out I need to export something in order for a lib for to be created, so I did that using a couple of lines from another poster. I now had a lib file which I linked to when I compiled the Fortran code. (My actual C code will be exporting plenty of data, so that is not a problem).&lt;BR /&gt;&lt;BR /&gt;I then added a couple of #define...declspec... lines and a DLLExport line and got the Fortran code to compile, linking to the C code without any errors. Maybe it's not quite right yet, but it's progress.&lt;BR /&gt;&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;&lt;BR /&gt;&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;I'll be looking at AnthonyRichards post in more detail. Yes, I am familiar with dependencywalker; it did not show any problems. I also need to dig up my CD/DVD and see if I can find those sample files.&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;&lt;BR /&gt;&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Thanks. Every littlel bit helps&lt;BR /&gt;&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;&lt;BR /&gt;&lt;/DIV&gt;
&lt;DIV style="margin:0px;"&gt;Onward.&lt;BR /&gt;&lt;/DIV&gt;
&lt;P&gt;Mike&lt;/P&gt;</description>
      <pubDate>Sat, 08 Nov 2008 03:46:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-dll-from-FORTRAN/m-p/855920#M67428</guid>
      <dc:creator>sabur</dc:creator>
      <dc:date>2008-11-08T03:46:42Z</dc:date>
    </item>
  </channel>
</rss>

