1

我正在尝试编译一个子例程 sendmsg.f90,我们以前用 lf90 作为 32 位现在用 gcc 作为 64 位编译。目的是将它与其他用 gcc 编译的 64 位子程序链接起来。

subroutine UpdateDisplay(WINHAND,NTPS,QCANCEL,NITER)
  use win32mod
  implicit none
  integer WINHAND, NTPS, MESSNUM, NITER, QCANCEL
!                SendMessageA is a Windows function
!                  WINHAND = handle of window (from ISDev code)
!                  MESSNUM = message number (assigned by ISDev)
!                  NTPS = number of year iteration currently on (WParam)
!                  DUM = 0 (LParam)  

  MESSNUM = 1114
  QCANCEL = SendMessageA(carg(WINHAND),carg(MESSNUM),carg(NTPS), carg(NITER))

end subroutine

我们有 Lahey Fortran 7.5,它有 lf90 编译器和 gcc(它使用 gfortran)和一个叫做 lgf 的东西(我想它使用 gcc,对吗?)。

我是 Windows 编程(和 Fortran)的新手。使用 gcc 编译的代码可以调用 Windows API 吗?我应该使用 ISO_C_BINDING 吗?

我应该这样做还是应该尝试将 lf90 创建的 .obj 文件与 gcc 创建的 .o 文件链接?

谢谢你的帮助!

更新:我尝试使用以下方法调用 Windows API:

MODULE SND_C
    interface
        integer(C_LONG) FUNCTION SendMessage  &
            (WINHAND,MESSNUM,NTPS, NITER)  &
            bind(C,Name='SendMessage')
            use ISO_C_BINDING
            implicit NONE
            integer(C_LONG), VALUE :: WINHAND
            integer(C_LONG), VALUE :: MESSNUM
            integer(C_LONG), VALUE :: NTPS
            integer(C_LONG), VALUE :: NITER
        end function SendMessage
    end interface
END MODULE SND_C


subroutine UpdateDisplay(WINHAND,NTPS,QCANCEL,NITER)    
  USE ISO_C_BINDING, ONLY: C_LONG
  USE SND_C
  implicit none
  integer(C_LONG) WINHAND, NTPS, MESSNUM, NITER, QCANCEL
!                SendMessageA is a Windows function
!                  WINHAND = handle of window (from ISDev code)
!                  MESSNUM = message number (assigned by ISDev)
!                  NTPS = number of year iteration currently on (WParam)
!                  DUM = 0 (LParam)  

!GCC$ ATTRIBUTES DLLEXPORT :: UpdateDisplay
  MESSNUM = 1114
  QCANCEL = SendMessage(WINHAND,MESSNUM,NTPS, NITER)

end subroutine

但是当我尝试用“gcc -m64 sendmsg.f90”编译时,我得到了错误:

C:\Users\StephanieJ\Documents\Lahey-Fujitsu Fortran>gcc -m64 sendmsg.f90
c:/progra~2/lahey-~1/v7.5/gcc-gf~1/bin/../lib/gcc/x86_64-w64-mingw32/4.7.4/../..
/../../x86_64-w64-mingw32/lib/../lib/crt2.o: In function `__tmainCRTStartup':
c:\gccbuild\mingw-crt\build32-64\mingw-w64-crt/../../mingwsvn/mingw-w64-crt/crt/
crtexe.c:323: undefined reference to `__laheypause'
C:\Users\STEPHA~2\AppData\Local\Temp\ccWjjo5b.o:sendmsg.f90:(.text+0x3e): undefi
ned reference to `SendMessage'
c:/progra~2/lahey-~1/v7.5/gcc-gf~1/bin/../lib/gcc/x86_64-w64-mingw32/4.7.4/../..
/../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):
In function `main':
c:\gccbuild\mingw-crt\build32-64\mingw-w64-crt/../../mingwsvn/mingw-w64-crt/crt/
crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

这种方法有意义吗?为什么我对 SendMessage 的引用未定义?

4

1 回答 1

2

调用 API 的最简单方法可能是通过 C 包装函数,如下所示:

#include <windows.h>
void updatedisplay_(  HWND *winhand, WPARAM  *ntps, LRESULT *qcancel, LPARAM *niter)
{
  //  integer NTPS, MESSNUM, NITER, QCANCEL
  /*
  !                  WINHAND = handle of window (from ISDev code)
  !                  MESSNUM = message number (assigned by ISDev)
  !                  NTPS = number of year iteration currently on (WParam)
  !                  DUM = 0 (LParam)  
  */
  UINT messnum = 1114;
  *qcancel = SendMessage(*winhand,messnum,*ntps,*niter);
}

小写字母和尾随下划线与 Fortran 默认函数命名相匹配。如果您假设文件名为 updatedisplay.c,请使用以下命令对其进行编译:

gcc -c 更新显示.c

在 Fortran 方面,不是发送零作为窗口句柄,而是使用 ISO_C_BINDING 中的 C_NULL_PTR。然后,您可以将目标文件 updatedisplay.o 与最初调用同名 Fortran 函数的 Fortran 代码链接,它应该可以工作。

要摆脱 laheypause 的问题,请从正确的 lib 目录链接到文件 lgfpstub.o,x86_64-w64-mingw32\lib 用于 x86_64 或 x86_64-w64-mingw32\lib32 用于 x86。

希望这可以帮助!

于 2013-08-31T01:22:33.293 回答