Linking a C program with D library

Mike Parker aldacron at gmail.com
Wed Aug 15 01:56:34 UTC 2018


On Tuesday, 14 August 2018 at 23:05:26 UTC, Joe wrote:
> I'm attempting a piecemeal conversion of some C programs. I've 
> converted a few that depend on C modules that are in a library 
> and now I'm sort of in the middle of converting those C 
> modules. One of them has an array of strings, i.e., array of 
> char*, which most easily translated to D's string[]. However, 
> the array is used by a function that expects a const char*, so 
> I had to use toStringz to pass those values.
>
> Now there are some C programs that haven't been converted yet, 
> but they depend on the function above. When linking those 
> programs, the linker complains about an undefined reference to 
> the mangled name of std.string.toStringz.
>
> What is the most appropriate way around this? Is there a way to 
> declare an array of const char* and initialize it to literal 
> strings? Or can the mangled name (and Phobos library) somehow 
> be made visible to the linker?

The correct thing to do is to keep the original C function 
signatures in the converted code, i.e. don't change char* to 
string[]. And don't use anything from Phobos internally that 
requires linking. In other words, treat your converted code as C 
code in a D module as much as possible. Only when the conversion 
is complete and everything is in D do you start pulling in the D 
features.


More information about the Digitalmars-d-learn mailing list