What is this undefined reference with -betterC about?

jfondren julian.fondren at gmail.com
Wed Jun 16 14:21:40 UTC 2021


Here's a complete script that you can run right now, using
a dub module that I just updated:

```d
#!/usr/bin/env dub
/+ dub.sdl:
     dependency "hostname" version="~>0.1.1"
     buildOptions "betterC"
+/

extern(C) void main() {
     import hostname : hostnamez;
     import core.stdc.stdio : printf;

     printf("hostname: %s", hostnamez);
}
```

... which fails:

```
/usr/bin/ld: 
/tmp/.dub/build/hostnameex2-~master/application-debug-linux.posix-x86_64-dmd_v2.097.0-96F370DD71342805A23ECDFD2C6CCE6C/hostnameex2.o: in function `main':
/home/jfondren/mars/learn/./hostnameex2.d:11: undefined reference 
to `_D8hostname9hostnamezPa'
```

I've rm -rf'd /tmp/.dub and ~/.dub/packages , I've gotten things 
to a point
where I could modify dub's cached hostname/source.d and confirm 
that e.g.
pragma(msg, hostnamez.mangleof) has exactly the same output in 
that file
as in the script above. This all works fine if instead of a dub 
dependency
I add the path to hostname.d in dflags.

And this script works fine, without betterC:

```d
#!/usr/bin/env dub
/+ dub.sdl:
     dependency "hostname" version="~>0.1.1"
+/

void main() {
     import std.stdio : writeln;
     import hostname : hostname, hostnamez;
     import std.string : fromStringz;

     writeln("hostname: ", hostname);
     writeln("hostname: ", hostnamez.fromStringz);
}
```

Why isn't this linking?


More information about the Digitalmars-d-learn mailing list