Thread overview
How to use templates in a separate library?
5 days ago
CrazyMan
5 days ago
frame
5 days ago
monkyyy
4 days ago
frame
5 days ago

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?

5 days ago

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.

5 days ago

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

>

linking

make sure you use the -i flag when compiling

4 days ago

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.