2017-09-25 317 views
1

我試圖編譯這個Calculator.ada文件使用gcc -c Calculator.ada並收到錯誤warning: Calculator.ada: linker input file unused because linking not done - 我試過尋找解決方案並下載其他東西,可能編譯這個但我還沒有想通出來呢....Ada - 用GCC編譯Ada

這裏是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編譯相同的方法。

+0

雖然沒有編譯,或者我錯過了一些@EugeneSh。 ?我sitll只有.ada,不應該/已經產生了一個.o? – NikkiNelson

+1

鑑於[this](https://gcc.gnu.org/onlinedocs/gnat_ugn/Running-a-Simple-Ada-Program.html),ADA計劃應該有一個擴展名「adb」或「ads」。這可能是'gcc'只是不明白它是一個ADA程序... –

+1

鍵入「gnat --version」...如果你得到「命令未找到」你的gcc安裝不完整,你會有找到並安裝它的Ada部分(通常是一個名爲「gnat- 」的包,然後「gnatmake Calculator.adb」(重命名該文件!)應編譯並鏈接它(及其所有依賴項) –

回答

2

默認情況下,Ada源文件需要以.ads(對於包規範)或.adb(對於實體)結尾,文件名需要與它們包含的頂層實體相匹配。在你的情況下,你應該使用calc.adb

如果你有更復雜的源文件包含多個實體,你可以使用gnatchop tool to rename source files

File Naming Topics and Utilities下,本手冊包含了更多關於如何在文件系統中表示源代碼的文檔。

+0

我確實已經閱讀過這篇文章,並將我的文件更改爲:'Calc.adb'和試圖以相同的方式編譯 - 我現在/然後收到錯誤:'gcc:error:CreateProcess:No such file or directory' – NikkiNelson

+0

難道你的'gcc'沒有ADA的支持嗎?它並不明顯 –

+1

它是'calc.adb'(小寫)。關於'CreateProcess'錯誤,這很奇怪。這不是FSF GCC打印的東西。您使用的是AdaCore發行版嗎? –

3

由於GCC只識別.ads.adb爲Ada的源文件結尾(如由Eugene提到this link解釋),你需要明確地告訴它,你想這個文件被編譯爲阿達源。您可以通過

gcc -x ada -c Calculator.ada 

編譯器這樣做會那麼可能會給像

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

警告,但可以忽略。但是,最佳做法是使用gcc預期的文件名。

+0

我現在收到'沒有這樣的文件'存在錯誤 – NikkiNelson

+0

那麼你沒有顯示你的完整代碼。你導入一個不標準的包'Text_IO'(只有一個'Ada.Text_IO'包)。該錯誤可能在該包的界面中,您需要顯示其代碼。 – flyx