如何编译这个程序?链接器阶段出错

问题描述:

我已经在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

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; 

将解决您的问题。