4 ms·
Zig also takes this approach, and even exposes its C compiler (which if I recall correctly is basically Clang plus diverse sysroots and other customisation out
by jeffparsons 4y ago
Zig also takes this approach, and even exposes its C compiler (which if I recall correctly is basically Clang plus diverse sysroots and other customisation out of the box) as a separate `zig cc`.
I do a lot of work in Rust, and cross-compilation can be a pain when you have a lot of C dependencies. Fortunately https://github.com/messense/cargo-zigbuild https://github.com/messense/cargo-zigbuild exists. It sounds crazy, but using Zig's inbuilt C compiler to help build my Rust projects has been the smoothest option I've found.
I can't help but wonder if it would be worth it for Rust to follow D and Zig by shipping its own inbuilt C compiler, even if they still want to also support external C toolchains. It should be roughly the same effort as it was for Zig, given that they both use LLVM.
- WalterBright 4y agoD can compile and link C programs with: dmd hello.c C and D code can be mixed with: dmd mars.d pluto.c C code can be imported by D code: import stdio; // looks for stdio.d, stdio.h, stdio.c in that order void main() { printf("using C printf from D!"); } It keys off of the file extension. Amusingly, C code can also import D code: ----- D file ---- int square(int x) { return x * x; } ---- C file ---- __import square; int test() { return square(3); } closing the circle, enabling D libraries to be written and accessed by C.
- WalterBright 4y ago> Zig also takes this approach D took the much more fun way, which is to implement a new C parser from scratch and tweak the D lexer and semantics to handle the differences of C. It's not too bad, about 5000 lines of D: https://github.com/dlang/dmd/blob/master/compiler/src/dmd/cparse.d https://github.com/dlang/dmd/blob/master/compiler/src/dmd/cp...