簡體   English   中英

將數組從fortran傳遞給C ++函數

[英]Passing an array from fortran to a C++ function

我在Fortran中有一個主程序。 我在Visual Studio 2010上使用Intel Visual Fortran XE2011。我想使用在C ++中編碼的函數。 我正在使用的功能是獲取多個數組(輸入-從主要的fortran程序中設置),並使用它們形成輸出數組(返回至主要的fortran程序)。 我已采取以下步驟:

1)我使用Fortran主程序和模塊創建了一個Fortran項目,並將其設置為“啟動項目”。

2)我創建了一個類型為“靜態庫”的C ++項目。

3)我添加了$(IFORT_COMPILERvv)\\compiler\\lib\\ia32 ,如此處http://software.intel.com/zh-cn/articles/configuring-visual-studio-for-mixed-language-applications所述

C ++靜態庫的構建沒有問題。 我得到的錯誤與在fortran程序中real(8)變量的聲明有關。

對於所有real(8)聲明,我得到以下兩個錯誤,即總共6個錯誤:

錯誤#5082:語法錯誤,當期望以下情況之一時發現'(':::%FILL,TYPE BYTE CHARACTER CLASS DOUBLE DOUBLECOMPLEX DOUBLEPRECISION ...

錯誤#5082:語法錯誤,當期望以下其中一項時,發現“ ::”:(*,; [/ = =>

這是我使用的代碼:

主要的Fortran程序:

Program Fort_call_C

use iso_c_binding

implicit none

interface 

   subroutine vec_sum_c(a,b,c) bind (C, name = "vec_sum_c")

      use iso_c_binding

      implicit none

      real(8) (c_double), intent (in), dimension (*) :: a,b
      real(8) (c_double), intent (out), dimension (*) :: c

   end subroutine get_filled_ar

end interface  

integer:: i
integer (c_int)::m
real(8)(c_double),dimension(:):: a, b, c

open(unit=10, file="input_arrays.txt",status="unknown")
read(10,*) m
allocate(a(m),b(m),c(m))

do i=1,m
   read(10,*)a(i),b(i)
end do
close(10)

call vec_sum_c(m,a,b,c)

do i=1,m
   print*, c(i)
end do

pause

end program

C ++函數是:

extern"C" void vec_sum_c(int *m, double *a, double *b, double *c){
    int mm = *m;
    for(int i=0;i<=m-1;i++){
        c[i]=a[i]+b[i];
     }
}

有人可以幫我解決這個問題嗎? 您能否讓我知道將整個數組從fortran程序發送到c ++例程的想法是安全的還是有問題的(最好避免)?

您的Fortran語法已過期。 你有兩次真正的善良。 嘗試

REAL(C_DOUBLE), INTENT(IN), DIMENSION(*) :: a, b

等等

C_DOUBLE是一個命名常量。 該處理器恰好具有值8。

也:

  • 您在Fortran接口主體中缺少C函數的參數m
  • 您會改變在開頭和結尾語句之間的Fortran接口主體中子例程的名稱的想法!
  • 您的C ++ for循環小於等於,則與m進行比較,該值應該為mm

以這種方式發送整個數組沒有固有的問題。

我只設法將變量的值從C函數傳遞給fortran函數。

我在這里粘貼了兩個源文件,即main.c和fortran.f。您可以在Microsoft Visual Studio 10中使用這兩個文件。按照http://software.intel.com/頁上的建議在Visual Studio中進行所有設置之后。 zh-cn / articles / configuring-visual-studio-for-mixed-language-applications ,您需要進行以下更改:

  1. 轉到C / C ++靜態庫的項目屬性;
  2. 轉到C / C ++
  3. 轉到代碼生成
  4. 將運行時庫設置為多線程調試(/ MTd)

現在您可以構建程序了。

main.c中:

#include <stdio.h>
#include <malloc.h>

void testc(double **pa, double **p)
{
 double b;
 double *a, *c;
 int m;

 c = (double*) malloc(sizeof(double));
 *c = 10;

 *p = c;

 a = (double*) malloc(sizeof(double)*5);
 a[0]=1.23;
  a[1]=2.46;
a[2]=3.69;
 a[3]=4.11;
 a[4]=7.21;
 *pa=a;
 for (m=0;m<5;m++)
 {
    b=a[m];
    b=b+1.0;
    a[m]=b;
  }
 } 

fortran.f:

 program test
  use iso_c_binding
  implicit none
 interface
  subroutine testc(pa, m) bind(c)
  use iso_c_binding
   type(c_ptr):: m
   type(c_ptr):: pa
  end subroutine testc
  end interface

  type(c_ptr) :: pa
  type(c_ptr) :: m
  real(c_double),pointer::fpa(:)
   real(c_double),pointer::fm(:)

    call testc(pa,m)
     call c_f_pointer(pa, fpa, [5])
     call c_f_pointer(m, fm, [1])
     print *, fm(1)
    print*, fpa(1)
   pause

 end program test 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM