c calling a fortran subroutine

G

Guest

I am developing a c program in Visual Studio .NET 2003. I also have an
Intel(R) Fortran compiler for MVS .NET

My fortran sourcecode already existed. I started a new fortran project and
chose to create a dynamic link library. The beginning of the fortran code
looks like:

SUBROUTINE SFTCK3
!DEC$ ATTRIBUTES DLLEXPORT::SFTCK3

It compiled ok, and created both a SFTCK3.lib and SFTCK3.dll

In the C code, I've tried a variety of online suggestions, but for now it
looks like:
(from top)

#include <stdio.h>
#include <process.h>
#include <iostream.h>
#include "SelectOp.h"
#include "SelectOpGui.h"

extern void _stdcall SFTCK3 (void);

later on, in the code, i call:

case 8:

SFTCK3();
break;

I get the following error when trying to build:

error LNK2019: unresolved external symbol _SFTCK3@0 referenced in function
_SelectOp_OnOperation, which would be the call made above.

In the c codes' property settings, i do have, under additional dependencies:

c:\Temp\FAST_CSCI\SFTCK3\SFTCK3.lib

I'm pretty sure it's because I'm not linking the c and fortran together, but
not sure how to get this working. Any help would be appreciated. Thanks
 
C

Carl Daniel [VC++ MVP]

RichN said:
I am developing a c program in Visual Studio .NET 2003. I also have an
Intel(R) Fortran compiler for MVS .NET

My fortran sourcecode already existed. I started a new fortran
project and chose to create a dynamic link library. The beginning of
the fortran code looks like:

SUBROUTINE SFTCK3
!DEC$ ATTRIBUTES DLLEXPORT::SFTCK3

It compiled ok, and created both a SFTCK3.lib and SFTCK3.dll

In the C code, I've tried a variety of online suggestions, but for
now it looks like:
(from top)

#include <stdio.h>
#include <process.h>
#include <iostream.h>
#include "SelectOp.h"
#include "SelectOpGui.h"

extern void _stdcall SFTCK3 (void);

later on, in the code, i call:

case 8:

SFTCK3();
break;

I get the following error when trying to build:

error LNK2019: unresolved external symbol _SFTCK3@0 referenced in
function _SelectOp_OnOperation, which would be the call made above.

In the c codes' property settings, i do have, under additional
dependencies:

c:\Temp\FAST_CSCI\SFTCK3\SFTCK3.lib

I'm pretty sure it's because I'm not linking the c and fortran
together, but not sure how to get this working. Any help would be
appreciated. Thanks

Use dumpbin /symbols on your STFCK3.lib import library to see what the
exported symbol name is. That'll give a clue to the solution (which
probably involves changing the name you use in the C code, or in the Fortran
code, or changing calling convention, or some such).

-cd
 
G

Guest

Thanks for reply Carl. I was never able to get dumpbin.exe to work on my PC,
so had colleague run it on his. After running dumpbin on my SFTCK3.lib, I got
the following:

File Type: LIBRARY

COFF SYMBOL TABLE
000 005D0C05 ABS notype Static | @COMP.ID
001 00000000 SECT2 notype External | __IMPORT_DESCRIPTOR_SFTCK3
002 C0000040 SECT2 notype Section | .idata$2
003 00000000 SECT3 notype Static | .idata$6
004 C0000040 UNDEF notype Section | .idata$4
005 C0000040 UNDEF notype Section | .idata$5
006 00000000 UNDEF notype External | __NULL_IMPORT_DESCRIPTOR
007 00000000 UNDEF notype External | |SFTCK3_NULL_THUNK_DATA

String Table Size = 0x50 bytes

COFF SYMBOL TABLE
000 005D0C05 ABS notype Static | @comp.id
001 00000000 SECT2 notype External | __NULL_IMPORT_DESCRIPTOR

String Table Size = 0x1D bytes

COFF SYMBOL TABLE
000 005D0C05 ABS notype Static | @comp.id
001 00000000 SECT2 notype External | |SFTCK3_NULL_THUNK_DATA

String Table Size = 0x1C bytes

Summary

C3 .debug$S
14 .idata$2
14 .idata$3
4 .idata$4
4 .idata$5
C .idata$6

