0

According to clang's documentation,

Clang supports a wide variety of C standard library implementations.

But it lacks the information about how to actually use desired libc.

For example, how to use clang with musl if my system provides Glibc by default? Do I need musl-built clang itesl?

Anthony
  • 1,877
  • 17
  • 21
  • Looks like you need to use they own `gcc` build: https://www.musl-libc.org/how.html – Eugene Sh. Nov 09 '21 at 19:48
  • https://libcxx.llvm.org//UsingLibcxx.html – stark Nov 09 '21 at 20:05
  • Note that I'm interested in using differnt _C library_, not C++. – Anthony Nov 09 '21 at 20:30
  • The `-stdlib=` option is available for both C and C++ compilations. – stark Nov 09 '21 at 20:57
  • 1
    Have you compiled and installed musl on your system? `how to use clang with musl?` Normally, there is no difference. `Do I need musl-built clang itesl?` No, clang is separate to musl, they are separate things. You can build clang with any ABI-compatible compiler and build musl with any (different or same) ABI-compatible compiler. To use a different standard library than your systems default, specify `-sysroot=` or `-stdlib=` or similar options. I have no problems using clang on alpine system which uses musl. `How to compile C program using Clang and musl?` Just `clang file.c` – KamilCuk Nov 09 '21 at 21:14

1 Answers1

0

From official document of musl it may be not easy to do so.

You can try to link against libc.so or libc.a in your musl install path, and instruct clang not to link standard libc. Also you need some way to let it include headers form musl's include path.

There can be problems. I'm not sure if gcc-targeted musl can work fine with clang compiled object.

And here's someone's work that may help: https://github.com/dslm4515/CMLFS

breaksuika
  • 34
  • 4
  • > I'm not sure if gcc-targeted musl can work fine with clang compiled object. By "gcc-targeted musl" do you mean "musl built with gcc"? Does it mean that I need clang-compiled musl as well? – Anthony Nov 10 '21 at 14:18
  • Why is this so? Isn't a machine code is just a machine code, no mater which compiler produced it? In the end, it just object files and symbols. Why all this is so difficult/interleaved/coupled? – Anthony Nov 10 '21 at 14:19
  • BTW, musl provides `musl-gcc` and `musl-clang` wrappers, The former works just fine, and it uses something called "specs" (I'm not aware of what is this). The later seem to be "hacky" script which sets/unsets compiler and linker flags in unusual way. It doesn't work out of the box in my system. – Anthony Nov 10 '21 at 14:25
  • by gcc-targeted I mean, in official document it suggests you to use special version of gcc(musl-gcc) to compile. from that I guess it may require some gcc specific feature to link. – breaksuika Nov 11 '21 at 06:01
  • > Isn't a machine code is just a machine code... Real world can be more complex, E.g. calling convention can be different. Without pratice I can't tell if it works. – breaksuika Nov 11 '21 at 06:02
  • maybe `musl-clang` just does what listed above. can you post some output of your `musl-clang`? – breaksuika Nov 11 '21 at 06:06
  • AFAIK, calling convention is defined in ABI. Both GCC and Clang targeting Linux use same ABI. Or I'm wron? – Anthony Nov 11 '21 at 13:05
  • Compiler wrappers: https://gist.github.com/tribals/7cd0b4d5da9ef3a0d579d214f3e5cd91 – Anthony Nov 11 '21 at 13:11
  • Sorry that I cannot help. I'm less famillar to the musl than you. – breaksuika Nov 12 '21 at 05:29
  • I don't think I'm more familiar than you :) – Anthony Nov 13 '21 at 19:41