0

I am trying to create a simple 2D library, consisting entirely of class templates. I created an empty project, changed configuration type to static library, and then added my header files. It currently looks like this:

enter image description here

When I try to build this project, the build succeeds but no .lib file is actually created.

Build succeeds, but no .lib file generated

When googling for a way to fix this, I found this question and this answer, which refers to the same problem of not being able to generate a .lib file because the project contains no .cpp file.

The answer says that they solved it by adding a .cpp file that just includes all the header files:

And this does indeed solve the issue:

But surely there's a more elegant way of solving this? Why doesn't VS want to generate a .lib file if you don't do this?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
JensB
  • 839
  • 4
  • 19
  • 2
    What's the purpose of building a lib file if your library is only made out of templates? The user only needs to include your header files anyway, what are you trying to do? – UnholySheep Jul 29 '21 at 18:28
  • @UnholySheep Not sure I understand what you mean. Do libraries never consist of just class templates or just classes? – JensB Jul 29 '21 at 18:31
  • 2
    [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) - TL;DR; Your `lib` file would be empty if you don't put explicit instantiations of your class templates and function templates in it. That's usually _not_ what you want. "_Do libraries never consist of just class templates_" - Yes, and they are then usually called "header only" libraries. – Ted Lyngmo Jul 29 '21 at 18:31
  • 1
    Libraries consist of _compiled code_. Templates are instructions for _writing code_. The common redistributable for templates is "a header file" – Drew Dormann Jul 29 '21 at 18:32
  • 1
    Wikipedia page on [Header-Only libraries](https://en.wikipedia.org/wiki/Header-only) – user4581301 Jul 29 '21 at 18:39

0 Answers0