【问题标题】:Trying to call Intel Visual Fortran function from Visual C [duplicate]尝试从 Visual C 调用英特尔 Visual Fortran 函数 [重复]
【发布时间】:2016-12-25 18:28:00
【问题描述】:

我的解决方案中有两个项目:CPartFortranPartFortranPart 依赖于CPart,后者包含main 函数。这是main.c的代码

#include <stdio.h>

extern int __stdcall FORTRAN_ADD(int *A, int *B);

int main()
{
    int a = 3;
    int b = 4;
    int c = FORTRAN_ADD(&a, &b);

    printf("%i\n", c);

    return 0;
}

这是我的 fortran 模块的代码

module FORTRAN_UTILS

implicit none

contains

integer*4 function fortran_add(a, b) result(c)
implicit none
integer*4, intent(in) :: a, b
c = a + b
end function fortran_add

end module FORTRAN_UTILS

fortran 编译完成后,我得到文件FortranPart.lib。在CPart 项目依赖项中,我将其添加为外部库。当我尝试编译并运行CPart 时,我得到以下信息

Error   LNK2019 unresolved external symbol _FORTRAN_ADD@8 referenced in function _main  CPart   c:\Users\sasha\documents\visual studio 2015\Projects\MSCourse\MSCourse\main.obj 1   

附: 我需要主程序是 C,而不是 C++。

【问题讨论】:

  • 这里有很多很多关于这个问题的问题和答案。你有没有试过读一些?为什么使用__stdcall FORTRAN_ADD?为什么要 stdcall 呢?为什么要大写字母?你编译32位还是64位?哪个编译器版本?

标签: c visual-studio fortran fortran-iso-c-binding mixed-programming


【解决方案1】:

更多的研究给我带来了这个页面 https://software.intel.com/ru-ru/node/678422

我稍微修改了我的代码,现在它可以工作了。

subroutine fortran_add(a, b, r) bind(c)
    use, intrinsic :: iso_c_binding
    implicit none
    integer (c_int), value :: a, b
    integer (c_int), intent(out) :: r
    r = a + b
    end subroutine fortran_add

main.c

#include <stdio.h>

void fortran_add(int a, int b, int *r);

int main()
{
    int a = 3;
    int b = 4;
    int c;

    fortran_add(a, b, &c);

    printf("%i\n", c);

    scanf_s("");

    return 0;
}

【讨论】:

  • 那个链接不用找了,先看看这里再问!或者只是阅读我添加的标签的 wiki fortran-iso-c-binding
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
相关资源
最近更新 更多