AdaCore / gprbuild

GPRbuild is an advanced build system designed to help automate the construction of multi-language systems.
Other
65 stars 21 forks source link

How to build an executable that does not require GLIBC ? #148

Closed MarioBlunk closed 1 month ago

MarioBlunk commented 1 month ago

Hello, I'm not familiar with compiling, binding and linking and all those things. I want to build an executable that does not require any external libraries like GLIBC. The reaseon: The binary file shall be executable on any Linux system without forcing the user to update anything. Here is the link to the project:

https://github.com/Blunk-electronic/ada_training/tree/master/src/gtk/canvas

Just by running gprbuild

I get a binary file that depends on GLIBC when launched. So what can I do ? Thanks.

jklmnn commented 1 month ago

Hi,

you can add -static to the linker flags in your project file:

package Linker is
   for Default_Switches ("Ada") use ("-static");
end Linker;

This will build a binary that does not depend on shared libraries:

$ ldd main
        not a dynamic executable
MarioBlunk commented 1 month ago

Thanks, but where in the project file must these statements be inserted ? I tried this:

with "gtkada.gpr"; project demo is for exec_dir use "."; for object_dir use "obj"; for source_dirs use ("." , "lib"); for main use ("demo"); package Linker is for Default_Switches ("Ada") use ("-static"); end Linker; end demo;

But I get lots of errors when the linker starts like:

user@machine1:~> gprbuild using project file demo.gpr Bind [gprbind] demo.bexch [Ada] demo.ali Link [link] demo.adb /usr/lib64/gcc/x86_64-suse-linux/13/../../../../x86_64-suse-linux/bin/ld: cannot find -lgtk-3: No such file or directory

jklmnn commented 1 month ago

The location of the linker switch is already correct. The problem you're facing is that your distribution doesn't ship with a static libgtk-3. So somewhere on your system is a libgtk-3.so but no libgtk-3.a. I think the best way would be to build GTK 3 from source.

Please keep in mind that this problem is neither Ada nor gprbuild related. It is specific how Linux distributions in general and your distribution in particular handle libraries. You can replicate the problem with a C hello world:

$ gcc main.c -lgtk-3 # works
$ gcc main.c -lgtk-3 -static
/usr/bin/ld: cannot find -lgtk-3: No such file or directory
collect2: error: ld returned 1 exit status

If you really want to build binaries that run on most of the systems I suggest you take a look at appimages.