# How to link specific C language headers

**URL:** <https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642>\
**Category:** General\
**Tags:** ada\
**Created:** [January 7, 2024, 4:01am UTC](https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642 "2024-01-07T04:01:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Verisimilitude](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/verisimilitude/32/384_2.png) [@Verisimilitude](https://forum.ada-lang.io/u/Verisimilitude)\
**Post date:** [January 7, 2024, 4:01am UTC](https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642/1 "2024-01-07T04:01:07Z")

</div>

Hello, this is my first topic on this forum. I’m having issues getting a test program to compile. Here’s the relevant code:  
[http://verisimilitudes.net/2023-09-30](http://verisimilitudes.net/2023-09-30)

I’ve never had to fiddle with compiler options, because I only use the C language standard library, but I’ve been unable to get this test to compile; I’m always told that sqrt is undefined. I’m not at all certain as to what I’m doing wrong here, but I’d appreciate any direction.

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [January 7, 2024, 4:42am UTC](https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642/2 "2024-01-07T04:42:30Z")

</div>

Someone who knows better will likely respond, but: [looking at the ARM](http://ada-auth.org/standards/22rm/html/RM-B-3.html), I don’t see a `Sqrt` function defined in `Interfaces.C` or any other package you are `use`ing. Do you have some other idea where it should be defined?

I suspect that if you want access [to the C standard library’s `sqrt` function](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/math.h.html), then you’ll need to create the bindings, unless they already exist somewhere.

---

<div class="post-metadata">

**Author:** ![Verisimilitude](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/verisimilitude/32/384_2.png) [@Verisimilitude](https://forum.ada-lang.io/u/Verisimilitude)\
**Post date:** [January 7, 2024, 4:47am UTC](https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642/3 "2024-01-07T04:47:36Z")

</div>

Yes, I’m trying to use the C standard library’s sqrt function, but I can’t get it to link for some reason, and I’ve never had this issue before.

---

<div class="post-metadata">

**Author:** ![JeremyGrosser](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jeremygrosser/32/3_2.png) [@JeremyGrosser](https://forum.ada-lang.io/u/JeremyGrosser)\
**Post date:** [January 7, 2024, 5:41am UTC](https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642/4 "2024-01-07T05:41:52Z")

</div>

Assuming this is on a POSIX-ish environment, you need to add `-lm` to your linker switches to get the math functions from libm.

If you’re using gprbuild, try adding this to your .gpr file:

```ada
package Linker is
    for Switches ("Ada") use ("-lm");
end Linker;

```

Here’s my version using the gnatmake flags `-largs -lm`:

> **[Compiler Explorer - Ada (x86-64 gnat 12.2)](https://gcc.godbolt.org/z/W1hcW4b7v)**
>
> with Interfaces.C;
> with Ada.Text\_IO;
> 
> procedure Example is
> function Errno return Interfaces.C.int is
> function True\_Errno return access Interfaces.C.int
> with Import, Convention =\> C, External\_Name =\> "\_\_errno\_location";
> ...

---

<div class="post-metadata">

**Author:** ![Verisimilitude](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/verisimilitude/32/384_2.png) [@Verisimilitude](https://forum.ada-lang.io/u/Verisimilitude)\
**Post date:** [January 7, 2024, 6:22am UTC](https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642/5 "2024-01-07T06:22:32Z")

</div>

This is very odd, then, as I already found and used those flags, to no success.

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [January 7, 2024, 9:17am UTC](https://forum.ada-lang.io/t/how-to-link-specific-c-language-headers/642/6 "2024-01-07T09:17:26Z")

</div>

On macOS,

- the function for errno is \_\_error()
- the linker finds \_sqrt without special switches
- sqrt(-1.0) sets errno to 0 (or, leaves it at 0)
- the result of sqrt(-1.0) is NaN
