5 ms·
The dlang D compiler includes a full C compiler, so you can mix and match C and D code. You can import C code from D, and D code from C. dmd hello.c ./
by WalterBright 2y ago
The dlang D compiler includes a full C compiler, so you can mix and match C and D code. You can import C code from D, and D code from C.
dmd hello.c
./hello
hello world
This helps enormously in binding to existing C libraries.
- brabel 2y agoIs it possible to do what Zig does and cross compile to any OS/arch, from any OS/arch? Last I tried, I found some compiler options and commands to do it, but they were badly explained, I tried for hours and it just didn't work at all, so I gave up.
- WalterBright 2y agoThe dmd D compiler can cross-compile to any of the targets that it supports. I use it all the time to develop on other platforms. I am currently adding AArch64 support to DMD's code generator. dmd doesn't run on my Raspberry Pi (yet!), so I use sshfs to access the Pi's filesystem from Ubuntu, compile programs on Ubuntu, transfer the .o file to the Pi, link it on the Pi, and run it on the Pi. I'm sorry you're having trouble with this, so I recommend posting on the D forums, and people are there to help! https://forum.dlang.org/ https://forum.dlang.org/
- wyldfire 2y agoThe grandparent post refers to the fact that Zig ships with C, C++ library source that it lazily cross-builds (at least for linux, I don't recall how it solves this problem for macOS or windows). And possibly it uses lld too? Since Zig ships with these, it makes the whole cross-build experience much smoother and less dependent on whether your host OS distribution includes packages for your desired target OS/architecture.
- brabel 2y agoExactly. Zig tooling includes a linker, and that's also cross-platform. Zig can generate actual runnable executables that run anywhere from a single machine. As far as I can tell, D's DMD can cross compile but not link. I probably missed that before (actually, I think I was trying with LDC, I found the page where they document the process while searching for it just now: https://wiki.dlang.org/Cross-compiling_with_LDC https://wiki.dlang.org/Cross-compiling_with_LDC if you try those options, nothing works, at least from MacOS). I just tried and this only works with the -c option (compile, but not link): dmd -os=linux -c source/app.d Without `-c`: ``` Error: linker exited with status 1 cc app.o -o app -m64 -Xlinker -no_compact_unwind -L/Users/me/dlang/dmd-2.109.1/osx/bin/../lib -Xlinker -Bstatic -lphobos2 -Xlinker -Bdynamic -lpthread -lm (dmd-2.109.1) ``` I am sure Walter knows how to compile on one machine, then go to another machine and do the linking or whatever is needed. I know nothing about those, but with Zig I need not know anything other than which flag to use to cross compile. It's a huge difference. D should do that, otherwise it's just not competitive for people like me (I am just an average joe programmer who knows how to write code but has very little interest in learning how linkers work).
- wyldfire 2y ago> I am sure Walter knows how to compile on one machine, then go to another machine and do the linking or whatever is needed. Historically, you would or at least could get your compiler and linker from different vendors -- assembler, disassembler and other tools too. So it's normal/common for C compilers like gcc, clang, dmd to not be distributed like Zig is. But I would dare say that the way Zig is doing it is a great model for others. > go to another machine and do the linking BTW - if you use something like debian/ubuntu for development, they have compilers+linkers+binutils for several different architectures available to make cross-building without Zig at least a little bit easier.
- kubkon 2y agoFYI, Zig is in the process of dropping dependence on lld on Linux and Windows; macOS uses Zig's own linker since roughly 2021 as far as I recall. Tracking issue for reference: https://github.com/ziglang/zig/issues/8726 https://github.com/ziglang/zig/issues/8726
- o11c 2y agoGenerally, the hard part isn't the cross compiler (nor, these days, the cross binutils), but the cross libraries. There are at least 3 ways (/usr/lib/$ARCH, /usr/$ARCH/lib, /$CHROOT/usr/lib/) to set up the libraries, all incompatible with each other. Zig is notable for putting a lot of work into hard-coding basic C libraries, though of course you still need a scattering of other C libraries for most real-world programs. (for a sufficiently advanced language ecosystem, the language's own libraries should be easy enough)
- wucke13 2y agoThe nixpkgs have a rather elegant way to deal with that. Let's presume you need a libpng for an armv7 platform, such as an old RaspberryPi. Then Nix allows you to - get a suitable cross compiler toolchain - pick whether you want dynamic or static linking (most likely static when the target system does not come with /nix/store) - compile all prerequisites for the library such as transitive dependencies - compile the library with the cross compiler for the selected target arch All in one command line call, that does not require you to 'apt-get install' any of the tools manually: nix-build '<nixpkgs>' -A pkgsStatic.libpng --arg crossSystem '{ config = "armv7a-unknown-linux-gnueabihf"; }'
- Cloudef 2y agoYou can combine zig and nix with <https://github.com/Cloudef/zig2nix https://github.com/Cloudef/zig2nix> (shameless plug)
- Daunk 2y agoD is so underrated. Keep up the good work Walter.
- throwaway2037 2y agoI am pretty sure that most of the language design is done by Andrei Alexandrescu now.
- nequo 2y agoHow exactly does this relate to the post, Aro, other than this also being a C compiler? Could you draw a comparison for those who came here for Zig and not D? What conclusion should we draw about Aro based on what you’re saying?
- WJW 2y agoIt doesn't relate to Zig at all, but Walter Bright is the creator of D and tremendously proud of it.
- nequo 2y agoI know that he is but this is incredibly off-topic. Aro deserves the attention it is getting, without the discussion getting hijacked by another project. D would deserve attention too, in a separate post, or here if it was explained how it relates to Aro.
- jraph 2y agoWalter's comment could be read more charitably and could show his interest in Zig and similar choices that are made. The last sentence, or, indeed, the whole comment, can be read like "We did this for D, and it works well. [go for it]". I don't think D is casting any shadow on Zig. Both languages play in the same field, D is older but I feel like we hear about Zig way more these days. His comment is likely to interest people interested in Zig or PLs in general. The comment as is might lack some finesse but I don't think it deserves this level of aggressiveness.
- bithavoc 2y agoI have profound respect for Walter and all his work, same for Andrei; but his comment was off-topic.
- pests 2y agoHis post does seem self-promotional I admit but Walter usually shows up in any threads about compilers and usually shares interesting comparisons and contrasts to how his language and compiler handled the issue. For me, I appreciate the perspective and opinions of someone else in the industry with proven results. D is a major language, you can't deny that. Maybe this post he should have went into a little more detail but I do not think he posted this with ill intent. If anything I'm sure he would love to continue a dialog about the design decisions that led him to implement the same feature - and maybe this was his way of starting the conversation. Given that he is a 15 year member of our community, and has earned 70k karma, we should give him the benefit of the doubt.
- binary132 2y agoI would 100% be a D user if it had full C++ typesystem fluency.
- WalterBright 2y agoD has a lot of compatibility with C++'s type system. But not with everything - for example, D does not have a `volatile` type. What is missing that you're interested in?
- binary132 2y agoI know, and that’s a major selling point for D in my book; iirc it’s what led me to explore D in the first place. Hardly any languages have good C++ interoperability. I was following Calypso with great interest for a while, but sadly it looks like the author lost momentum, so it never got exception handling for MSVC. What I need to work reliably from C++ are 1. instantiation of imported templates, ideally including CRTP, and 2. handling in D of exceptions thrown from C++, because if I use a C++ library that throws, I need to be able to handle the exception. So far I’ve found languages that can import template classes and functions from C++ (usually by extending LLVM/Clang), but no languages that have good, reliable cross-platform C++ exception handling.
- fuzztester 2y agoInteresting, good to know.
- WhereIsTheTruth 2y agoI was about to say this, and DMD compiles C code insanely FAST!
- flohofwoe 2y ago...I mean Zig has those features too since the beginning with `zig cc`, translate-c and @cImport (currently via an embedded Clang), but the post is about an indepdent C compiler written in Zig (but which will most likely replace Clang as the embedded C frontend once Clang/LLVM isn't compiled into the Zig compiler anymore).