VC ++++AndFortranHybrid programming is generated by using FortranDLLProceed
(Use C's default Address Transfer Method for function parameter transfer)
1. generate DLL in Fortran
Create a Fortran DLL program test1.f
Add the following code:
! Test1.f90
!
! FUNCTIONS/SUBROUTINES exported from test1.dll:
! Test1-subroutine
! Examples of child routines without return values
Subroutine test1 (a, B)
! Expose subroutine test1 to users of this DLL
!
! DEC $ attributes c, DLLEXPORT: test1
! Variables
! Body of test1
Integer a, B
Integer sum
Sum = a + B
Return
End subroutine test1
! In this example, four integer arithmetic operations are returned.
! Add two numbers
Function add (a, B)
Implicit none
! DEC $ attributes c, DLLEXPORT: add
Integer a, B, add
Add = a + B
Return
End
! Subtraction of two numbers
Function abstract (a, B)
Implicit none
! DEC $ attributes c, DLLEXPORT: abstract
Integer a, B, abstract
Abstract = a-B
Return
End
! Multiply two numbers
Function multiply (a, B)
Implicit none
! DEC $ attributes c, DLLEXPORT: multiply
Integer a, B, multiply
Multiply = a * B
Return
End
! Division of two numbers (you need to add a judgment that determines whether the divisor is 0 and whether the Division can be completed)
Function divided (a, B)
Implicit none
! DEC $ attributes c, DLLEXPORT: divided
Integer a, B, divided
Divided = a/B
Return
End
After compilation, files such as test1.dll and test1.obj are generated. These two files are required for calling in VC.
Shows the functions generated in test1.dll.
Note: Use a pseudo-Comment statement! After DEC $ attributes c, DLLEXPORT: functionName, the generated function name is the same as the function name defined in Fortran, and the function name is not changed to larger according to the default attribute of the Fortran compiler, as shown in. Make sure that the name of the called function is consistent during subsequent VC calls. Otherwise, an error message indicating that the function cannot be found is displayed.
2. The VC console calls the DLL generated by Fortra.
Create a VC console application and a project named checktest1.
Note: You need to add the test1.dll and test1.obj files generated in the previous step in the file dialog box of the add to project Sub-menu under the project menu of the project. Otherwise, the compilation will pass and the link will fail. You also need to copy the above two files to the debug directory of the checktest1 project. Otherwise, an error message is displayed when the file cannot be found during running. I tested it myself. The above two steps are required.
Add the following code: (pay attention to the red part)
# Include "stdafx. h"
# Include "iostream. h"
// Extern "C" {_ stdcall TEST1 (int * x, int * y );}
// Extern "C" {_ stdcall ADD (int * x, int * y );}
// Extern "C" {_ stdcall ABSTRACT (int * x, int * y );}
// Extern "C" {_ stdcall MULTIPLY (int * x, int * y );}
// Extern "C" {_ stdcall DIVIDED (int * x, int * y );}
// Note that the function name must be the same as that when the DLL is generated (in the blue section below). Otherwise, the function cannot be found. And remember to remove the pointer symbol * in the parameter *.
Extern "C" {_ cdecl test1 (int x, int y );}
Extern "C" {_ cdecl add (int x, int y );}
// Change _ stdcall to _ cdecl if the value is passed to C.
// Add the C call method at the corresponding Fortran DLL! DEC $ attributes dllexport: add :! DEC $ attributes c, DLLEXPORT: add
// Adapt to pseudo comments! DEC $ attributes c, DLLEXPORT: The DLL function generated after add only contains the function named add. Neither ADD nor _ ADD @ 8 exists. For more information, see the DLL function name in
Extern "C" {_ cdecl abstract (int x, int y );}
Extern "C" {_ cdecl multiply (int x, int y );}
Extern "C" {_ cdecl divided (int x, int y );}
Int main (int argc, char * argv [])
{
Int a = 35, B = 5;
Int sum = 0;
Int abs = 0;
Int mul = 0;
Int div = 0;
// TEST1 (& a, & B );
// Sum = ADD (& a, & B );
// Abs = ABSTRACT (& a, & B );
// Mul = MULTIPLY (& a, & B );
// Div = DIVIDED (& a, & B );
Test1 (a, B );
Sum = add (a, B );
Abs = abstract (a, B );
Mul = multiply (a, B );
Div = divided (a, B );
Printf ("a + B = % dn", sum );
Printf ("a-B = % dn", abs );
Printf ("a * B = % dn", mul );
Printf ("a/B = % dn", div );
Printf ("Hello World! N ");
Return 0;
}
Then compile and run the command to get the correct result: