2017-03-01 58 views
2

我已經在Windows 10上安裝了gnat gpl 2016並嘗試使用gnatmake編譯下面的(小)程序。問題是由於從libc導入了一個函數,這個任務似乎比簡單的gnatmake.exe gsh_repl.adb複雜得多[gnatmake會在linux上編譯這個很好 - 在最壞的情況下添加-lglibc就足夠了]。我的問題是,我不知道應該添加哪個選項才能使鏈接階段成功完成。下面是程序:如何編譯這個程序?鏈接器階段出錯

with Ada.Text_IO; 
with System; 
procedure GSH_Repl is 

    function System (Command : in String) return Integer is 
    Actual_Cmd : aliased constant String := Command & Character'Val (0); -- append nul to string 
    function System_C (Command : in Standard.System.Address) return Integer 
    with Import => True, External_Name => "system", Convention => StdCall; 
    begin 
    return System_C (Actual_Cmd'Address); 
    end System; 

begin 
    loop 
    declare 
     File : Ada.Text_Io.File_Type; 
     Line : constant String := Ada.Text_IO.Get_Line; 
     Status : Integer := 0; 
    begin 
     if Line = "exit" then 
    exit; 
     end if; 

     Ada.Text_Io.Open (File, Ada.Text_Io.Out_File, "script"); 
     Ada.Text_IO.Put_Line (File, Line); 
     Ada.Text_Io.Close (File); 

     Status := System ("c:\repos\gsh\obj\dev\gsh.exe script"); 
     Ada.Text_Io.Put_Line ("$? = " & Integer'Image (Status)); 
    end; 
    end loop; 
end GSH_Repl; 

可能有某種程序中的錯誤 - 但它編譯罰款,連接一階段失敗:

>gnatmake.exe -L"c:\Programs\GNAT_2016\bin" -llibglibc-2.0-0.dll gsh_repl.adb 
gnatmake.exe -L"c:\Programs\GNAT_2016\bin" -llibglibc-2.0-0.dll gsh_repl.adb 
gnatbind -x gsh_repl.ali 
gnatlink gsh_repl.ali -Lc:\Programs\GNAT_2016\bin 
.\gsh_repl.o:gsh_repl.adb:(.text+0x1cc): undefined reference to `[email protected]' 
collect2.exe: error: ld returned 1 exit status 
gnatlink: error when calling C:\Programs\GNAT_2016\bin\gcc.exe 
gnatmake: *** link failed. 
+0

'未定義參考系統@ 4'表明有某種名稱重整回事...第一步可能是在庫中找到的函數名...或者是在libc中,而不是glibc的? –

+2

如果向下滾動從鑽頭[這裏](http://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ugn/platform_specific_information.html#mixed-language-programming-on-windows),你會發現關於調用約定的東西。由於StdCall約定,添加了@ 4,您可以嘗試使用'C'約定。或者你可以嘗試一個'GNAT.OS_Lib.Spawn'調用,也許? –

+0

@SimonWright真的,調用約定是問題:/ Thx尋求幫助 - 甚至沒有想到這可能是問題。 – darkestkhan

回答

1

Stdcall是Win32 API的使用的慣例。當你用GNAT編譯代碼時,libc是GCC的libc,因此它使用C約定來處理所有事情。

改變System_C結合以下幾點:

function System_C (Command : in Standard.System.Address) return Integer 
with Import => True, External_Name => "system", Convention => C; 

將解決您的問題。