# Ada Programming/Interfacing
## Interfacing

Ada is one of the few languages where interfacing is part of the language standard. The programmer can interface with other programming languages, or with the hardware.

## Other programming languages

The language standard defines the interfaces for C, Cobol and Fortran. Of course any implementation might define further interfaces — GNAT for example defines an interface to C++.
Interfacing with other languages is actually provided by pragma Export, Import and Convention.

### Interfacing with C
#### Calling a C function from Ada

The simplest interfacing with C is to call a function without parameters. A C function can be used in Ada by creating a "wrapper": an Ada function declaration that is used to represent the C function and its parameters.
For example, suppose the given C function say_hello:
A wrapper in Ada for this function can be declared as follows:
Another way to declare the same procedure using Aspects in Ada 2012 style:
Both states that Say_Hello procedure is imported under the C convention, from a function called "say_hello". The External_Name aspect is optional if the name of the function is the same in both languages. Thus, the following will also work, because the procedure name is converted to a C function name down-cased to "say_hello" automatically:
In the following example, the Ada main procedure is C_Test in the c_test.adb file. The program prints "Hello from Ada" to standard output first, then call say_hello from C, and then prints "Again from Ada".
The hello.c file contains the "say_hello" function declaration and implementation. To use printf, the stdio.h library is required.
To compile both files the compiler must create the object (*.o) files, and then the linker must used them as input to produce the executable. In GCC/GNAT this can be accomplished by the following terminal instructions:
The first statement compiles hello.c into an object file hello.o. The second one, compiles the c_test.adb generating its object file, and links it with hello.o. The "-largs hello.o" is a parameter passed directly to the linker.
Another tool to compile is gprbuild. It requires a project file. The following can compile the project creating the main program c_test.
To run gprbuild and start compilation:

#### Interfacing in general

The package Interfaces.C is used to define C types. C function wrappers should be used to encapsulate types and functions on the C side. This way the code is portable and forward-compatible. This is similar to the way of interfacing with C in Java's JNI. Wrappers should be used for:
* Translating typedefs defined in C includes to types defined in Interfaces.C;
* Using macros and exposing macro values to the Ada side;
* Using variable parameter list functions;
* Defining multiple function wrappers for a function that takes weakly typed parameters such as a function that takes a char_array or a null pointer;
* Using getters and setters for C structs that depend on operating system version or other compile-type aspect;
* Using pointer arithmetic;
* Keeping Ada source memory-safe.

#### Importing C variables in Ada

Variables can be imported from C to Ada programs. The package Interfaces.C defines several types to contain the data from C. Therefore, to use any variable defined in C in Ada, a wrapper counterpart must be declared with the Import pragma or Import => aspect.
For example, a C integer variable int my_number = 10 can be declared in Ada with:
The following table shows the types in C and its corresponding Ada type. The first column shows different C data types, the second its interfaces intended to store the same data. The third column shows the Ada data type defined in the standard intended for the same use.
No convertion in numbers are necessarily required. The Interfaces.C package defines them as scalars types, which inherits several attributes and operations from other scalars. This feature allows to use numbers variables as usual. However, other types (mostly characters and strings) my require a function to convert. In this case, the function is usually called To_Ada (...) to convert from interface types to Ada, and To_C (...) from Ada to interface types.
Types in Ada used to store C data. :
| C | Ada interface | Ada |
| int | Interfaces.C.int | Integer |
| short | Interfaces.C.short | Short_Integer |
| long | Interfaces.C.long | range... |
| float | Interfaces.C.C_float | Float |
| double | Interfaces.C.double | Standard.Long_Float |
| char | Interfaces.C.char | Character |
| char[N] | Interfaces.C.char_array | String |
| char* (strings) | Interfaces.C.Strings.chars_ptr | String |
| nul | |
| NULL | System.Null_Address | |
A string in C can be created with two forms of declarations: as an array (ex.: char mystring = "TEXT"), and as a pointer (ex.: char* mystring = "TEXT"). In both cases, the zero character is considered as part of the string. In Ada, there are two types for each declaration form: Interfaces.C.char_array for the C array declaration, and Interfaces.C.Strings.chars_ptr for the C pointer declaration. Using them interchangeably may lead to store garbage information or to raise Storage_Error (stack overflow) exceptions.
The following code C and Ada code are a complete example. The C code defines several variables of different types, and the Ada program imports them and prints them on output. The intention is to show an example of the interface code, along with the data stored when executed.

#### Example

The following project file (getopenfilename.gpr) shows how to link to comdlg32.dll:

## Hardware devices

Embedded programmers usually have to write device drivers. Ada provides extensive support for interfacing with hardware, like using representation clauses to specify the exact representation of types used by the hardware, or standard interrupt handling for writing Interrupt service routines.

## See also
### Wikibook

* Ada Programming
* Ada Programming/Libraries/Interfaces

### Ada Reference Manual

* Annex B Interface to Other Languages (Annotated)
* Annex C Systems Programming (Annotated)

### Ada 95 Rationale

* B Interface to Other Languages

### Ada Quality and Style Guide

* 7.6.4 Interfacing to Foreign Languages