D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12546 - DMD does not generate required symbols for linker
Summary: DMD does not generate required symbols for linker
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-04-08 07:05 UTC by Tomer Filiba (weka)
Modified: 2024-12-13 18:19 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomer Filiba (weka) 2014-04-08 07:05:29 UTC
When using templates defined in another module (via an alias), all the files must be compiled together or link errors ensue. For example:

Compile together
$ dmd main.d mymodule.d && echo "ok"
ok

Compile separately
$ dmd -c main.d && dmd -c mymodule.d && dmd main.o mymodule.o
main.o:(.data._D78TypeInfo_S8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifier6__initZ+0x40): undefined reference to `_D8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifier8__xopCmpFKxS8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifierKxS8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifierZi'
main.o:(.data._D78TypeInfo_S8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifier6__initZ+0x48): undefined reference to `_D8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifier8toStringMxFZAya'
collect2: error: ld returned 1 exit status
--- errorlevel 1

This screws up our build system (which builds each file separately)
Here is the code for reproducing this error:

=============================================
main.d
=============================================
import mymodule;

void main()
{
    int dict[MyType];
    auto x = dict[MyType(0)];
}

=============================================
mymodule.d
=============================================
module mymodule;

import std.string;

struct TypedIdentifier(string NAME) {
    int value;

    this(int val) {
        value = val;
    }
    int opCmp(ref const int rhs) const {
        return value > rhs ? 1 : (value < rhs ? -1 : 0);
    }
    string toString() const {
        return format("%s(%s)", NAME, value);
    }
}

alias MyType = TypedIdentifier!"MyType";
Comment 1 Tomer Filiba (weka) 2014-04-16 12:39:22 UTC
Further examination shows that it's caused by the automatic addition of function attributes:

=================================
$ nm mymodule.o | grep TypedIdentifier | grep toString
_D8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifier8toStringMxFNaNfZAya

$ nm main.o | grep TypedIdentifier | grep toString
_D8mymodule40__T15TypedIdentifierVAyaa6_4d7954797065Z15TypedIdentifier8toStringMxFZAya
=================================

NaNf = pure @safe (according to http://dlang.org/abi.html )

When the `alias` sits in mymodule.d, toString gets qualified with `pure @safe`, while when it's invoked in main.d, it only looks for `const`. If I move the alias into main.d, or just skip the alias and use the full type, everything's fine. 

This seems like a bug in the alias mechanism of the core compiler.
Comment 2 Rainer Schuetze 2014-10-30 07:50:54 UTC
I guess this is another instance of Issue 10442.
Comment 3 dlangBugzillaToGithub 2024-12-13 18:19:41 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18813

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB