How to link specific C language headers

Hello, this is my first topic on this forum. I’m having issues getting a test program to compile. Here’s the relevant code:

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.

Someone who knows better will likely respond, but: looking at the ARM, I don’t see a Sqrt function defined in Interfaces.C or any other package you are useing. 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, then you’ll need to create the bindings, unless they already exist somewhere.

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.

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:

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

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

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

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