【问题标题】:Mixed programming failed in release but succeeded in debug混合编程发布失败但调试成功
【发布时间】:2013-06-10 21:42:25
【问题描述】:

在我的编译器中,一个名为“func”的函数将在用 fortran 编译后重命名为 _FUNC@*。如果 c 代码使用 _stdcall 调用约定,则函数名称,例如Ab,将被重命名为 _Ab@* 编译后。因此,这可以为 fortran 和 c 之间的混合编程提供一种简洁的方法。以下是我的代码。 source1.f90

program main
implicit none
call subprint()
read(*,*)
endprogram

ccode.c

#include <stdio.h>
#ifdef _cplusplus
extern "C" {
#endif
  void _stdcall SUBPRINT()
{printf("hello the world\n");}
#ifdef _cplusplus
}

我的平台是win 7,用的是vs2010。编译器是visual c++。 ccode.c 将生成一个 .lib,fortran 项目将使用它。这将在调试模式下成功运行。但是当我将项目更改为发布模式时,就会出现错误。错误是Source1.f90中的main函数找不到_SUBPRINT。注意,我已经在发布模式下生成了.lib,并将其添加到fortran项目中。

混合编程的另一种方法是使用 _cdecl 调用约定。以下代码将在调试和发布模式下成功运行。

module abc
  interface
    subroutine subprint()
      !dec$ attributes C,alias:'_subprint'::subprint
    end subroutine
  end interface
end module

program main
  use abc
   implicit none
   call subprint()
   read(*,*)
endprogram

这里是c代码。默认调用约定只是 _cdecl。

#ifdef _cplusplus
extern "C" {
#endif
 void  subprint()
 {printf("hello the world\n");}
#ifdef _cplusplus
}
#endif

为什么会这样?我将所有这些代码放在同一个解决方案中。所以配置是一样的。

【问题讨论】:

  • We all know that a function named "func" will be renamed as _FUNC@* after compiling in fortran 不!不!不!这是高度编译器和系统特定的。
  • 另外,我之前已经告诉过你,VS2010是一个EDITOR,你得说你用的是哪个COMPILER!
  • 感谢您的推荐。事实上,我不是工程师。很抱歉说出如此武断的判断。

标签: c visual-studio-2010 fortran release mixed-mode


【解决方案1】:

首先,请注意您的 C 函数是 SUBPRINT 而不是 subprint,即使在 C 内部也很重要。

其次,你应该使用__cplusplus,而不是_cplusplus

第三,只需使用现代 Fortran 与 C 进行互操作:

c.cc:

#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
  void subprint(){
   printf("hello the world\n");
  }
#ifdef __cplusplus
}
#endif

f.f90:

program main
implicit none

interface
  subroutine subprint() bind(C,name="subprint")
  end subroutine
end interface

call subprint()
read(*,*)
endprogram

gfortran c.cc f.f90

./a.out
hello the world

【讨论】:

  • 我已经解决了。 Fortran->外部过程->调用约定->cvf
  • 下次编译器改肯定有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 2022-12-10
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多