6 ms·
Next C compiler is a D compiler: Introducing DMD's ImportC
- WalterBright 4y agoThis article is about using the D compiler's ability to import vanilla C code to compile entire C projects.
- tristanbvk 4y agoThis might actually make my life a lot easier compared to how I am currently doing certain things with some of my libraries that need to interoperate with C. Going to give ImportC a spin there!
- melony 4y agoCan DMD compile C projects directly similar to the Zig compiler?
- WalterBright 4y agoI haven't used Zig, but DMD can compile C projects directly using makefiles or by simply putting the .c files on the dmd command line.
- cxr 4y agoDo you plan on giving Zig a try? (They seem to be doing a lot right.) Taking the opportunity here to point people to "Adding Modules to C in 10 Lines of Code" from NWCPP earlier this year: <https://www.youtube.com/watch?v=2ImfbGm0fls https://www.youtube.com/watch?v=2ImfbGm0fls> (The pitfalls you mention running into there wrt to different compiler instances is itself a decent case study in not relying on singletons <http://www.object-oriented-security.org/lets-argue/singletons http://www.object-oriented-security.org/lets-argue/singleton...>—even if it ultimately wasn't so inconvenient that the work never got done, it would be even more convenient if it didn't need to have been done in the first place!)
- simplotek 4y ago> Do you plan on giving Zig a try? (They seem to be doing a lot right.) All this Zig spam is getting very tiring.
- klodolph 4y agoIn a conversation about languages with this kind of C interop, how can you omit Zig?
- simplotek 4y ago> In a conversation about languages with this kind of C interop It's not. It's a discussion about "the C compiler built into the Digital Mars D compiler". Please stop with all the Zig spam.
- primeblue 4y ago
- pverghese 4y agoInstead of being petulant about it, you could ignore it and move on
- 10241024 4y agoHard to ignore it when it's always in your face: https://news.ycombinator.com/item?id=33331549 https://news.ycombinator.com/item?id=33331549 I don't mind languages getting hyped but seeing posts every week on the front page of HN about an unstable esoteric language with 0 adoption - yes, it's getting tiring.
- pverghese 4y agoHow come it's always accounts made a few days ago which seem too have this problem with other people's interests
- melony 4y agoZig can be used as a standalone CC replacement and not just for FFI purposes.
- benreesman 4y agoA lot of people might not know that before doing the D language, Walter was already a world-wide acknowledged expert on the compilation of C and C++. I don’t use D seriously but I’d still bet my ass that something smart happens when you feed C code to the D compiler toolchain.
- krylon 4y ago> A lot of people might not know that Personally, that would surprise me, because C++ compilers is the context where I first read the name. I'm not saying this is incorrect, but I would find it amusing somehow.
- nkozyra 4y agoIt's fair to say that Walter is best known for his work with D rather than his work with C++, though.
- krylon 4y agoDepends on how old you are. ;-)
- benreesman 4y agoIndeed. He was a legend a long time before there was a D programming language.
- a9h74j 4y agoYes, Zortech C++: about 1991 IIRC. If this was not the first commercial compiler available on Windows, it was one of them.
- Kukumber 4y agoThat's impressive, i thought the preprocessor wasn't planned, looks like it takes care of it too, i guess it's time to revisit D, C but better
- WalterBright 4y agoPreprocessing is now fully supported!
- tristanbvk 4y agoMy experience with D is always a joyful one. If it isn't because it is a relatively straightforward language to learn then it is learning something new now and then that just makes life to much more easier. Infact, D was the first language I learnt templating in and I really like it (or rather, the way D goes about it - made it much more approachable for me).
- WalterBright 4y agoI wasn't going to add templates to D until I figured out a way to make them simple.
- Zoadian 4y agoHey walter, have you thought about adding first class types to the language? that would enable us to use CTFE for type based algorithms instead of templates like std.meta. If you did, what are the reasons against them?
- tristanbvk 4y agoYou definately made it simple! I recall the very first time I tried learning then in D (was trying to understand the `to!` from `std.conv` and it seemed simple enough). The syntax for inferring a type for function calk arguments is nifty too. Lastly, today I finally understand what mixins are , I just read some stdlib file somewhere and thought, wow that's rather useful. I definately think I will be making use of mixins sometime soon, been working on a small compiler in D itself and there are times were duplication (or perhaps inclusion is a better word) of code would be nice in such a manner.
- bumblebritches5 4y ago
- mhh__ 4y agoThere is a question about C23 if you can see [dead] people: 1. We can support it, I suspect no headers will go near it for a while but we can. 2. C23 support actually brings some C semantics closer to D ones anyway...
- TazeTSchnitzel 4y ago> Error: C designator-list not supported yet I guess only a subset of C11 is supported? Is there all of C89?
- WalterBright 4y agoDesignator-list support has since been added. The multi-layer designators are still to be done, though (they're rarely used, so are a lower priority).
- flohofwoe 4y agoI think something along those lines (rarely used...) was also given by the C++ Committee as justification why so much of the C99 designated init feature set was left out of C++20, which is a bit of a shame. All C99 designated-init 'sub-features' complement each other nicely once you start using them. It's a great way for building 'data-centric' APIs. For instance check out this call to create a 3D-API pipeline-state-object in C99: https://github.com/floooh/sokol-samples/blob/ad9047e228a84413d379e858eed49e5e702a0892/sapp/cube-sapp.c#L80-L98 https://github.com/floooh/sokol-samples/blob/ad9047e228a8441...
- Kukumber 4y agoInteresting.. i personally use that a lot in C, this lib for example: https://github.com/floooh/sokol#sokol_gfxh https://github.com/floooh/sokol#sokol_gfxh I couldn't live without it https://dlang.org/spec/struct.html#static_struct_init https://dlang.org/spec/struct.html#static_struct_init D support it, what's blocking it?
- arjvik 4y agoThis is just like Zig's C compiler
- generichuman 4y agoZig doesn't have a C compiler yet, it bundles clang. Someone in the community was working towards a C compiler in Zig though. ImportC is basically a C frontend for the D compiler, semantics are close enough but there are some changes to be made still.
- csande17 4y agoZig does have "translate-c", which attempts to convert C code to Zig code. Similarly to D, it's used for a feature to automatically import C headers into a Zig program. But even that's based on libclang's parser and AST.
- p0nce 4y agoImportC has bitfields and #pragma pack but not translate-c.
- flohofwoe 4y agoThe important part in Zig is that you don't need another compiler toolchain to build a mixed C/C++/ObjC project, how that's implemented under the hood (e.g. embedded clang vs self-made compiler) is an implementation detail. I'd argue that C++ and ObjC support is just as important as C support for real world projects (because a lot of important libraries are written in C++ unfortunately, and ObjC is needed for calling into macOS system APIs - although this could also be done more awkwardly by going through the ObjC C runtime). The one big downside of embedding Clang and the whole of LLVM is the compiler's executable size (somewhere around 170 MB).
- cylinder714 4y agoWalter, since you're here: what's your position regarding bootstrapping and Ken Thompson's "Reflections on Trusting Trust"?
- WalterBright 4y agoI haven't read it.
- deleted 4y ago[deleted]
- KerrAvon 4y agoThe D compiler will steal your passwords, but only for old VAX BSD versions of passwd, so Walter can log in and fix your K&R C bugs while you're sleeping. your daily edition of WalterBrightFacts™
- tzs 4y agoHmmm...once at Caltech someone got into my account on the PDP-10 and all they did, as far as I could tell, was fix some bugs in a program of mine I was having trouble with. I never did find out who did it. I wonder if it was Walter? I was an overly obnoxious frosh at the time and I'm certain I was quite annoying to the older students who frequented the terminal room such as Walter (I think he would have been a junior at the time). And I'm certain Walter was aware of me, because he had once either targeted me in a prank or used me in a prank against someone else (or perhaps both--I've never been clear on who was the target). The someone else was one of the system admins, Bill. Bill really liked playing Walter's game "Empire". So one day I was sitting at a terminal hacking away at something and I got a message via the system for sending a text message to another terminal from Bill telling me to not do what I just did again or I'd be in trouble and have my account suspended. I had no idea what he was talking about. A while later I got another message. Bill seemed really annoyed with me. I then got summoned to his office. He was really mad. He said I keep sending him rude messages, like telling him to stop playing Empire and get his fat ass back to work. I denied sending these messages and he was not buying it. And then another insult from me came through while I was there in his office. Bill then started to maybe believe me, and went to Walter's directory to look at the Empire source. Sure enough, Walter had recently added code that checked for Bill playing while I was also logged on, and would fake rude and insulting messages from me from my terminal to Bill. And that is why I will never use a compiler from Walter if there is any way the compiler might be able to tell I'm the one using it. :-)
- mort96 4y agoThe article's title is "Your next C compiler is a D compiler", which makes sense, though it's a bit clickbaity. The HN submission title is "Next C compiler is a D compiler", which barely makes grammatical sense, and to the extent that it does make sense, it sounds like there's a C compiler called "Next C compiler" which is also a D compiler.
- WalterBright 4y agoMy mistake. Thanks for pointing it out.
- throwamon 4y agoI don't think it's your mistake. It seems HN automatically removes certain leading/trailing words from titles so as to reduce clickbait but it doesn't seem to be very smart about it.
- bachmeier 4y agoIt gets annoying, because in cases like this it doesn't make sense, but in other cases it actually changes the meaning of the title. I honestly don't understand it given the prohibition against submitters changing the title.
- Tomte 4y agoMore importantly, this change is silent. I'd be much less annoyed by it if dang finally made it a pre-submit check (maybe with a checkbox "Overrule") like the "too long" check. But no, it seems to be important that the submitter has to check manually whether the title he just typed in is still there.
- deleted 4y ago[deleted]
- generichuman 4y agoDoes ImportC support extensions like attribute cleanup yet? It is basically the same thing as D's scope(exit). More broadly, are there plans to support C extensions that are available in GCC and clang?
- WalterBright 4y agoImportC already supports some extensions, what we figured were the most used ones. We plan to support others on an as-needed basis.
- stevefan1999 4y agoHello Walter, do you have any advice on integrating Rust and D? Especially regarding GC
- benreesman 4y agoRust is pretty clearly the ascendant language in this (big) niche. I forecast that as it’s pragmatic, industrial uses (inevitably) grow sharply, that the FFI story will just have to improve. Rust is going to be the next “low-level” language. The “Pure Rust” talking point will have to recede a bit but it will be a net win for Rust’s truly broad adoption, which is going to happen whether cranky old-timers like me like it or not :)
- benreesman 4y agoBitching about downvotes is generally frowned upon around here, but I’ll burn a little of my old-timer credit. How is acknowledging that Rust is clearly and obviously the next big thing an insult to anyone. It’s not my favorite language but it’s a good one and I know it’s going to displace C/C++ in my lifetime. What more does the downvote brigade want?
- newpavlov 4y agoUsing Rust code in D should be relatively easy: expose C API, compile it into a shared library and use it on the D side as any other library. cbindgen (https://github.com/eqrion/cbindgen https://github.com/eqrion/cbindgen) can help with generation of header files. Static linking is possible as well. Using D code in Rust probably will be much more painful and I am not sure if it can be done 100% safely...
- bachmeier 4y ago> Using D code in Rust probably will be much more painful and I am not sure if it can be done 100% safely... Can you elaborate? Saying it will be "much more painful" without anything further is not especially helpful. I've added D functions to C programs more or less trivially, including compiling to a shared library, with no change to the code.
- svnpenn 4y agoWhat I don't like about LDC, is you have to install the entire Visual Studio if you want a static build [1]. Contrast this with Go, Nim, Rust, Zig and others, that don't put this burden developers. Is DMD any different in this regard? 1. https://github.com/ldc-developers/ldc/issues/4047 https://github.com/ldc-developers/ldc/issues/4047
- WalterBright 4y agoDMD doesn't require anything else. Except for Win64, where it needs the VS C runtime library and linker.
- svnpenn 4y ago> VS C runtime library and linker is that included with the DMD installer? If not thats the same situation as LDC.
- gavinray 4y agoNote that the one feature holding this back from "true greatness" during development is that the tooling for providing LSP completions for D symbols in editors doesn't appear capable of detecting C code in your project yet One of the major appeals of ImportC is that it does away with needing to write (and maintain) D bindings to C libraries -- by hand, or generated. I was trying to experiment with io_uring in D and using liburing headers included in a C file + ImportC My D app compiled and ran fine, and I could call liburing but in my editor I was in the dark as to what the methods I could call or their signatures were. For hardcore vim programmers this is likely the norm, but for me I consider that a step backwards, and I went back to manually generating D bindings and using those. Waiting for the day we'll see ImportC fully integrated into dub and pure-d/workspace-d!
- isthisthingon99 4y ago> For hardcore vim programmers this is likely the norm Just FYI, not anymore.
- winrid 4y agoCan you tell the D compiler to free a pointer returned from a C method when it goes out of scope? Like uniq_ptr
- boundchecked 4y agoD has scopeguards similar to defer.
- WalterBright 4y agoD can do RAII.
- germandiago 4y agovia structs, right? As for classes you should not rely on it I recall.
- pjmlp 4y agoYou can stack allocate classes, and then they behave like structs. https://dlang.org/blog/2017/07/07/go-your-own-way-part-one-the-stack/ https://dlang.org/blog/2017/07/07/go-your-own-way-part-one-t...
- arunc 4y agoC has head const and it's pretty common in C code bases. From what I remember, D had transitive const and not head const, so how does ImportC deal with this now? Is head const now supported in DMD with ImportC? https://dlang.org/articles/const-faq.html https://dlang.org/articles/const-faq.html
- sylware 4y agoFor me, the next C compiler is a RISC-V assembler...