www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use templates in a separate library?

reply CrazyMan <superdanik2004 gmail.com> writes:
I have a separate library and some template interface in it

```d
interface IFoo(T)
{
      void setValue(const(T) value);
}
```

But when using it in the main program, it throws a linking error. 
I found that you can make a sourceLibrary that copies the code 
instead of creating a binary. But with it, I also get a linking 
error, and at the same time it is no longer associated with my 
interface, but with a third-party dependency (bindbc.opengl)

What can I do to fix this?
Jun 23 2022
next sibling parent frame <frame86 live.com> writes:
On Thursday, 23 June 2022 at 08:12:32 UTC, CrazyMan wrote:
 I have a separate library and some template interface in it

 ```d
 interface IFoo(T)
 {
      void setValue(const(T) value);
 }
 ```

 But when using it in the main program, it throws a linking 
 error. I found that you can make a sourceLibrary that copies 
 the code instead of creating a binary. But with it, I also get 
 a linking error, and at the same time it is no longer 
 associated with my interface, but with a third-party dependency 
 (bindbc.opengl)

 What can I do to fix this?
This is insufficient information to help you. - Separate library could mean multiple things: a compiler library, a static linked binary, a SO/DLL binary. - What error is thrown? - Are you using `extern` declarations? For a successful build the linker needs to get all symbols from any referenced source. The template isn't an actual symbol, it's just a information for the compiler to generate one. It says nothing about the symbol will be really generated or not. Also the linker needs to know if a library has to be used.
Jun 23 2022
prev sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Thursday, 23 June 2022 at 08:12:32 UTC, CrazyMan wrote:
linking
make sure you use the -i flag when compiling
Jun 23 2022
parent frame <frame86 live.com> writes:
On Thursday, 23 June 2022 at 23:50:42 UTC, monkyyy wrote:
 On Thursday, 23 June 2022 at 08:12:32 UTC, CrazyMan wrote:
linking
make sure you use the -i flag when compiling
But note, that would be the opposite of using a library.
Jun 24 2022