-1

I'm working with a third party C library that is using the default (__cdecl) calling convention and I need to be able to call the third party library from FORTRAN. FORTRAN's default calling convention is __stdcall and so I've written a basic wrapper in C++

int __stdcall wrapper_sub(char* ver, int* days)
{
   return original_sub(ver,days);
};

with function prototype

int   __stdcall wrapper_sub(char*, int*);

I'm able to compile without errors but when I check the symbol table of the wrapper library I get

?wrapper_sub@@YAHOADPAH@Z(int __cdecl original_sub(char *, int *))

It would seem that the linker is not finding original_sub and the process is failing silently. I've included all necessary references and directories into the project so that the wrapper solution should correctly resolve the third party API.

The FORTRAN call is made as follows

PROGRAM MAIN
IMPLICIT REAL*8 (A-H, O-Z)
CHARACTER*(4)        VER

VER = '1.0'
ISTAT = wrapper_sub(VER,IDAYS)

STOP
END

after including an external reference to the wrapper library, I get the following error

error LNK2001: unresolved external symbol _WRAPPER_SUB@12

Is there something I'm missing? or is there a another (less error prone?) way to wrap the C API so that I can call it from FORTRAN?

  • Show the FORTRAN call, too, please. And how do you know it is "failing silently?" What are the symptoms that let you know it failed? – Dale Wilson Jun 11 '14 at 16:55
  • @ Dale: I should explain that by "failing silently" I mean that the VS solution for the c++ lib project is compiling and building without warnings or errors. – user3730844 Jun 11 '14 at 17:06

1 Answers1

0

The linker can't link your FORTRAN code to the library because the function name is mangled ( C++ thinks the function is named ?wrapper_sub@@YAHOADPAH@Z, FORTRAN is expecting it to be named wrapper_sub)

Write your wrapper in C, not C++, or add an extern "C" to fix the problem.

extern "C" int __stdcall wrapper_sub(char* ver, int* days)

See this SO question for more details: In C++ source, what is the effect of extern "C"?

Community
  • 1
  • 1
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • Gotcha, I had accidentally commented out the #ifdef __cplusplus extern "C" { #endif and closing bracket macro – user3730844 Jun 11 '14 at 17:27
  • thanks a lot for the help Dale! I'm now back to my original problem (not referenced in this question), should I start a new question or should I just post the new error message in an edit? – user3730844 Jun 11 '14 at 17:29
  • It is not correct to say "FORTRAN's default calling convention is stdcall". Some Fortran compilers use that as a default (CVF, MS PowerStation), others do not. Pretty much all of the Windows Fortran compilers have options and syntax to let you choose. Which Fortran compiler are you using? (Name and version, please.) – Steve Lionel Jun 11 '14 at 17:33
  • Hey Steve, sounds reasonable. For some reason I remember reading that, though it is more likely that what I read was particular to a FORTRAN compiler....anyways, I'm working with two compilers: Powerstation 4.0 and Intel(R) Visual Fortran Composer XE 2013 SP1 Update 3 Integration for Microsoft Visual Studio* 2012, 14.0.0092.11. That last one is long winded but accurate :) – user3730844 Jun 11 '14 at 17:39
  • @user3730844 If it's a different problem, start a new question with a descriptive title. The purpose of SO is not so much to help you as it is to help the next 100 or so people who run into the same problem. (Oh and search so see if the question has already been asked, but you knew that.) – Dale Wilson Jun 11 '14 at 22:11