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

Call Fortran DLL from C

HKort
Débutant
1 954 Visites
Hi,
Can anyone give me (C newbie) a short example on how to create a Fortran DLL that I can call from C? I tried the vc-example from the Compaq website, but it does not compile. When I add to the Fortran code the line
!DEC$ ATTRIBUTES ALIAS: "_DLL_ROUT@20"::DLL_ROUT
and change the deefinitionline in theC code to
extern void _stdcall DLL_ROUT (,
the code compile, but the application gives an application error when called. I am just so desperate. Can anyone help???
Thanks,
Haje
0 Compliments
9 Réponses
Steven_L_Intel1
Employé
1 954 Visites
The example on the Compaq example does build and execute properly. I often find that when people have problems with the example, they have not copied it properly.
What compiler are you using - Compaq or Intel?
Did you name the C source with a .c or .cpp extension?
0 Compliments
HKort
Débutant
1 954 Visites

Steve,

the fortran compiler intel v8, the c main file is .c not .cpp. I use Visual Studio .NET 2003.

Thanks, Haje

0 Compliments
Steven_L_Intel1
Employé
1 954 Visites
Ok. Change the C code to look like this:
/* Main program written in C++ that calls a Fortran DLL */
#include 
#include 

/* Declare the Fortran routine. The following items are of note:
                        
   - The "C" attribute prevents C++ name mangling  Remove it
     if the file type is .c
   - The dllimport specification is required                   
   - Fortran character arguments have a hidden length
     argument at the end of the argument list
   - Routine name must be in uppercase to match Fortran.
*/
                        
extern  __declspec(dllimport) void DLL_ROUT (
    int *INT_ARG,
    char *STR_IN,
    char *STR_OUT,
    int STR_IN_LEN,
     int STR_OUT_LEN);
                        
void main (int argc, char *argv[])
    {
    char instring[40];
    char outstring[40];
    int intarg;
                        
    strcpy(instring,"Testing...");
    intarg = 123;
    /* Call Fortran routine - pass intarg by reference,
       pass length of outstring explicitly */
    DLL_ROUT(&intarg,instring,outstring,strlen(instring),40);
    printf("%s
",outstring);
    }

Note that I took the "C" out because the source file is C, took out the __stdcall and moved the string lengths to the end. The Fortran code doesn't need to change.

Message Edited by sblionel on 05-06-2004 01:26 PM

Message Edited by sblionel on 05-06-2004 02:01 PM

0 Compliments
HKort
Débutant
1 954 Visites

Steve,

man, you are great, that one worked! Now, I have an example to start working from. Once I have a few more issues ironed out, I can place an order for a permanent license.

Haje

0 Compliments
Steven_L_Intel1
Employé
1 954 Visites
You're welcome. I edited the comments above to properly reflect what Intel Fortran expects.
0 Compliments
kwei89
Débutant
1 954 Visites
You're welcome. I edited the comments above to properly reflect what Intel Fortran expects.

For the same example, I am having problem on 64 bit platform. First I can not use win32 for fortran projectfor visual studio says that win32 compiler is not installed. so i have change the FLIB project to x64 in order to compile. however i got link error when I tried to build C_USELIB project.

error LNK2019: unresolved extern symbol _FSUB referenced in function _main

C_USELIB is built using win32 and i seem to be unable to change it to x64.

i don't know what to do.
0 Compliments
Ron_Green
Modérateur
1 954 Visites
Quoting - kwei89

For the same example, I am having problem on 64 bit platform. First I can not use win32 for fortran projectfor visual studio says that win32 compiler is not installed. so i have change the FLIB project to x64 in order to compile. however i got link error when I tried to build C_USELIB project.

error LNK2019: unresolved extern symbol _FSUB referenced in function _main

C_USELIB is built using win32 and i seem to be unable to change it to x64.

i don't know what to do.

You will either need to get and install the 32bit version of the Intel compiler OR install the x64 components in Visual Studio. You cannot mix 32 bit and 64 bit objects.

adding x64 bits to VS: First make sure your Windows is the x64 version. Then put in your VS2005 or 2008 installation CD:

select "Change or Remove VS 2005"
select "Add or Remove Features"

Expand the Visual C++ group,
check the box next to 'x64 Compilers and Tools'

Add the 32bit compiler: download w_cprof_p_11.0.xxx_ia32.exe from https://registrationcenter.intel.com and install.
0 Compliments
kwei89
Débutant
1 954 Visites
Quoting - kwei89

For the same example, I am having problem on 64 bit platform. First I can not use win32 for fortran projectfor visual studio says that win32 compiler is not installed. so i have change the FLIB project to x64 in order to compile. however i got link error when I tried to build C_USELIB project.

error LNK2019: unresolved extern symbol _FSUB referenced in function _main

C_USELIB is built using win32 and i seem to be unable to change it to x64.

i don't know what to do.

You will either need to get and install the 32bit version of the Intel compiler OR install the x64 components in Visual Studio. You cannot mix 32 bit and 64 bit objects.

adding x64 bits to VS: First make sure your Windows is the x64 version. Then put in your VS2005 or 2008 installation CD:

select "Change or Remove VS 2005"
select "Add or Remove Features"

Expand the Visual C++ group,
check the box next to 'x64 Compilers and Tools'

Add the 32bit compiler: download w_cprof_p_11.0.xxx_ia32.exe from https://registrationcenter.intel.com and install.
Thanks for the help. I downloaded and installedw_cprof_p_11.0.074_novsppe.exe. I was able to compile the fortran project in win32 and to build and to run the sample solutionfor win32, a huge progress. but when i try to do the same thing for 64 bits, i got a lot of link errors complaining conflicted symbols between msvcrtd and libcmtd and some unresolved symbols from libifcore.lib (see below).

Help gain please.

Error 1 error LNK2005: _amsg_exit already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 2 error LNK2005: exit already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 3 error LNK2005: _cexit already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 4 error LNK2005: __xi_a already defined in MSVCRTD.lib(cinitexe.obj) libcmt.lib
Error 5 error LNK2005: __xi_z already defined in MSVCRTD.lib(cinitexe.obj) libcmt.lib
Error 6 error LNK2005: __xc_a already defined in MSVCRTD.lib(cinitexe.obj) libcmt.lib
Error 7 error LNK2005: __xc_z already defined in MSVCRTD.lib(cinitexe.obj) libcmt.lib
Error 8 error LNK2005: _XcptFilter already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 9 error LNK2005: signal already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 10 error LNK2005: raise already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 11 error LNK2005: _configthreadlocale already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 12 error LNK2005: _encode_pointer already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 13 error LNK2005: _encoded_null already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 14 error LNK2005: _decode_pointer already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 15 error LNK2005: realloc already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 16 error LNK2005: __set_app_type already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 17 error LNK2005: _unlock already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 18 error LNK2005: _lock already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 19 error LNK2005: _get_osfhandle already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 20 error LNK2005: _open_osfhandle already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 21 error LNK2005: _isctype already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 22 error LNK2005: _stricmp already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 23 error LNK2005: getenv already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 24 error LNK2005: tolower already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib
Error 25 error LNK2019: unresolved external symbol _pxcptinfoptrs referenced in function GETEXCEPTIONPTRSQQ libifcore.lib
Error 26 error LNK2019: unresolved external symbol errno referenced in function for__open_proc libifcore.lib
Error 27 error LNK2001: unresolved external symbol errno libifcore.lib
Error 28 error LNK2019: unresolved external symbol MAIN__ referenced in function main libifcore.lib
Error 29 fatal error LNK1120: 3 unresolved externals D:TesterCLR_FLIBx64DebugCLR_FLIB.exe
0 Compliments
Lorri_M_Intel
Employé
1 954 Visites

Make sure that you are consistent in the types of libraries you are linking against. A message like this one,

" tolower already defined in MSVCRTD.lib(MSVCR80D.dll) libcmt.lib"

says to me that you are linking against both the dynamic C runtime library (MSVCRTD) and the static, multi-threaded C runtime library (libcmt).

Look at both the C project and the Fortran project, make sure the library format matches. That is the most likely culprit.

- Lorri
0 Compliments
Répondre