windows - 如何编译这个程序?链接器阶段错误

标签 windows gcc linker ada libc

我已经在 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 `system@4'
collect2.exe: error: ld returned 1 exit status
gnatlink: error when calling C:\Programs\GNAT_2016\bin\gcc.exe
gnatmake: *** link failed.

最佳答案

Stdcall 是 win32 API 使用的约定。当您使用 GNAT 编译代码时,libc 是 GCC 的 libc,因此它对所有内容都使用 C 约定。

将 System_C 绑定(bind)更改为以下内容:

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

将解决您的问题。

关于windows - 如何编译这个程序?链接器阶段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42541777/

相关文章:

c++ - 用 GCC 编译 WIN32 代码

c++ - 为什么调试器总是在这个程序中跳过一行?

c++ - 嵌入式系统C++编译中的问题

c++ - 列出 Windows 进程和模块

c++ - 测量任何 Windows 可执行文件的内存使用情况和执行时间

c++ - 在 Windows C++ 中更改控制台代码页

c - 错误 : invalid conversion from ‘void*’ to ‘arguments*’ [-fpermissive]

c++ - 另一个链接器问题

c - 从其他库 (OS X/POSIX) 重新导出共享库符号

python - Python:OSError:无法加载库libcairo.so.2