Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link math.h with gprbuild

Tags:

gcc

ada

gprbuild

I'm trying to build an Ada application that calls C code. The C code use the function sqrt from math.h. If I remove the call to sqrt, the compilation and linkage work perfectly. When I try with the sqrt call, the linker tells me undefined reference to sqrt.

This is my gpr file:

project Struct_Interfacing is
    for Languages use ("Ada", "C");

    for Source_Dirs use ("src");
    for Object_Dir use "obj";
    for Exec_Dir use ".";

    for Main use ("struct_interfacing.adb");

    package Compiler is
        C_Switches := ("-pedantic", "-Wall", "-Werror");
        for Default_Switches("C") use C_Switches;
    end Compiler;

    package Linker is
        for Default_Switches("C") use ("-lm");
    end Linker;
end Struct_Interfacing;

I thought the solution would be for Default_Switches("C") use ("-lm"); but it still doesn't work.

like image 530
Louis Etienne Avatar asked Mar 27 '26 05:03

Louis Etienne


1 Answers

Your main program is in Ada, so you should tell your compiler to link Ada with -lm, even if the call is made from C:

    package Linker is
        for Default_Switches("Ada") use ("-lm");
    end Linker;
like image 126
egilhh Avatar answered Apr 01 '26 08:04

egilhh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!