Completely static linking with clang

与世无争的帅哥 提交于 2019-12-13 02:59:42

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/49368627/completely-static-linking-with-clang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!