Fortran Coder

C++调用fortran生成的dll文件的问题求助

查看数: 8306 | 评论数: 4 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2017-2-6 10:54

正文摘要:

C++主程序代码以及待调用的fortran代码都在图1中所示,步骤为:首先生成fortran代码对应的TEST.dll和TEST.lib文件;加入到C++的工程中;编译总是出现图中的错误,请教各位这是什么原因 ...

回复

fcode 发表于 2017-2-6 17:48:48
建议你用标准的混编方法:ISO_C_Binding
不但在 VS+IVF 可以用,在其他编译器(gcc,gfortran)也可以用。
它是语法规范的标准用法。


[C++] 纯文本查看 复制代码
extern "C" {
  void GETSTRING(char *A,
    int LEN);
}
void main()
{
  char STR[] = "hello";
  GETSTRING(STR, strlen(STR));
  return;
}

[Fortran] 纯文本查看 复制代码
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

alonewolf 发表于 2017-2-6 15:53:57
有没有可能是fortran编译器版本的问题呢?
alonewolf 发表于 2017-2-6 13:17:19
C++源代码如下:
[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代码如下:
[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
fcode 发表于 2017-2-6 11:37:51
请以文本(而非截图)的方式,贴出 C++ 代码和 fortran 代码。

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-6 19:51

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表