简体   繁体   中英

Static & Dynamic Library Creation: How to?

So I made a few C++ header files for binary trees-based data structures, such as AVLs, Red-blacks, Tries etc. and I would like to create a .lib file that I can use on bigger projects whenever I want. The code I wrote is all object-oriented and templated. The thing is, all I have is a lot of header files(I included the functions inside the header files because I used templates, and I wanted maximum modularity) and no source files. I tried to create a new .lib project inside Visual C++, but it does not produce any .lib file without source files, which I cannot create without a compromise I am not willing to make. I don't see any solution to this problem, so can I please get some input on how I could proceed? Thanks!

If you have created a library whose source code consists entirely of C++ header files, that is what we call a header-only library . There is nothing more you need to do.

Header-only C++ libraries are normal. Many of the Boost libraries are header-only. The subset of the Standard C++ Library traditionally known as the Standard Template Library was traditionally header-only. When the library interface consists entirely of templates, the implementation is very probably done when you have written the header files, and in that case the header files are the library. 1

You want your template library to be reusable in other projects? It already is. To reuse it:-

  • You tell your compiler where to find the header files using its include-directories option or the equivalent project setting in your IDE

  • You #include the header file(s) as required.

That's all there is to it.

Cutting your teeth on MS Visual Studio may have given you an early impression that a library must have a .lib file without a clear idea why that should be so. In the MS Windows world, that is true when the library is a conventional library - not a C++ header-only library. (And the corresponding thing is true in Linux and other OSes, except that they have something else instead of .lib files). A conventional C or C++ library is built from both header files, that define its interface, and separate source files, that define its implementation. Building the library requires compiling each source file into an object file that contains part of the library implementation, and then packaging all those object files together in some form - a dynamic library or static library - that contains the whole implementation.

So to reuse a conventional library I need its header files, so that I can correctly invoke its interfaces in my source code; and I need the object-file library package that contains its binary implementation, so that I can tell the linker to link my object files together with that package and generate a complete working program. The library's .lib file represents the package of object files to the linker (though represents means one thing for a static library and something quite different for a DLL).

For a header-only library, by definition there are no separate source files that define parts of the implementation. Hence no separate object files. Hence no separate package of object files. No .lib . No linkage. There is nothing to to be linked.


[1] A library that provides exclusively template interfaces may well be implemented exclusively with templates, consisting of nothing but the template implementations of its interfaces, in its header files. But nothing stops the implementer from writing internal parts of the implementation with non-template code, if they see some decisive advantage in doing so - size optimization, perhaps? In that case, they don't get a header-only library, even though the interfaces are all templates. In the absence of countervailing considerations, the natural implementation of template interfaces is a template implementation and a header-only library is the natural product.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM