By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

How to link Visual Studio C to Fortran DLL?

P: 2
here is my code:
Visual C:
Expand|Select|Wrap|Line Numbers
  1. **********************************
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. extern __declspec(dllimport) void _stdcall abcd(float *aa , float *bb , float *cc , float *dd); 
  5. void main (void)
  6.     float aa=1.0;
  7.     float bb=2.0;
  8.     float cc=3.0;
  9.     float dd;
  10.     abcd(&aa,&bb,&cc,&dd);
  11.     printf("dd=%f",dd);
  12.     return;
  13. }
  14. *******************************
fortran :
Expand|Select|Wrap|Line Numbers
  1. *******************************
  2.       SUBROUTINE abcd(aa,bb,cc,dd)
  5.        REAL aa                
  6.        REAL bb                
  7.        REAL cc               
  8.        REAL dd               
  9.        dd=( aa+bb)*cc
  10.        RETURN
  11.        END  
  13. ************************
It`s ok to compile these codes together.
but I can not get the right answer after debug, the data seems no connection with fortran dll.
Here are the outputs:
dd -1.0737418e+008 float
bb 2.0000000 float
cc 3.0000000 float
aa 1.0000000 float
can somebody tell me what`s wrong with it?
thank you very much.
Dec 1 '10 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 5K+
P: 9,197
Have you made a library containing your abcd function?

You add that library to your FORTRAN link.
Dec 1 '10 #2

Expert 100+
P: 983
David Hsu,

I've done things like this before, and you have to be very careful about providing the correct argument list to FORTRAN.

My first question is if your program is running at all, and it seemst to be, thus I expect that you got the mechanics of the linkage correct.

Since that's working, you may need to look in your FORTRAN compiler's manuals to see what the calling conventions are. You might be surprised at just how convoluted things can get. Especially with

The next thing I'd try is to return a constant value from your FORTRAN subroutine. It's an easy test, and it will let you triage the problem rapidly.

Also, in the same pass, you might try using FORTRAN I/O to print out the values you recieved from C. If they aren't passed in correctly, then the return value is pointless. This, of course, depends on the ability to link the FORTRAN I/O libraries into your DLL - which may be problematic.

Of course, if the second test fails, you will have no choice but to go to the manual. Make sure the manual matches your FORTRAN implementation. When I had to deal with this problem, Microsoft's FORTRAN and Borland's FORTRAN had different calling conventions.

This sounds like a fun problem.

Good Luck!
Dec 1 '10 #3

P: 2
thanks a lot, it`s ok to write message on the screen for Fortran DLL. But the output still wrong for this code,now I consider about the using of :void main (void)
is this right for sneding input and output, or I should edit it?
thank you very muck
Dec 2 '10 #4

Expert 100+
P: 983
David Hsu,

In C++, the main function should always return an int.

Often this is managed by simply returning zero (success) at the end of function, with the use of exit calls having actuall error return codes.

Dec 2 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.