0

我在我的解決方案中有兩個項目:CPartFortranPartFortranPart取決於CPart,後者包含main函數。這裏的main.c試圖從Visual C中調用英特爾Visual Fortran函數

#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 [email protected] referenced in function _main CPart c:\Users\sasha\documents\visual studio 2015\Projects\MSCourse\MSCourse\main.obj 1 

P.S. 我需要主程序在C中,而不是C++。

+1

真的有很多很多很多的問題,在這裏對這個問題的答案。你有沒有嘗試閱讀一些?你爲什麼使用'__stdcall FORTRAN_ADD'?爲什麼stdcall呢?爲什麼大寫字母?你編譯32位或64位?哪個編譯器版本? –

回答

0

多一點的研究把我帶到這個頁面 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; 
} 
+0

沒有必要搜索該鏈接,只需在問問前查看這裏!或者只是閱讀我添加的標籤的wiki [tag:fortran-iso-c-binding]。 –