Trying to call Intel Visual Fortran function from Visual C

246 Views Asked by At

I have two projects: CPart and FortranPart in my solution. FortranPart depends on CPart and the later contains the main function. Here is code of the 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;
}

Here is code of my fortran module

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

After the fortran is compiled I get the file FortranPart.lib. In CPart project dependencies I added it as external library. When I try to compile and run CPart I get the following

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   

P.S. I need the main program to be in C, not C++.

1

There are 1 best solutions below

1
On

A little more research brought me this page https://software.intel.com/ru-ru/node/678422

I changed my code a little and now it's working.

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

and 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;
}