2016-06-17 49 views
1

最終,我試圖編寫一個使用Fortran計算的IPC計算器來計算C,並在兩個Fortran程序之間傳遞數據。當我完成它有望樣子:編寫C函數,返回int到Fortran

Fortran program to pass input -> Client written in C -> Server written in C -> Fortran program to calculate input and pass ans back 

C客戶機/服務器部分已經完成了,但此刻我是卡試圖寫一個程序,它輸入一個Fortran程序,並將其傳遞給C程序來計算答案。但是,我看到索姆奇怪的行爲。

Fortran程序

program calculator 
    !implicit none 

    ! type declaration statements 
    integer x 
    x = 1 

    ! executable statements 
    x = calc(1,1) 
    print *, x 

end program calculator 

C函數

int calc_(int *a, int *b) { 
    return *a+*b; 
} 

我寫了驗證,當所謂的在C calc_(1,1)INT calc_()確實返回2主程序,但是當我運行程序時,我從Fortran獲得輸出。

我使用這個Makefile 爲C#使用gcc和gfortran的Fortran代碼。 CC = GCC FC = gfortran

calc : calcf.o calcc.o 
    $(FC) -o calc calcf.o calcc.o 

calcc.o : calcc.c 
    $(CC) -Wall -c calcc.c 

calcf.o: calcf.f90 
    $(FC) -c calcf.f90 

我不能爲世界弄清楚爲什麼是這樣的話,它的駕駛我瘋了。

+3

爲什麼你有'隱式沒有'註釋掉?如果它沒有被註釋掉,編譯器會告訴你問題是什麼! – IanH

回答

0

幾乎令人尷尬的簡單。您必須在Fortran中將calc聲明爲整數。 Fortran的工作代碼是

program calculator 
    !implicit none 

    ! type declaration statements 
    integer x, calc 
    x = 1 

    ! executable statements 
    x = calc(1,1) 
    print *, x 

end program calculator 
+1

考慮使用ISO-C綁定來實現兼容性。 – haraldkl

+0

@haraldkl,這是用來幹什麼的? – GLaDER

+0

ISO-C綁定提供了一種與C進行交互的可移植方式。它例如需要關注符號名稱並提供數據類型以確保從一側傳遞到另一側的數據的互操作性。 – haraldkl