Ada 程式設計/庫/介面
外觀
Interfaces 包有助於與其他程式語言進行介面。Ada 是少數將與其他語言的介面作為語言標準一部分的語言之一。語言標準定義了與 C、Cobol 和 Fortran 的介面。當然,任何實現都可能定義進一步的介面——例如,GNAT 定義了與 C++ 的介面。
實際上,與其他語言的介面由以下內容提供pragma Export,pragma Import 和pragma Convention.
在 Ada 2012 中,這些子句具有使用 "with" 代替 "pragma" 的替代形式。
例如,以下是一個完整程式,它查詢 Unix 系統上的 terminfo(終端資訊)資料庫以獲取當前終端視窗的尺寸。它與 ncurses 包中的 C 語言函式進行介面,因此它必須在編譯時與 ncurses 連結(例如,使用 -lncurses 開關)。
withAda.Text_IO;withInterfaces.C;withInterfaces.C.Pointers;withInterfaces.C.Strings;procedurectestisuseInterfaces.C;typeint_arrayisarray(size_trange<>)ofaliasedintwithPack;packageInt_PointersisnewPointers ( Index => size_t, Element => int, Element_Array => int_array, Default_Terminator => 0 ); -- int setupterm (char *term, int fildes, int *errret);functionGet_Terminal_Data ( terminal : Interfaces.C.Strings.chars_ptr; file_descriptor : int; error_code_pointer : Int_Pointers.Pointer )returnintwithImport => True, Convention => C, External_Name => "setupterm"; -- int tigetnum (char *name);functionGet_Numeric_Value (name : Interfaces.C.Strings.chars_ptr)returnintwithImport => True, Convention => C, External_Name => "tigetnum";functionFormat (value : int)returnStringisresult : String := int'Image (value);beginreturn(if result(1) = ' ' then result(2..result'Last) else result);endFormat; error_code :aliasedint; error_code_pointer : Int_Pointers.Pointer := error_code'Access;beginifGet_Terminal_Data (Interfaces.C.Strings.Null_Ptr, 1, error_code_pointer) = 0thenAda.Text_IO.Put_Line ( "Window size: " & Format (Get_Numeric_Value (Interfaces.C.Strings.New_String ("cols"))) & "x" & Format (Get_Numeric_Value (Interfaces.C.Strings.New_String ("lines"))) );elseAda.Text_IO.Put_Line ("Can't access terminal data");endif;endctest;
- Interfaces.C
- Interfaces.CPP (GNAT)
- Interfaces.COBOL
- Interfaces.Fortran
- Interfaces.OS2Lib (GNAT, OS/2)
- Interfaces.OS2Lib.Errors (GNAT, OS/2)
- Interfaces.OS2Lib.Synchronization (GNAT, OS/2)
- Interfaces.OS2Lib.Threads (GNAT, OS/2)
- Interfaces.Packed_Decimal (GNAT)
- Interfaces.VxWorks (GNAT, VxWorks)
- Interfaces.VxWorks.IO (GNAT, VxWorks)
