VC + + calls Fortran-generated DLL instance

Source: Internet
Author: User
Tags abs abstract mul printf

  VC + + + and Fortran mixed programming with the help of Fortran-generated DLLs

(using the C default method to transfer function parameters)

1.Fortran Build DLL

New FORTRAN DLL Program TEST1.F

Add the following code:

! Test1.f90

! Functions/subroutines exported from Test1.dll:

! Test1-subroutine

! Example subroutine with no return value

Subroutine Test1 (A,B)

! Expose subroutine Test1 to the 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

! Example has a return value of an integer arithmetic

! Add two numbers

function Add (a,b)

Implicit none

! dec$ ATTRIBUTES C,dllexport::add

Integer A,b,add

Add=a+b

Return

End

! Subtract 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

! Divide by two numbers (you need to add a judgment that considers whether dividend is 0 and whether it is divisible)

function divided (a,b)

Implicit none

! dec$ ATTRIBUTES c,dllexport::divided

Integer a,b,divided

Divided=a/b

Return

End

Generates files such as test1.dll,test1.obj after compilation. These two files are required for us to invoke in VC.

See the functions generated in Test1.dll as shown in the following figure.

Note: Use pseudo comment statements! dec$ ATTRIBUTES C,dllexport::functionname, the resulting function name is consistent with the function name defined in FORTRAN and does not convert the function names to larger by the default properties of the Fortran compiler, as shown in the following figure. In subsequent VC calls to keep the call of the function name is consistent, or you can not find the function of the error hint.

2.VC Console calls Fortra-generated DLL

New VC Console application, create a new Checktest1 project.

Note: You need to add the Test1.dll,test1.obj two files from the previous step in the file dialog box of the Add to Project submenu under Project's Project menu, otherwise the compilation passes and the link fails. It is also necessary to copy the above two files to the debug directory of the Checktest1 project, otherwise there will be no error prompts to find the file when running. Test yourself, the above two steps are necessary.

Add the following code: (Note 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 is to be consistent with the DLL when it is generated (the blue section below), otherwise there will be an error indicating that the function could not be found. And be sure to remove the pointer symbol * from the parameter.

extern "C" {_cdecl test1 (int x,int y);}

extern "C" {_cdecl add (int x,int y);}

Use C to pass the value way, then need to change _stdcall to _cdecl

The corresponding Fortran DLL to add C to call the way, is about to! dec$ ATTRIBUTES Dllexport::add modified to:! dec$ ATTRIBUTES C,dllexport::add

adapt to pseudo annotation! The function named add is only present in the DLL function generated after dec$ ATTRIBUTES C,dllexport::add, and neither add nor _add@8 exists, see the DLL function name in the figure above

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 the compile run can produce the correct result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.