Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static link libstdc++ using clang

When I use GCC, I can build program on my Ubuntu 15.04 using this:

-static-libgcc -static-libstdc++

And compiled binary can run on "stock" Ubuntu 14.04 without any external packages, only standard updates.

Is there possibility do build with this static linking to library with clang?

Most common answers:

  • using test ubuntu rep (ppa:ubuntu-toolchain-r/test)
  • update server
  • recompile on target server
  • don't use GCC

is not suitable for me.

Just can I do this with clang for run it on Ubuntu 14.04.3 LTS?

like image 785
vladon Avatar asked Dec 22 '15 09:12

vladon


1 Answers

clang is compatible with gcc on this matter. Basically for hello-world program that uses iostream to ensure libstdc++ requirement (actual lib versions may vary between distributions):

$ clang++ test.cpp
$ ldd ./a.out
        linux-vdso.so.1 (0x00007ffec65c0000)
        libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/5.3.0/libstdc++.so.6 (0x00007ff937bb6000)
        libm.so.6 => /lib64/libm.so.6 (0x00007ff9378b6000)
        libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/5.3.0/libgcc_s.so.1 (0x00007ff93769e000)
        libc.so.6 => /lib64/libc.so.6 (0x00007ff9372fe000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff937f3e000)

Here is a dependency for libstdc++ and libgcc_s. But if you add -static-libgcc -static-libstdc++:

$ clang++ test.cpp -static-libgcc -static-libstdc++
$ ldd ./a.out
        linux-vdso.so.1 (0x00007ffe5d678000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fb8e4516000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fb8e4176000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fb8e4816000)

That still leaves dependency on libc, but that is a different question.

clang: warning: argument unused during compilation: '-static-libstdc++' means clang ignored this flag, because flag is useless in current situation. First two examples that coming to mind is compiling C code (which obviously don't depend on libstdc++), or issuing compile-only command without linking (-c flag). Since .o file cannot hold information about static or dynamic linking, this flag have to be specified on linking phase (and, to avoid warning, only on linking phase).

like image 124
keltar Avatar answered Oct 11 '22 00:10

keltar