user16038533
user16038533

Reputation:

Is it possible to make a static library from a header only file through visual studio?

By following some tutorials, I was successfully able to make a static library through visual studio , but it only works if there is a corresponding .cpp file for the .h file. Take these two files for example

.h

void print();

.cpp

void print()
{
    std::cout << "printing from lib";
}

I went I to

project->properties->configuration properties->configuration type

and set the configuration type to static library. When I build this, it makes the required .lib files and stores it in the debug folder. It also outputs this to the output console.

1>engine.vcxproj -> C:\Users\me\source\repos\game\Debug\engine.lib

But now if I delete the .cpp file and change the .h file to

void print()
{
     std::cout << "printing from lib";
}

and then try to build, it says build successful, but It does not create any .lib files. It also does not output

 `1>engine.vcxproj -> C:\Users\me\source\repos\game\Debug\engine.lib`

Upvotes: 4

Views: 1392

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283733

You can force the compiler to run on a header file, but for your own sanity and those of future maintenance developers, just put a .cpp file with the single line

#include "library.h"

Note that libraries distributed in header files usually do so because of templates and will not make a useful static library.

Upvotes: 3

Related Questions