账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    关于QT程序调用Fortran生成的Dll的问题,求解答!!
    302
    0

    题目描述

    QT+VS2017 无法调用Fortran生成的DLL

    题目来源及自己的思路

    自己学习中遇到的问题,当时以为是路径的问题,但是我用绝对路径也不行,把dll文件拷到exe目录也不行。

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)
    1、用LoadLibrary()

    #include "Test00.h"
    #include<Windows.h>
    #include<qmessagebox.h>
    typedef int(*FUNCTIONABZERO)(int input);
    
    Test00::Test00(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        loadDll = LoadLibrary(TEXT("C:\VisualStudioWorkspace\QtStudy\Test00\Test00\TestDll.dll"));
        if (loadDll == NULL)
        {
            QMessageBox::warning(this, QStringLiteral("警告!"), QStringLiteral("载入库文件失败!"), QMessageBox::Yes);
        }
    }
    
    void Test00::on_startBtn_clicked()
    {
        FUNCTIONABZERO ABZERO = (FUNCTIONABZERO)GetProcAddress(loadDll,"ABZERO");
        if (ABZERO==NULL)
        {
            QMessageBox::warning(this, QStringLiteral("警告!"), QStringLiteral("函数不存在!"), QMessageBox::Yes);
        }
        else
        {
            int a = ui.arg1->text().trimmed().toInt();
            int result = ABZERO(a);
            ui.result->setText(QString::number(result));
        }
    }

    2、换用QLibrary

    QLibrary myDLL("C:\VisualStudioWorkspace\QtStudy\Test00\Test00\TestDll.dll");
        if (myDLL.load())
        {
            QMessageBox::information(this,"OK","DLL is loaded! ");
        }
        else
        {
            QMessageBox::warning(this,"Warning","DLL load failure!");
        }

    你期待的结果是什么?实际看到的错误信息又是什么?

    希望大佬解惑!!!

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • Pasttense 普通会员 1楼

      在Qt程序中调用Fortran生成的DLL,主要涉及接口问题和编译器兼容性问题。以下是一种基本的步骤:

      1. 编写Fortran DLL: 在Fortran中创建一个子程序(或函数),并使用BIND(C)属性使其符合C语言的调用规范,这样其他语言(如C++/Qt)可以正确调用。例如:

      ```fortran module mymod use, intrinsic :: ISO_C_BINDING

      contains subroutine mysubroutine(a, b, c) bind(C) integer(c_int), intent(in) :: a real(c_double), intent(out) :: b character(kind=c_char), dimension(*) :: c

      ! ...你的Fortran代码...
      

      end subroutine mysubroutine end module mymod ```

      1. 编译Fortran为DLL: 使用相应的Fortran编译器(如Intel Fortran,GFortran等)编译上述Fortran代码为DLL。例如,在GFortran下,可能需要如下命令:

      bash gfortran -shared -fPIC -o mydll.dll myfortranfile.f90

      1. Qt中调用Fortran DLL: 在Qt中,你可以通过QLibrary加载DLL,并声明对应的函数指针类型来调用。

      ```cpp

      include

      typedef void (mysubroutine_type)(int, double, char*);

      mysubroutine_type mysubroutine = nullptr; QLibrary lib("mydll.dll");

      if (lib.load()) { mysubroutine = (mysubroutine_type) lib.resolve("mysubroutine_"); // 注意Fortran导出函数名可能带下划线后缀 if (mysubroutine) { int a = 10; double b; char c[256]; mysubroutine(a, &b, c); // ...处理返回结果... } else { qDebug() << "Failed to resolve function"; } } else { qDebug() << "Failed to load library"; } ``` 请注意,实际的函数名称可能会因为编译器的不同而有所变化(例如,GFortran会在函数名后添加下划线和额外的字符)。你可能需要查看编译生成的DLL的导出表以确定正确的函数名称。另外,确保Fortran编译器与Qt使用的C++编译器兼容,避免因ABI不匹配导致的问题。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部