www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias using Complex!Double in module ... linker error??

reply james.p.leblanc <james.p.leblanc gmail.com> writes:
I am getting linker errors with this stripped-down example:

-------------------------------------------
**my_main.d:**

import std.stdio;
import std.complex;
import my_module;


     void main(){
           my_TYPE xxx;
           writeln(xxx);
     }

-------------------------------------------
**my_module.d:**

module my_module;

import std.complex;

alias my_TYPE = Complex!double;    *// this causes link error: 
"undefined reference"*

/* alias my_TYPE = double; */      *// this works fine*

------------------------------------------

Why does the linker fail when I alias to the Complex!double ...
but would work fine when alias to the double ??

Any help to understand what is greatly appreciated.
James
Aug 03 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 3 August 2021 at 21:40:09 UTC, james.p.leblanc wrote:
 I am getting linker errors with this stripped-down example:

 -------------------------------------------
 **my_main.d:**

 import std.stdio;
 import std.complex;
 import my_module;


     void main(){
           my_TYPE xxx;
           writeln(xxx);
     }

 -------------------------------------------
 **my_module.d:**

 module my_module;

 import std.complex;

 alias my_TYPE = Complex!double;    *// this causes link error: 
 "undefined reference"*

 /* alias my_TYPE = double; */      *// this works fine*

 ------------------------------------------

 Why does the linker fail when I alias to the Complex!double ...
 but would work fine when alias to the double ??

 Any help to understand what is greatly appreciated.
 James
The alias to Complex!double is a template instantiation. A template instantiation creates a symbol that needs to be linked. So you need to compile my_module.d along with my_main.d. ``` dmd my_main.d my_module.d ``` Or alternatively: ``` dmd -i my_main.d ``` double is a built-in type, so that alias doesn't create any symbols that need linking. An alias in and of itself is a compile-time-only construct, but the symbols you assign it might require linking something.
Aug 03 2021
parent james.p.leblanc <james.p.leblanc gmail.com> writes:
On Wednesday, 4 August 2021 at 01:10:15 UTC, Mike Parker wrote:
 On Tuesday, 3 August 2021 at 21:40:09 UTC, james.p.leblanc 
 wrote:
 [...]
The alias to Complex!double is a template instantiation. A template instantiation creates a symbol that needs to be linked. So you need to compile my_module.d along with my_main.d. ``` dmd my_main.d my_module.d ``` Or alternatively: ``` dmd -i my_main.d ``` double is a built-in type, so that alias doesn't create any symbols that need linking. An alias in and of itself is a compile-time-only construct, but the symbols you assign it might require linking something.
Mike, Aha... that makes sense now! Thanks kindly for helping me understand what was happening with your informative reply. The fact of template instantiations and symbols had escaped me completely. I appreciate it. Best Regards, James
Aug 03 2021