digitalmars.D.learn - betterC DLL in Windows
- Tamas (54/54) Feb 04 2023 Hi,
- Richard (Rikki) Andrew Cattermole (9/9) Feb 04 2023 You don't have access to druntime/Phobos stuff at runtime.
- Tamas (33/42) Feb 04 2023 I'm not sure I understand every bit of it yet, but this looks
- Richard (Rikki) Andrew Cattermole (6/7) Feb 04 2023 DMD is currently a write off for DLL support (some things do work, but
- bachmeier (8/9) Feb 04 2023 Anyone that cares about performance will use LDC rather than DMD.
- Tamas (3/13) Feb 05 2023 Good to know, thank you. The posts I've seen so far online
- Adam D Ruppe (7/14) Feb 04 2023 then don't use the switch lol
- Tamas (15/29) Feb 04 2023 Hi, thanks for the feedback. I'm happy to see 2 replies so
- Adam D Ruppe (14/18) Feb 04 2023 Then you'd probably be better off taking my word for it (or even
- Tamas (13/26) Feb 04 2023 I do take your word for it, but now I have to re-evaluate my
- bachmeier (9/31) Feb 04 2023 I'm not sure what Adam's getting at when he says "hopelessly
- Adam D Ruppe (9/11) Feb 04 2023 You're almost guaranteed to hit some wall with it and have no
- Adam D Ruppe (60/68) Feb 04 2023 This is achieved without the betterC switch, and severely limited
- Richard (Rikki) Andrew Cattermole (8/11) Feb 04 2023 This only affects you if you use full D to interact with -betterC code.
- Tamas (20/32) Feb 05 2023 I'm repeating myself, but I'm very happy to see so many helpful
- Richard (Rikki) Andrew Cattermole (16/22) Feb 05 2023 So LDC with druntime and yes GC turned on is good enough right now that
- Tamas (5/15) Feb 06 2023 Sounds quite convincing, I think I might do it this way, instead
- Tamas (6/14) Feb 05 2023 I appreciate all of this... however, as a newcomer, I wish the
- bachmeier (10/16) Feb 06 2023 Technically, those pages are [the spec rather than
- H. S. Teoh (8/17) Feb 06 2023 IIRC the spec was started as part of an ongoing effort to fully specify
- Tamas (4/10) Feb 06 2023 I see... I guess I was confused by the title of the link,
- ryuukk_ (12/12) Feb 04 2023 In betterC mode you don't have access to the standard library or
Hi, I'm new to D and trying my hands on a few simple things. Being able to build a Windows DLL with betterC enabled is of particular interest to me, as I'd need it for a project that I'd like to do in D. I'm trying to follow some examples, such as [this](https://wiki.dlang.org/Win32_DLLs_in_D) and [this](https://tekmoji.com/?post=Calling%20D%20(dlang)%20shared%20libs%20from%20Pyt on%20on%20Windows), and they compile without `-betterC`, but fail with link errors when using the switch. example DLL, or see 2nd link for one without the mixin ```D //WindowsApp1.d module WindowsApp1; import core.sys.windows.windows; import core.sys.windows.dll; export extern (C) void main() { import core.stdc.stdio : printf; printf("Hello betterC\n"); } mixin SimpleDllMain; ``` then ``` dmd -m64 -shared -betterC WindowsApp1.d ``` yields error: ``` error LNK2019: unresolved external symbol _D4core3sys7windows3dll18dll_process_attachFPvbZb referenced in function DllMain error LNK2019: unresolved external symbol _D4core3sys7windows3dll18dll_process_detachFPvbZv referenced in function DllMain error LNK2019: unresolved external symbol _D4core3sys7windows3dll17dll_thread_attachFbbZb referenced in function DllMain error LNK2019: unresolved external symbol _D4core3sys7windows3dll17dll_thread_detachFbbZb referenced in function DllMain ``` I'm not a complete beginner to programming, so I'm guessing the linker is missing a library, but as I'm not familiar with D yet, and these are fairly basic examples that I expected to run without a hitch, I'm a bit confused as to what I should be doing. Shouldn't the system modules provide the link targets themselves? Should I add a parameter to include a windows system library? BtW, I've tried compiling from the command line and from Visual Studio using the visuald extension - I'm assuming the extension makes sure the environment variables are set correctly. When using dmd from the command line, I also tried invoking `dmd2vars64.bat` and `vcvars64.bat` before running dmd.exe to ensure the variables are set. In any case, the build process is successful without `betterC`, so I'm assuming the path/variables are set correctly.
Feb 04 2023
You don't have access to druntime/Phobos stuff at runtime. SimpleDllMain is for initializing and uninitializing druntime. See: https://github.com/Project-Sidero/basic_memory/blob/main/source/sidero/base/dllmain.d You want to only use ldc atm. The defaults right now should most likely "just work" in dub for a single compile + link step (i.e. only a single static library in dll). If you do it manually, you'll want to add the flag ``--fvisibility=public`` to export everything.
Feb 04 2023
On Saturday, 4 February 2023 at 16:51:36 UTC, Richard (Rikki) Andrew Cattermole wrote:You don't have access to druntime/Phobos stuff at runtime. SimpleDllMain is for initializing and uninitializing druntime. See: https://github.com/Project-Sidero/basic_memory/blob/main/source/sidero/base/dllmain.d You want to only use ldc atm. The defaults right now should most likely "just work" in dub for a single compile + link step (i.e. only a single static library in dll). If you do it manually, you'll want to add the flag ``--fvisibility=public`` to export everything.I'm not sure I understand every bit of it yet, but this looks helpful, thank you. I was able to compile this (based on dllmain.d linked by you) with both DMD and LDC: ```D module WindowsApp1; version (Windows) { version (D_BetterC) { import core.sys.windows.windef : HINSTANCE, BOOL, DWORD, LPVOID; extern (Windows) BOOL DllMain(HINSTANCE hInstance, DWORD ulReason, LPVOID reserved) { return true; } } else { import core.sys.windows.dll; mixin SimpleDllMain; } } export extern (C) void foo() { import core.stdc.stdio : printf; printf("Hello betterC\n"); } ``` What's the reason to prefer LDC over DMD?
Feb 04 2023
On 05/02/2023 7:29 AM, Tamas wrote:What's the reason to prefer LDC over DMD?DMD is currently a write off for DLL support (some things do work, but its very easy to hit situations that don't). I've been looking into this for the past year, implemented some things in dub to get it to work, also currently working on the redesign of export :)
Feb 04 2023
On Saturday, 4 February 2023 at 18:29:41 UTC, Tamas wrote:What's the reason to prefer LDC over DMD?Anyone that cares about performance will use LDC rather than DMD. It's hard to imagine a case where someone would want betterC to avoid the GC, but they wouldn't want to use LDC. When I started using D many years ago, LDC was behind DMD, so you needed to use DMD to have a current compiler. That's no longer true. The only reason I use DMD today is the faster compilation speed when I'm doing a lot of write-compile-debug iterations.
Feb 04 2023
On Saturday, 4 February 2023 at 19:44:15 UTC, bachmeier wrote:On Saturday, 4 February 2023 at 18:29:41 UTC, Tamas wrote:Good to know, thank you. The posts I've seen so far online suggesting to start with DMD must've been outdated, then.What's the reason to prefer LDC over DMD?Anyone that cares about performance will use LDC rather than DMD. It's hard to imagine a case where someone would want betterC to avoid the GC, but they wouldn't want to use LDC. When I started using D many years ago, LDC was behind DMD, so you needed to use DMD to have a current compiler. That's no longer true. The only reason I use DMD today is the faster compilation speed when I'm doing a lot of write-compile-debug iterations.
Feb 05 2023
On Saturday, 4 February 2023 at 16:45:31 UTC, Tamas wrote:and they compile without `-betterC`, but fail with link errors when using the switch.then don't use the switch lol -betterC is barely supported and completely useless so better to just not use it.export extern (C) void main() mixin SimpleDllMain;No need to ever mix two mains together, the DllMain is the required one.error LNK2019: unresolved external symbol _D4core3sys7windows3dll18dll_process_attachFPvbZb referenced in function DllMainThis is just betterC breaking the code.
Feb 04 2023
Hi, thanks for the feedback. I'm happy to see 2 replies so quickly. On Saturday, 4 February 2023 at 17:26:17 UTC, Adam D Ruppe wrote:On Saturday, 4 February 2023 at 16:45:31 UTC, Tamas wrote:Indeed, that is an oversight on my part. However, that's just a typo I happened to include in my last attempt. I use foo() and others, and .def exporting them etc. Tried various things based on the examples I linked.export extern (C) void main() mixin SimpleDllMain;No need to ever mix two mains together, the DllMain is the required one.Well, as I'm new to D this isn't something I have insight into. However, this being and advertised feature of the language, I'm surprised to see that you dismiss it so casually. Also, for my project, I mostly need interop with C libraries and D would only serve as a glue, so I don't really need several features like GC etc. `betterC`, at least what I thought it was supposed to be, seemed to be a good fit for what I need.and they compile without `-betterC`, but fail with link errors when using the switch.then don't use the switch lol -betterC is barely supported and completely useless so better to just not use it.Perhaps, but this doesn't help me much.error LNK2019: unresolved external symbol _D4core3sys7windows3dll18dll_process_attachFPvbZb referenced in function DllMainThis is just betterC breaking the code.
Feb 04 2023
On Saturday, 4 February 2023 at 18:11:05 UTC, Tamas wrote:Well, as I'm new to D this isn't something I have insight into.Then you'd probably be better off taking my word for it (or even trusting your own limited experience where things worked until you added the switch) and just not using -betterC switch. It is hopelessly broken, but thankfully, it also brings zero benefit, so simply not using it is a viable path forward.I mostly need interop with C libraries and D would only serve as a glue, so I don't really need several features like GC etc.Just don't call those functions and they won't hurt you, aside from adding ~200 KB of size to the dll. On the other hand, the -betterC switch is hurting you - as evidenced by your own attempt working until you added it.Perhaps, but this doesn't help me much.It is a very easy solution to your problem. It is up to you if you want to take it and continue on in productivity or keep suffering for no benefit (and you'll find more trouble the further you go using the broken, barely supported switch).
Feb 04 2023
On Saturday, 4 February 2023 at 18:27:34 UTC, Adam D Ruppe wrote:On Saturday, 4 February 2023 at 18:11:05 UTC, Tamas wrote:I do take your word for it, but now I have to re-evaluate my expectations towards D and perhaps use it for another project. I've got most of my project working in C already, but I was hoping to add some safety and better readability/managability by using some of the convenient features D offers over C. And, of course, learn D in the process. Also, your words give me the impression that I cannot trust the documentation; which isn't a great start into the learning process.Well, as I'm new to D this isn't something I have insight into.Then you'd probably be better off taking my word for it (or even trusting your own limited experience where things worked until you added the switch) and just not using -betterC switch. It is hopelessly broken, but thankfully, it also brings zero benefit, so simply not using it is a viable path forward.It can be evidence of something broken in D as you say (simplified) or of my lack of experience with D - a simple missing include, badly configured PATH, or lack of understanding on my part what SimpleDllMain does.I mostly need interop with C libraries and D would only serve as a glue, so I don't really need several features like GC etc.Just don't call those functions and they won't hurt you, aside from adding ~200 KB of size to the dll. On the other hand, the -betterC switch is hurting you - as evidenced by your own attempt working until you added it.
Feb 04 2023
On Saturday, 4 February 2023 at 18:40:51 UTC, Tamas wrote:I'm not sure what Adam's getting at when he says "hopelessly broken" but it's basically a subset of D. The main reason I've seen people wanting betterC is to avoid the GC. Well, you don't need betterC for that, you use nogc and then you don't have to worry about the GC.It is hopelessly broken, but thankfully, it also brings zero benefit, so simply not using it is a viable path forward.I do take your word for it, but now I have to re-evaluate my expectations towards D and perhaps use it for another project. I've got most of my project working in C already, but I was hoping to add some safety and better readability/managability by using some of the convenient features D offers over C. And, of course, learn D in the process. Also, your words give me the impression that I cannot trust the documentation; which isn't a great start into the learning process.I think the point is that it works without the betterC switch, so it's not your lack of experience that's the problem, it's the functionality that you lose when you use that switch.It can be evidence of something broken in D as you say (simplified) or of my lack of experience with D - a simple missing include, badly configured PATH, or lack of understanding on my part what SimpleDllMain does.I mostly need interop with C libraries and D would only serve as a glue, so I don't really need several features like GC etc.Just don't call those functions and they won't hurt you, aside from adding ~200 KB of size to the dll. On the other hand, the -betterC switch is hurting you - as evidenced by your own attempt working until you added it.
Feb 04 2023
On Saturday, 4 February 2023 at 19:49:41 UTC, bachmeier wrote:I'm not sure what Adam's getting at when he says "hopelessly broken" but it's basically a subset of D.You're almost guaranteed to hit some wall with it and have no solution. Some of these are bugs but some of them are by design; betterC is a set of arbitrary restrictions without much consistent reasoning behind them. For example, with dlls, you still have to deal with all the potential problems of C library version mismatches, but if you use any module that touches a D feature it is likely to compile but then fail to link when someone uses it! All pain, no gain.
Feb 04 2023
On Saturday, 4 February 2023 at 18:40:51 UTC, Tamas wrote:I do take your word for it, but now I have to re-evaluate my expectations towards D and perhaps use it for another project. I've got most of my project working in C already, but I was hoping to add some safety and better readability/managability by using some of the convenient features D offers over C.This is achieved without the betterC switch, and severely limited with it.Also, your words give me the impression that I cannot trust the documentation; which isn't a great start into the learning process.There's a lot of things described in the documentation that don't actually work. D can be an *extremely* productive language if you know which parts to focus on, but most the newer hyped features just don't deliver. The table of contents on the left side of the site is in roughly chronological order. The things near the top are pretty reliable, with a few exceptions (which are generally called out in the documentation if you read it carefully, like how the "static initialization of associative arrays" is "not yet implemented", or misuse of shared is "is not an error yet by default"). The things near the bottom are not reliable. The C++ interface works in some cases but you aren't likely to get far using it in any real world project; the C++ stdlib bindings are extremely minimal and Windows-only. The Objective-C interface works beautifully on dmd.... which cannot target the new Apple devices. ldc can generate code for those arm chips, but the Objective-C interface is not implemented on ldc. The portability guide and named character entities are pretty much ok. The memory safety ` safe` stuff only actually works if you 1) opt into it in the functions and 2) opt into it on command line params. This is mentioned in the docs as a small note near the bottom. The design of safe also allows several things that can escape it on certain systems. The ABI page is ok, but there are several bugs in the debug info output. I find it good enough but it can be annoying. The vector extensions work on dmd if you can piece together the magic but the other compilers do it differently. The betterC page has several falsehoods on it, including the misinformation that you need to use it to link into C applications. This is just 100% nonsense. It also claims nearly the full language remains available, but this is also simply false. Some of the things listed there can be made to work with a bunch of code, but many things just plain don't work, even if you add the small amounts of code necessary to enable them. For example, interfaces don't work, even if you implement your own dynamic cast function. It just doesn't let them link. It claims constructors and destructors work, which is true, unless they're `static`, in which case you get random errors. Delegates and lambdas work if you manage captured variables in your own functors, but otherwise are simply disabled, even if you had your own backing system. Even module imports can fail because betterC disables outputting the module data info, even if it would otherwise be required by language rules, despite it not using the druntime. Then importC only works for toy examples. Using two separate headers or modules will result in spurious compile errors (and bugs detailing how to address this have been closed WONTFIX), and there's several kinds of C apis it just doesn't support. And finally, the live functions also only work in toy examples and the design is fundamentally limited to only direct function calls, no struct aggregation is supported at all. It is a complete dead end. On the other hand, if you avoid most the compiler switches and stick to the more solid features - "Interfacing to C" and above on the list, for the most part - you'll find D is a very capable conservative language.
Feb 04 2023
On 05/02/2023 1:20 PM, Adam D Ruppe wrote:Even module imports can fail because betterC disables outputting the module data info, even if it would otherwise be required by language rules, despite it not using the druntime.This only affects you if you use full D to interact with -betterC code. Even then it is very easy to work around. https://github.com/Project-Sidero/basic_memory/blob/main/source/sidero/base/moduleinfostubs.d -betterC DLL's do work with full D executables. If all you need it for is gluing some stuff together don't listen to Adam about it not working, because it absolutely does and quite usable if you're not expecting there to be a GC.
Feb 04 2023
On Sunday, 5 February 2023 at 00:27:19 UTC, Richard (Rikki) Andrew Cattermole wrote:On 05/02/2023 1:20 PM, Adam D Ruppe wrote:I'm repeating myself, but I'm very happy to see so many helpful responses to my newbie question. It gives me a good feeling about the community here. As I wrote, I'd like to use D only in a limited way, for now. My project actually interfaces Python and glib/gstreamer, the glue being provided by my C code. What I'd like to do is to improve on my C code, inspired by [this interview](https://dlang.org/blog/2017/08/23/d-as-a-better-c/). I don't want to introduce another GC or runtime into the picture, and I probably don't need to call any D libraries. Python -> my C code -> glib/gstreamer C libs to Python -> C improved by betterC -> glib/gstreamer C libs On the surface, betterC seems to be perfect for this case. How would YOU do it (Adam, Richard)? BtW, gstreamer also has D bindings, and maybe in the future I'll use those. I suspect that Adam's suggestions have a stronger relevance to that case, right?Even module imports can fail because betterC disables outputting the module data info, even if it would otherwise be required by language rules, despite it not using the druntime.This only affects you if you use full D to interact with -betterC code. Even then it is very easy to work around. https://github.com/Project-Sidero/basic_memory/blob/main/source/sidero/base/moduleinfostubs.d -betterC DLL's do work with full D executables. If all you need it for is gluing some stuff together don't listen to Adam about it not working, because it absolutely does and quite usable if you're not expecting there to be a GC.
Feb 05 2023
On 05/02/2023 10:01 PM, Tamas wrote:On the surface, betterC seems to be perfect for this case. How would YOU do it (Adam, Richard)? BtW, gstreamer also has D bindings, and maybe in the future I'll use those. I suspect that Adam's suggestions have a stronger relevance to that case, right?So LDC with druntime and yes GC turned on is good enough right now that you can probably make it work without too much effort. You don't necessarily have to limit yourself to -betterC. In fact if you don't, you have access to PyD[0] which can handle the interop stuff for you. I haven't personally used it, but it might work for your use case :) The main thing is no matter what flags you pass, you'll need to be careful about memory ownership between the language divide and the ownership therein. You have already got a hang of that though from the C version! Just gotta be extra careful with GC memory that its pinned on the D side and won't get free'd elsewhere. But yeah if you can avoid having to create bindings for stuff (such as glib which we have bindings for), or wrappers ext. do so. That way you can do more of the fun things! [0] https://github.com/ariovistus/pyd
Feb 05 2023
On Sunday, 5 February 2023 at 19:00:16 UTC, Richard (Rikki) Andrew Cattermole wrote:So LDC with druntime and yes GC turned on is good enough right now that you can probably make it work without too much effort. You don't necessarily have to limit yourself to -betterC. In fact if you don't, you have access to PyD[0] which can handle the interop stuff for you. I haven't personally used it, but it might work for your use case :) [...] But yeah if you can avoid having to create bindings for stuff (such as glib which we have bindings for), or wrappers ext. do so. That way you can do more of the fun things!Sounds quite convincing, I think I might do it this way, instead of with betterC. Not needing additional python bindings does sound like a big plus. Thanks.
Feb 06 2023
On Sunday, 5 February 2023 at 00:20:24 UTC, Adam D Ruppe wrote:There's a lot of things described in the documentation that don't actually work. D can be an *extremely* productive language if you know which parts to focus on, but most the newer hyped features just don't deliver. The table of contents on the left side of the site is in roughly chronological order. The things near the top are pretty reliable, [...]. The things near the bottom are not reliable. [...] and so onI appreciate all of this... however, as a newcomer, I wish the docs were simply more honest, instead of representing wishful thinking. I guess in any programming language, experience reveals things not present in the docs, but it seems to apply much more acutely here.
Feb 05 2023
On Sunday, 5 February 2023 at 08:48:34 UTC, Tamas wrote:I appreciate all of this... however, as a newcomer, I wish the docs were simply more honest, instead of representing wishful thinking. I guess in any programming language, experience reveals things not present in the docs, but it seems to apply much more acutely here.Technically, those pages are [the spec rather than documentation](https://dlang.org/spec/spec.html).This is the specification for the D Programming Language.I've been bitten by that a few times over the years, though to be honest, I'm not sure of the relationship of the spec to documentation. The Phobos documentation and compiler documentation appear to be actual documentation, in the sense that you can trust it to be accurate, and if not it's a bug. Maybe someone that has been around from the earliest days understands the goal of the spec.
Feb 06 2023
On Mon, Feb 06, 2023 at 03:54:40PM +0000, bachmeier via Digitalmars-d-learn wrote:On Sunday, 5 February 2023 at 08:48:34 UTC, Tamas wrote:[...]IIRC the spec was started as part of an ongoing effort to fully specify the language so that, at least in theory, someone could read the spec and implement a D compiler completely independent of the current ones. T -- INTEL = Only half of "intelligence".This is the specification for the D Programming Language.I've been bitten by that a few times over the years, though to be honest, I'm not sure of the relationship of the spec to documentation. The Phobos documentation and compiler documentation appear to be actual documentation, in the sense that you can trust it to be accurate, and if not it's a bug. Maybe someone that has been around from the earliest days understands the goal of the spec.
Feb 06 2023
On Monday, 6 February 2023 at 15:54:40 UTC, bachmeier wrote:I see... I guess I was confused by the title of the link, "Language Reference" on the Docs page, and didn't look carefully at the URL or the text after that.This is the specification for the D Programming Language.I've been bitten by that a few times over the years, though to be honest, I'm not sure of the relationship of the spec to documentation. The Phobos documentation and compiler documentation appear to be actual documentation, in the sense that you can trust it to be accurate, and if not it's a bug.
Feb 06 2023
In betterC mode you don't have access to the standard library or the runtime You can only access the libc functions Basically the modules from: ``import core.stdc`` So for your example, just do like you'd do in C As simple as that As for DMD/LDC, easy: DMD: reference compiler, fast compile time LDC: LLVM based compiler, LLVM is slow, but provides great optimizations It's common to use DMD for debug, fast iteration time, and LDC for release builds, so you en enjoy great performance
Feb 04 2023