【问题标题】:Compiling Ada with GCC用 GCC 编译 Ada
【发布时间】:2019-08-07 05:08:50
【问题描述】:

我正在尝试使用 gcc -c Calculator.ada 编译此 Calculator.ada 文件并收到错误 warning: Calculator.ada: linker input file unused because linking not done - 我已尝试查找解决方案并下载其他可能为我编译但尚未想到的东西还没出来……

这里是Calculator.ada

--
-- Integer calculator program.  Takes lines of input consisting of
-- <operator> <number>, and applies each one to a display value.  The
-- display value is printed at each step.  The operator is one of =,
-- +, -, *, /, or ^, which correspond to assign, add, subtract, multiply
-- divide, and raise, respectively.  The display value is initially zero.
-- The program terminates on a input of q.
--
with Text_IO;
with Gnat.Io; use Gnat.Io;
procedure Calc is
   Op: Character;               -- Operation to perform.
   Disp: Integer := 0;          -- Contents of the display.
   In_Val: Integer;             -- Input value used to update the display.
begin
   loop
      -- Print the display.
      Put(Disp);
      New_Line;

      -- Promt the user.
      Put("> ");

      -- Skip leading blanks and read the operation.
      loop
         Get(Op);
         exit when Op /= ' ';
      end loop;

      -- Stop when we're s'posed to.
      exit when Op = 'Q' or Op = 'q';

      -- Read the integer value (skips leading blanks) and discard the
      -- remainder of the line.
      Get(In_Val);
      Text_IO.Skip_Line;

      -- Apply the correct operation.
      case Op is
         when '='      => Disp := In_Val;
         when '+'      => Disp := Disp + In_Val;
         when '-'      => Disp := Disp - In_Val;
         when '*'      => Disp := Disp * In_Val;
         when '/'      => Disp := Disp / In_Val;
         when '^'      => Disp := Disp ** In_Val;
         when '0'..'9' => Put_Line("Please specify an operation.");
         when others   => Put_Line("What is " & Op & "?");
      end case;
   end loop;
end Calc;

对于我为什么无法编译它的任何帮助,我将不胜感激。我可以使用 gcc -c 编译 C 文件,并且读到我可以为 Ada 编译相同的方式。

【问题讨论】:

  • 它没有编译,或者我错过了什么@EugeneSh。 ?我只拥有 .ada,不应该在那里/产生了 .o 吗?
  • 给定this,ADA程序应该有一个扩展名adbads。可能是gcc 只是不明白这是一个 ADA 程序...
  • Type "gnat --version" ... 如果你得到 "command not found" 你的 gcc 安装不完整,你必须找到并安装它的 Ada 部分(通常是一个名为“gnat-。然后“gnatmake Calculator.adb”(重命名文件!)应该编译并链接它(及其所有依赖项)

标签: gcc ada


【解决方案1】:

由于 gcc 仅将 .ads.adb 识别为 Ada 源的文件结尾(如 Eugene 提到的 this link 中所述),您需要明确告诉它您希望将此文件编译为 Ada 源。您可以通过

gcc -x ada -c Calculator.ada

编译器可能会给出类似的警告

Calculator.ada:11:11: warning: file name does not match unit name, should be "calc.adb"

但你可以忽略它。但是,最佳做法是使用 gcc 预期的文件名。

【讨论】:

  • 我现在收到 no such file 存在错误
  • 好吧,您没有显示完整的代码。您导入了一个不标准的包Text_IO(只有一个包Ada.Text_IO)。错误很可能在该包的界面中,您需要显示其代码。
【解决方案2】:

默认情况下,Ada 源文件需要以.ads(对于包规范)或.adb(对于主体)结尾,并且文件名需要匹配它们包含的顶级实体。在您的情况下,您应该使用calc.adb

如果您有包含多个实体的更复杂的源文件,您可以使用gnatchop tool to rename source files

File Naming Topics and Utilities 下,该手册包含更多关于如何在文件系统中表示源代码的文档。

【讨论】:

  • 我确实读到了这一点,并将我的文件更改为:Calc.adb 并尝试以相同的方式编译 - 我现在/然后收到错误:gcc: error: CreateProcess: No such file or directory
  • 会不会是您的gcc 没有ADA 支持?不是很明显。
  • 它是calc.adb(小写)。关于CreateProcess 错误,这很奇怪。这不是 FSF GCC 会打印的东西。您使用的是 AdaCore 发行版吗?
  • @FlorianWeimer 我认为,如果 gcc(编译器驱动程序)无法为编译器本身(gcc1、gnat1 等)找到或生成进程,则会导致此 CreateProcess 错误。
  • Ada 源文件可以任意命名。但是,除非另有明确说明,否则特定的 Ada 编译器 GNAT 期望文件仅包含一个编译单元,并以小写字母“.”命名单元名称。替换为“-”,并以 .ads 或 .adb 结尾。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-01
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
相关资源
最近更新 更多