5

How can I generate completely static binaries with clang? I have used the following command:

clang -flto <source files> -o <executable output> -fuse-ld=lld -static-libgcc -lc -Bstatic -m32

And yet, the generated output depends on a certain .so file:

$ ldd <executable output file>
    linux-gate.so.1 =>  (0xf77dd000)
    libc.so.6 => /lib/libc.so.6 (0xf75f0000)
    /lib/ld-linux.so.2 (0x5663b000)

The following answer tries to answer the question but doesn't directly address the problem. Is it even possible, to generate completely independent binaries? Or should I have resort to using other different C library implementations other than libgcc?

If yes, then how do I link it with clang if I have the source code, of for example newlib?

Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27
soham
  • 1,508
  • 6
  • 30
  • 47
  • 1
    `linux-gate.so` is the syscall interface and `ld-linux.so` is the ELF binary interpreter (See: https://stackoverflow.com/questions/19981862/what-are-ld-linux-so-2-and-linux-gate-so-1). You might be able to get a statically linked version of `libc` but the others need to be there. This begs the question: "Why?" and "What is the ultimate purpose?" Unless you're doing a boot loader or some such ... – Craig Estey Mar 19 '18 at 17:24
  • If you statically link `libc.so`, you usually get an executable that is *less* portable. – Andrew Henle Mar 19 '18 at 17:24
  • I am trying to make some analysis in my compiler, and I need completely independent binary for that. – soham Mar 19 '18 at 17:45
  • Have you tried the -static option? You may find that some of the libraries you want are not available in a static form. You may be able to find static versions, or build them from source. – Gem Taylor Mar 19 '18 at 20:12
  • 1
    *I am trying to make some analysis in my compiler, and I need completely independent binary for that* So this is an [XY problem.](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) What *problem* in analyzing your compiler do you think a completely statically-linked binary solves? – Andrew Henle Mar 19 '18 at 20:44
  • It would be giving me the C standard library functions to my analysis in the compiler infrastructure. That is why I want to build a C standard library with Clang, so that I can analyse them. – soham Mar 19 '18 at 21:19

2 Answers2

5

Just compile it using the clang's -static flag.

On your case, try:

clang -flto <source files> -o <executable output> -static -m32

The results on my test program show:

[root@interserver ogrerobot.com]# ldd ./CppUtilsSpikes  
not a dynamic executable
zertyz
  • 601
  • 6
  • 9
1

If you, like me, got here expecting Darwin would behave somewhat similarly to Linux, that unfortunately isn't the case

Apple fully supports static libraries; if you want to create one, just start with the appropriate Xcode project or target template.

Apple does not support statically linked binaries on Mac OS X. A statically linked binary assumes binary compatibility at the kernel system call interface, and we do not make any guarantees on that front. Rather, we strive to ensure binary compatibility in each dynamically linked system library and framework.

Caique
  • 113
  • 6