I checked out many online sites to try and understand what this is all
telling me. I generally have an idea now about the fields, but can't
translate that into how it effects my particular problem. Can you help on
that? Also, would have running dumpbin /EXPORTS helped me to know what
subroutine name the dll is showing?
Thanks again for your earlier reply.
 
C

Carl Daniel [VC++ MVP]

RichN said:
Thanks for reply Carl. I was never able to get dumpbin.exe to work on
my PC, so had colleague run it on his. After running dumpbin on my
SFTCK3.lib, I got the following:

File Type: LIBRARY

COFF SYMBOL TABLE
000 005D0C05 ABS notype Static | @COMP.ID
001 00000000 SECT2 notype External |
__IMPORT_DESCRIPTOR_SFTCK3 002 C0000040 SECT2 notype Section
| .idata$2 003 00000000 SECT3 notype Static | .idata$6
004 C0000040 UNDEF notype Section | .idata$4
005 C0000040 UNDEF notype Section | .idata$5
006 00000000 UNDEF notype External |
__NULL_IMPORT_DESCRIPTOR 007 00000000 UNDEF notype External
| |SFTCK3_NULL_THUNK_DATA

String Table Size = 0x50 bytes

COFF SYMBOL TABLE
000 005D0C05 ABS notype Static | @comp.id
001 00000000 SECT2 notype External | __NULL_IMPORT_DESCRIPTOR

String Table Size = 0x1D bytes

COFF SYMBOL TABLE
000 005D0C05 ABS notype Static | @comp.id
001 00000000 SECT2 notype External | |SFTCK3_NULL_THUNK_DATA

String Table Size = 0x1C bytes

Summary

C3 .debug$S
14 .idata$2
14 .idata$3
4 .idata$4
4 .idata$5
C .idata$6

I checked out many online sites to try and understand what this is all
telling me. I generally have an idea now about the fields, but can't
translate that into how it effects my particular problem. Can you
help on that? Also, would have running dumpbin /EXPORTS helped me to
know what subroutine name the dll is showing?
Thanks again for your earlier reply.

Odd - it doesn't look like your function was exported from the DLL.

Yes, try running dumpbin /exports against the DLL to see what it shows.

-cd
 
G

Guest

hi Carl,

I am able, now, to run dumpbin at my PC. In any case, I ran /EXPORTS, and
got the following:

Dump of file sftck3.lib
File Type: LIBRARY
Exports
ordinal name
_SFTCK3

Summary
C3 .debug$S
14 .idata$2
14 .idata$3
4 .idata$4
4 .idata$5
C .idata$6

I checked this against a known working DLL I previously created in C, and
the output
is similar. So now I'm thinking that the subroutine name is there, but that
the original error "unresolved external symbol _SFTCK3@0" is indicating the
function name is undefined to the function that is calling it. Normally, in
C, I would have some kind of #include "xxxxxxxx" to define SFTCK3. Do I need
some type of .h to go with my fortran dll?
 
C

Carl Daniel [VC++ MVP]

RichN said:
hi Carl,

I am able, now, to run dumpbin at my PC. In any case, I ran /EXPORTS,
and got the following:

Dump of file sftck3.lib
File Type: LIBRARY
Exports
ordinal name
_SFTCK3

Summary
C3 .debug$S
14 .idata$2
14 .idata$3
4 .idata$4
4 .idata$5
C .idata$6

I checked this against a known working DLL I previously created in C,
and the output
is similar. So now I'm thinking that the subroutine name is there,
but that the original error "unresolved external symbol _SFTCK3@0" is
indicating the function name is undefined to the function that is
calling it. Normally, in C, I would have some kind of #include
"xxxxxxxx" to define SFTCK3. Do I need some type of .h to go with my
fortran dll?

OK, so your C code is expecting a function with the normal __stdcall name
decorations (_name@paramSize), while your Fortran DLL exported only a simple
name.

Are you sure that the Fortran code is __stdcall? It's named as if it's
__cdecl. Try changing your extern declaration in the C module to use
__cdecl. That'll probably make it link - if the Fortran function doesn't
actually __cdecl, then the mismatch will likely cause the program to crash
shortly after returning from the Fortran sub.

-cd
 
G

Guest

Hi Carl,

Just wanted to get back to you. cdecl worked ! I was able to compile and
link, and the program seems to be running ok. Thanks for advice !

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top