core.stdc and betterC

Jacob Carlborg doob at me.com
Sun Apr 29 15:40:20 UTC 2018


On 2018-04-29 16:42, dd886k wrote:
> Hello!
> 
> This is my first time posting, so do feel free to correct me and this post.
> 
> I started writing in D around Q2 2017 and recently been re-writing some 
> of my projects as betterC (entirely!).
> 
> I noticed something weird. It started with DMD 2.074.0 and it's still an 
> issue for me in DMD 2.079.1 (and any other compiler really).
> 
> Consider this piece:
> 
> ```
> import core.stdc.stdio;
> 
> extern(C) void main() {
>      printf("a");
> }
> ```
> 
> It compiles and runs absolutely wonderful (OMF, MSCOFF, ELF, etc.). No 
> issues.
> 
> Now consider this piece:
> 
> ```
> import core.stdc.stdio;
> 
> extern(C) void main() {
>      putchar('a');
> }
> ```
> 
> Oops, `Error 42: Symbol Undefined 
> __D4core4stdc5stdio7putcharFNbNiNeiZi`! To my surprise, `putchar` is 
> extern'd as D in druntime/src/core/stdc/stdio.d, and I find that _really 
> silly_.
> 
> Which means `putchar`, during linking, is affected for linking every 
> single C runtime out there, and the only reason I can think of is to 
> easily integrate it with D in general, which in my opinion, is not 
> necessarily needed since the D people (not meant as an insult) promotes 
> betterC as a stand-alone option.
> 
> On Windows, stdin, stdout, and stderr are affected when using -m32mscoff 
> and -m64 (and obviously, LDC) because under CRuntime_Microsoft, std* are 
> defined as `shared`.
> 
> I'm aware that an easy solution would be defining a version (D_betterC) 
> section.
> 
> Will I do a Pull Request? Unfortunately no, I fear I'll abandon mid-way 
> through. It's easier to advise the forums and let an actual professional 
> integrate the fix.
> 
> If you have other suggestions, I'm all ears.

Looks like "putchar" is inlined [1]. That means the "putchar" you're 
referencing is not the one in the C standard library but it's 
implemented in druntime. That means you need to link with 
druntime/phobos, it's not enough to link with the C standard library.

I don't know why it was done this way. Perhaps it's just a macro on some 
platforms.

[1] 
https://github.com/dlang/druntime/blob/master/src/core/stdc/stdio.d#L1289

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list