April 20, 2003
I'm getting a linker error while trying to make
a library that uses a template.


---------------------------------------------------------
Here is the template library file: temp.d
---------------------------------------------------------

Z:\D\errreport1>type temp.d

module temp;


template Foo(Type)
{
  bit isSorted(Type[] array, int delegate(Type a,Type b) cmp)
  {
    for(int i = 1; i < array.length; ++i)
    {
      if(cmp(array[i-1],array[i]) > 0)
        return false;
    }

    return true;
  }


  void insertionSort(Type array[], int delegate(Type a,Type b) cmp)
  in
  {
    assert(cmp);
  }
  out
  {
    assert(isSorted(array,cmp));
  }
  body
  {
    for (int i=1; i < array.length; i++)
    {
      Type index = array[i];
      int j = i;
      while ((j > 0) && cmp(index,array[j-1]) == -1)
      {
        array[j] = array[j-1];
        j = j - 1;
      }
      array[j] = index;
    }
  }
}

---------------------------------------------------------
Compile it and make it into a library
---------------------------------------------------------

Z:\D\errreport1>dmd -c temp.d

Z:\D\errreport1>lib -c temp.lib temp.obj
Digital Mars Librarian Version 8.00n
Copyright (C) Digital Mars 2000-2002 All Rights Reserved
www.digitalmars.com
Digital Mars Librarian complete.
---------------------------------------------------------
Here is the library that uses the template: usetemp.d
---------------------------------------------------------

Z:\D\errreport1>type usetemp.d

module usetemp;


import temp;


alias instance Foo(int).insertionSort intSort;


void bar(int[] arr)
{
  intSort(arr,
    delegate int(int a,int b)
    {
      if(a < b) return -1;
      else if(a == b) return 0;
      else return 1;
    });
}


---------------------------------------------------------
Compile it and make it into a library
---------------------------------------------------------

Z:\D\errreport1>dmd -c usetemp.d

Z:\D\errreport1>lib -c usetemp.lib usetemp.obj
Digital Mars Librarian Version 8.00n
Copyright (C) Digital Mars 2000-2002 All Rights Reserved
www.digitalmars.com
Digital Mars Librarian complete.
---------------------------------------------------------
Here is the test file
---------------------------------------------------------

Z:\D\errreport1>type err.d


import usetemp;


int main(char[][] argv)
{
  int[] test;
  test ~= 5;
  test ~= 12;
  test ~= 1;
  test ~= 3;
  test ~= 15;
  test ~= 45;
  test ~= 12;
  test ~= 9;

  bar(test);

  return 0;
}

---------------------------------------------------------
Try to put it all together and get a linker error
---------------------------------------------------------

Z:\D\errreport1>dmd err.d usetemp.lib temp.lib Z:\tools\dmd\bin\link.exe err,,,usetemp.lib+temp.lib+user32+kernel32/noi;

OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

temp.lib
 Warning 140: Library probably needs FIXLIB
usetemp.lib(usetemp)
 Error 42: Symbol Undefined _assert_temp
usetemp.lib(usetemp)
 Error 42: Symbol Undefined _array_temp
--- errorlevel 2
---------------------------------------------------------
Try compiling in the template directly same problem
---------------------------------------------------------

Z:\D\errreport1>dmd err.d usetemp.lib temp.d Z:\tools\dmd\bin\link.exe err+temp,,,usetemp.lib+user32+kernel32/noi;

OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

usetemp.lib(usetemp)
 Error 42: Symbol Undefined _assert_temp
usetemp.lib(usetemp)
 Error 42: Symbol Undefined _array_temp
--- errorlevel 2
---------------------------------------------------------
Finally this works
---------------------------------------------------------

Z:\D\errreport1>dmd err.d usetemp.d temp.d Z:\tools\dmd\bin\link.exe err+usetemp+temp,,,user32+kernel32/noi;
---------------------------------------------------------
DMD version
---------------------------------------------------------

Z:\D\errreport1>dmd
Digital Mars D Compiler Beta v0.61
Copyright (c) 1999-2003 by Digital Mars written by Walter Bright
www.digitalmars.com/d/index.html
Usage:
  dmd files.d ... { -switch }

  files.d        D source files
  -c             do not link
  -d             allow deprecated features
  -g             add symbolic debug info
  -gt            add trace profiling hooks
  -v             verbose
  -O             optimize
  -oobjdir       write .obj files to directory objdir
  -Ipath         where to look for imports
  -Llinkerflag   pass linkerflag to link
  -debug         compile in debug code
  -debug=level   compile in debug code <= level
  -debug=ident   compile in debug code identified by ident
  -inline        do function inlining
  -release	 compile release version
  -unittest      compile in unit tests
  -version=level compile in version code >= level
  -version=ident compile in version code identified by ident