11

I have some code which I want to run on a machine which I do not have root access to. That machine does not have some of the libraries needed to run this code.

Is there any way to include all dependencies when I compile? I realize the resultant file may be quite large.

1 Answer 1

13

What you're looking for is static compiling. Performing static compilation includes all of the libraries into the executable itself, so you don't have to worry as much about dependency chains on a specific system, distribution, etc.

You can do this with:

gcc -Wl,-Bstatic -llib1 -llib2 file.c

The -Wl passes the flags following to the linker, -Bstatic tells it to link static if possible, and then lib1, lib2, are the libs you intend to link.

Alternatively, try:

gcc -static file.c

The compilation will still need to match the architecture of the non-privileged system. And you need to have the static libraries installed on the compiling system (lib.a)

If compiled properly, it should show "not a dynamic executable" when you run:

ldd a.out
4
  • Thanks. Will confirm after I have checked it but I think that is exactly what I was after!
    – Hector
    Apr 27, 2013 at 0:20
  • 3
    Ok, Now I get this which seems strange /usr/bin/ld: cannot find -lgcc_s All i have added is what you suggested with -openssl after
    – Hector
    Apr 27, 2013 at 0:41
  • Add these flags as well: -static-libgcc and -static-libstdc++ (if using c++) Apr 27, 2013 at 0:48
  • 1
    -static-libgcc and -static-libstdc++ are not needed when -static is used. Mar 19, 2015 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.