C++调用fortran生成的dll文件的问题求助
C++主程序代码以及待调用的fortran代码都在图1中所示,步骤为:首先生成fortran代码对应的TEST.dll和TEST.lib文件;加入到C++的工程中;编译总是出现图中的错误,请教各位这是什么原因请以文本(而非截图)的方式,贴出 C++ 代码和 fortran 代码。 C++源代码如下:
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <msclr\marshal_cppstd.h>
#include "float.h"
#include <string>
#include <math.h>
#include <cstring>
#include <vector>
#include <istream>
#include <fstream>
using namespace msclr::interop;
using namespace std;
extern "C" {
void __stdcall GETSTRING( char *A,
int LEN);
}
void main()
{
char STR[]="hello";
GETSTRING(STR,strlen(STR));
return;
}
fortran代码如下:
subroutine getstring(a)
!DEC$ IF DEFINED (_DLL)
!DEC$ ATTRIBUTES DLLEXPORT ::GETSTRING
!DEC$ END IF
implicit none
character(len=*)::a
write(*,*)a
return
end subroutine
fortran编译器为Intel Parallel Studio XE 2013,C++编译器为VS2012 有没有可能是fortran编译器版本的问题呢? 建议你用标准的混编方法:ISO_C_Binding
不但在 VS+IVF 可以用,在其他编译器(gcc,gfortran)也可以用。
它是语法规范的标准用法。
extern "C" {
void GETSTRING(char *A,
int LEN);
}
void main()
{
char STR[] = "hello";
GETSTRING(STR, strlen(STR));
return;
}
subroutine getstring(a,n) Bind( C , Name = "GETSTRING" )
!DEC$ ATTRIBUTES DLLEXPORT ::GETSTRING
use ,Intrinsic::ISO_C_Binding
implicit none
type(C_PTR) , value :: a !c++主程序中传递进来的“a”变量,是C语言的指针
integer , value :: n !传入的字符串长度
character(len=n),pointer::pa !这是Fortran的字符串指针
call c_f_pointer( a , pa ) !把c语言的指针转换成fortran字符串指针
write(*,*) pa
end subroutine
页:
[1]