4 个解决方案
#1
Calling Fortran routines from C++
Example-1: Calling routines and functions
The following sample shows how Fortran routines and functions can be called from a C++ program.
(1) The C++ file:
// This illustrates how a Fortran routine and function may be
// called from a main program in C++
#include <iostream.h>
extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}
int main()
{
int n=10,nSq,nCube;
FR1(&n,&nSq);
cout << "The square is:" << nSq << endl;
nCube=FF1(&n);
cout << "The Cube is:" << nCube << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M)
C COMPUTES THE SQUARE OF N, RETURNS IN M
M=N*N
RETURN
END
C
INTEGER FUNCTION FF1(N)
C COMPUTES THE CUBE OF N
FF1=N*N*N
RETURN
END
Back to Top
Example-2: Passing C char string to a Fortran routine
The following sample shows how a C char string may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// This illustrates how a Fortran routine may be
// called from a main program in C++, and a char[] string passed to it
#include <iostream.h>
#include <string.h>
extern "C"
{
void __stdcall FR1(int *,int *,char *);
}
int main()
{
int n=10,nSq;
char szCtest[20];
strcpy(szCtest,"teststring");
FR1(&n,&nSq,szCtest);
cout << "The square is:" << nSq << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M,CSTR)
INTEGER*4 CSTR(1)
C HERE WE RECEIVE THE C CHAR STRING IN AN INTEGER ARRAY
C COULD ALSO HAVE USED A BYTE ARRAY
M=N*N
WRITE(6,20) (CSTR(L),L=1,3)
20 FORMAT(' CSTR=',3A4)
WRITE(6,*) 'DONE'
RETURN
END
Back to Top
Example-3: Passing arrays to a Fortran routine
The following sample shows how arrays may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// Illustrate passing integer and floating point arrays
// from C++ to Fortran
#include <iostream.h>
extern "C"
{
int __stdcall SUMIT(int *,int*);
float __stdcall MEAN(float*,int*);
}
int main()
{
int iA[]={3,5,6,7,2,3,4,5,11,7},iN=10,iSum;
float fpA[]={1.2f,3.f,44.f,2.5f,-1.3f,33.44f,5.f,0.3f,-3.6f,24.1f},fpMean;
iSum=SUMIT(iA,&iN);
cout << "The Sum of iA is:" << iSum << endl;
fpMean=MEAN(fpA,&iN);
cout << "The Mean of fpA is:" << fpMean << endl;
return 0;
}
(2) The Fortran File:
INTEGER FUNCTION SUMIT(IA,N)
INTEGER IA(1)
ISUM=0
DO 50 J=1,N
50 ISUM=ISUM+IA(J)
SUMIT=ISUM
RETURN
END
C
REAL FUNCTION MEAN(RA,N)
REAL RA(1)
SUM=0.
DO 50 J=1,N
50 SUM=SUM+RA(J)
IF(N.GT.0) MEAN=SUM/FLOAT(N)
RETURN
END
Back to Top
Calling C++ routines from Fortran
The following examples work with Microsoft Visual C++ and Compaq Visual Fortran. Your mileage may vary on other systems.
Example-1: Calling routines and functions
The following sample shows how C++ routines and functions can be called from a Fortran program.
(1) The Fortran file:
INTEGER CR2
N=10
CALL CR1(N,M)
WRITE(6,20) N,M
20 FORMAT(' The square of',I3,' is',I4)
K=CR2(N)
WRITE(6,30) N,K
30 FORMAT(' The cube of',I3,' is',I15)
CALL EXIT
END
(2) The C++ files:
extern "C"
{
void __stdcall CR1(int *,int *);
int __stdcall CR2(int *);
}
void __stdcall CR1(int *n, int *m)
{
// Compute the square of n, return in m
int k;
k=*n;
*m=k*k;
return;
}
int __stdcall CR2(int *n)
// Compute the cube of n
{
int m,k;
k=*n;
m=k*k*k;
return m;
}
Back to Top
Example-1: Calling routines and functions
The following sample shows how Fortran routines and functions can be called from a C++ program.
(1) The C++ file:
// This illustrates how a Fortran routine and function may be
// called from a main program in C++
#include <iostream.h>
extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}
int main()
{
int n=10,nSq,nCube;
FR1(&n,&nSq);
cout << "The square is:" << nSq << endl;
nCube=FF1(&n);
cout << "The Cube is:" << nCube << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M)
C COMPUTES THE SQUARE OF N, RETURNS IN M
M=N*N
RETURN
END
C
INTEGER FUNCTION FF1(N)
C COMPUTES THE CUBE OF N
FF1=N*N*N
RETURN
END
Back to Top
Example-2: Passing C char string to a Fortran routine
The following sample shows how a C char string may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// This illustrates how a Fortran routine may be
// called from a main program in C++, and a char[] string passed to it
#include <iostream.h>
#include <string.h>
extern "C"
{
void __stdcall FR1(int *,int *,char *);
}
int main()
{
int n=10,nSq;
char szCtest[20];
strcpy(szCtest,"teststring");
FR1(&n,&nSq,szCtest);
cout << "The square is:" << nSq << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M,CSTR)
INTEGER*4 CSTR(1)
C HERE WE RECEIVE THE C CHAR STRING IN AN INTEGER ARRAY
C COULD ALSO HAVE USED A BYTE ARRAY
M=N*N
WRITE(6,20) (CSTR(L),L=1,3)
20 FORMAT(' CSTR=',3A4)
WRITE(6,*) 'DONE'
RETURN
END
Back to Top
Example-3: Passing arrays to a Fortran routine
The following sample shows how arrays may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// Illustrate passing integer and floating point arrays
// from C++ to Fortran
#include <iostream.h>
extern "C"
{
int __stdcall SUMIT(int *,int*);
float __stdcall MEAN(float*,int*);
}
int main()
{
int iA[]={3,5,6,7,2,3,4,5,11,7},iN=10,iSum;
float fpA[]={1.2f,3.f,44.f,2.5f,-1.3f,33.44f,5.f,0.3f,-3.6f,24.1f},fpMean;
iSum=SUMIT(iA,&iN);
cout << "The Sum of iA is:" << iSum << endl;
fpMean=MEAN(fpA,&iN);
cout << "The Mean of fpA is:" << fpMean << endl;
return 0;
}
(2) The Fortran File:
INTEGER FUNCTION SUMIT(IA,N)
INTEGER IA(1)
ISUM=0
DO 50 J=1,N
50 ISUM=ISUM+IA(J)
SUMIT=ISUM
RETURN
END
C
REAL FUNCTION MEAN(RA,N)
REAL RA(1)
SUM=0.
DO 50 J=1,N
50 SUM=SUM+RA(J)
IF(N.GT.0) MEAN=SUM/FLOAT(N)
RETURN
END
Back to Top
Calling C++ routines from Fortran
The following examples work with Microsoft Visual C++ and Compaq Visual Fortran. Your mileage may vary on other systems.
Example-1: Calling routines and functions
The following sample shows how C++ routines and functions can be called from a Fortran program.
(1) The Fortran file:
INTEGER CR2
N=10
CALL CR1(N,M)
WRITE(6,20) N,M
20 FORMAT(' The square of',I3,' is',I4)
K=CR2(N)
WRITE(6,30) N,K
30 FORMAT(' The cube of',I3,' is',I15)
CALL EXIT
END
(2) The C++ files:
extern "C"
{
void __stdcall CR1(int *,int *);
int __stdcall CR2(int *);
}
void __stdcall CR1(int *n, int *m)
{
// Compute the square of n, return in m
int k;
k=*n;
*m=k*k;
return;
}
int __stdcall CR2(int *n)
// Compute the cube of n
{
int m,k;
k=*n;
m=k*k*k;
return m;
}
Back to Top
#2
楼上的写的好详细呀 学习
#3
他写的只是Windows下的,Linux下完全不是这个样子,但还是表扬以下!
#4
lz,我必须说一句,你是好人。。。
#1
Calling Fortran routines from C++
Example-1: Calling routines and functions
The following sample shows how Fortran routines and functions can be called from a C++ program.
(1) The C++ file:
// This illustrates how a Fortran routine and function may be
// called from a main program in C++
#include <iostream.h>
extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}
int main()
{
int n=10,nSq,nCube;
FR1(&n,&nSq);
cout << "The square is:" << nSq << endl;
nCube=FF1(&n);
cout << "The Cube is:" << nCube << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M)
C COMPUTES THE SQUARE OF N, RETURNS IN M
M=N*N
RETURN
END
C
INTEGER FUNCTION FF1(N)
C COMPUTES THE CUBE OF N
FF1=N*N*N
RETURN
END
Back to Top
Example-2: Passing C char string to a Fortran routine
The following sample shows how a C char string may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// This illustrates how a Fortran routine may be
// called from a main program in C++, and a char[] string passed to it
#include <iostream.h>
#include <string.h>
extern "C"
{
void __stdcall FR1(int *,int *,char *);
}
int main()
{
int n=10,nSq;
char szCtest[20];
strcpy(szCtest,"teststring");
FR1(&n,&nSq,szCtest);
cout << "The square is:" << nSq << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M,CSTR)
INTEGER*4 CSTR(1)
C HERE WE RECEIVE THE C CHAR STRING IN AN INTEGER ARRAY
C COULD ALSO HAVE USED A BYTE ARRAY
M=N*N
WRITE(6,20) (CSTR(L),L=1,3)
20 FORMAT(' CSTR=',3A4)
WRITE(6,*) 'DONE'
RETURN
END
Back to Top
Example-3: Passing arrays to a Fortran routine
The following sample shows how arrays may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// Illustrate passing integer and floating point arrays
// from C++ to Fortran
#include <iostream.h>
extern "C"
{
int __stdcall SUMIT(int *,int*);
float __stdcall MEAN(float*,int*);
}
int main()
{
int iA[]={3,5,6,7,2,3,4,5,11,7},iN=10,iSum;
float fpA[]={1.2f,3.f,44.f,2.5f,-1.3f,33.44f,5.f,0.3f,-3.6f,24.1f},fpMean;
iSum=SUMIT(iA,&iN);
cout << "The Sum of iA is:" << iSum << endl;
fpMean=MEAN(fpA,&iN);
cout << "The Mean of fpA is:" << fpMean << endl;
return 0;
}
(2) The Fortran File:
INTEGER FUNCTION SUMIT(IA,N)
INTEGER IA(1)
ISUM=0
DO 50 J=1,N
50 ISUM=ISUM+IA(J)
SUMIT=ISUM
RETURN
END
C
REAL FUNCTION MEAN(RA,N)
REAL RA(1)
SUM=0.
DO 50 J=1,N
50 SUM=SUM+RA(J)
IF(N.GT.0) MEAN=SUM/FLOAT(N)
RETURN
END
Back to Top
Calling C++ routines from Fortran
The following examples work with Microsoft Visual C++ and Compaq Visual Fortran. Your mileage may vary on other systems.
Example-1: Calling routines and functions
The following sample shows how C++ routines and functions can be called from a Fortran program.
(1) The Fortran file:
INTEGER CR2
N=10
CALL CR1(N,M)
WRITE(6,20) N,M
20 FORMAT(' The square of',I3,' is',I4)
K=CR2(N)
WRITE(6,30) N,K
30 FORMAT(' The cube of',I3,' is',I15)
CALL EXIT
END
(2) The C++ files:
extern "C"
{
void __stdcall CR1(int *,int *);
int __stdcall CR2(int *);
}
void __stdcall CR1(int *n, int *m)
{
// Compute the square of n, return in m
int k;
k=*n;
*m=k*k;
return;
}
int __stdcall CR2(int *n)
// Compute the cube of n
{
int m,k;
k=*n;
m=k*k*k;
return m;
}
Back to Top
Example-1: Calling routines and functions
The following sample shows how Fortran routines and functions can be called from a C++ program.
(1) The C++ file:
// This illustrates how a Fortran routine and function may be
// called from a main program in C++
#include <iostream.h>
extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}
int main()
{
int n=10,nSq,nCube;
FR1(&n,&nSq);
cout << "The square is:" << nSq << endl;
nCube=FF1(&n);
cout << "The Cube is:" << nCube << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M)
C COMPUTES THE SQUARE OF N, RETURNS IN M
M=N*N
RETURN
END
C
INTEGER FUNCTION FF1(N)
C COMPUTES THE CUBE OF N
FF1=N*N*N
RETURN
END
Back to Top
Example-2: Passing C char string to a Fortran routine
The following sample shows how a C char string may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// This illustrates how a Fortran routine may be
// called from a main program in C++, and a char[] string passed to it
#include <iostream.h>
#include <string.h>
extern "C"
{
void __stdcall FR1(int *,int *,char *);
}
int main()
{
int n=10,nSq;
char szCtest[20];
strcpy(szCtest,"teststring");
FR1(&n,&nSq,szCtest);
cout << "The square is:" << nSq << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M,CSTR)
INTEGER*4 CSTR(1)
C HERE WE RECEIVE THE C CHAR STRING IN AN INTEGER ARRAY
C COULD ALSO HAVE USED A BYTE ARRAY
M=N*N
WRITE(6,20) (CSTR(L),L=1,3)
20 FORMAT(' CSTR=',3A4)
WRITE(6,*) 'DONE'
RETURN
END
Back to Top
Example-3: Passing arrays to a Fortran routine
The following sample shows how arrays may be passed from a C++ program to a Fortran routine.
(1) The C++ file:
// Illustrate passing integer and floating point arrays
// from C++ to Fortran
#include <iostream.h>
extern "C"
{
int __stdcall SUMIT(int *,int*);
float __stdcall MEAN(float*,int*);
}
int main()
{
int iA[]={3,5,6,7,2,3,4,5,11,7},iN=10,iSum;
float fpA[]={1.2f,3.f,44.f,2.5f,-1.3f,33.44f,5.f,0.3f,-3.6f,24.1f},fpMean;
iSum=SUMIT(iA,&iN);
cout << "The Sum of iA is:" << iSum << endl;
fpMean=MEAN(fpA,&iN);
cout << "The Mean of fpA is:" << fpMean << endl;
return 0;
}
(2) The Fortran File:
INTEGER FUNCTION SUMIT(IA,N)
INTEGER IA(1)
ISUM=0
DO 50 J=1,N
50 ISUM=ISUM+IA(J)
SUMIT=ISUM
RETURN
END
C
REAL FUNCTION MEAN(RA,N)
REAL RA(1)
SUM=0.
DO 50 J=1,N
50 SUM=SUM+RA(J)
IF(N.GT.0) MEAN=SUM/FLOAT(N)
RETURN
END
Back to Top
Calling C++ routines from Fortran
The following examples work with Microsoft Visual C++ and Compaq Visual Fortran. Your mileage may vary on other systems.
Example-1: Calling routines and functions
The following sample shows how C++ routines and functions can be called from a Fortran program.
(1) The Fortran file:
INTEGER CR2
N=10
CALL CR1(N,M)
WRITE(6,20) N,M
20 FORMAT(' The square of',I3,' is',I4)
K=CR2(N)
WRITE(6,30) N,K
30 FORMAT(' The cube of',I3,' is',I15)
CALL EXIT
END
(2) The C++ files:
extern "C"
{
void __stdcall CR1(int *,int *);
int __stdcall CR2(int *);
}
void __stdcall CR1(int *n, int *m)
{
// Compute the square of n, return in m
int k;
k=*n;
*m=k*k;
return;
}
int __stdcall CR2(int *n)
// Compute the cube of n
{
int m,k;
k=*n;
m=k*k*k;
return m;
}
Back to Top
#2
楼上的写的好详细呀 学习
#3
他写的只是Windows下的,Linux下完全不是这个样子,但还是表扬以下!
#4
lz,我必须说一句,你是好人。。。