Thread overview
How to use templates in a separate library?
Jun 23, 2022
CrazyMan
Jun 23, 2022
frame
Jun 23, 2022
monkyyy
Jun 24, 2022
frame
June 23, 2022

I have a separate library and some template interface in it

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?

June 23, 2022

On Thursday, 23 June 2022 at 08:12:32 UTC, CrazyMan wrote:

>

I have a separate library and some template interface in it

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.

June 23, 2022

On Thursday, 23 June 2022 at 08:12:32 UTC, CrazyMan wrote:

>

linking

make sure you use the -i flag when compiling

June 24, 2022

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.