Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Calling C dll from FORTRAN

sabur
Beginner
763 Views

Hello,

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 is 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.

Here is what I'm using:

Intel Fortran Compiler Integration for Microsoft Visual Studio 2005, Version 9.1.3192.2005

Microsoft Visual Studio 2005 Standard Edition - ENU Service Pack 1 (KB926601)

I'm using the Visual Studio IDE

(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).

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.

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.

[cpp][/cpp]
[cpp]#include 

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]

I then created another Project where I created a DLL version of the C code above.

I simply want to call this from a simple FORTRAN rountine such as

program Forcall
implicit none
call testCdll
end program Forcall

I created this program in its own Project.

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.

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.

I've also put the name of the C dll in the "Additional Dependencies" box in the INPUT dialog box under "Linker".

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.

Mike

0 Kudos
4 Replies
DavidWhite
Valued Contributor II
763 Views
Mike,
Noticed you had got no responses. I;m not a C programmer, so hope I don't mislead you.
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.
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.
The only remaining issue then would be getting the arguments correct, as you already mentioned.
Regards.
David

0 Kudos
anthonyrichards
New Contributor III
763 Views
You must add something like
extern "C" __declspec(dllimport) void __stdcall testCdll( );
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.
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
!DEC$ ATTRIBUTES DLLIMPORT :: testCdll
!DEC$ ATTRIBUTES STDCALL :: testCdll
!DEC$ ATTRIBUTES ALIAS:'_testCdll@0':: testCdll
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.
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)

0 Kudos
Steve_Nuchia
New Contributor I
763 Views

The compiler ships with sample C / FORTRAN mixed projects, exploring those has been a big help for me.

0 Kudos
sabur
Beginner
763 Views
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).

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.

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.

Thanks. Every littlel bit helps

Onward.

Mike

0 Kudos
Reply