In general, that is normal. In C++ a "library" consists of a .lib/.dll and a .h./.hpp pair, and you need them both. Due to the fact that compiler and linker are in fact a different tools, you have different configuration settings for them.
Actually, to be able to use an external library, you need to configure not 2, but 4 things:
- add an entry to include-search-paths and/or make sure that .h file is there (in project properties)
- add an entry to library-search-paths and/or make sure that .lib file is there (in project properties)
- include .lib file in linking by adding it to the linker options (in project properties)
- include that .h with an #include with proper relative path (in code)
However, it is somewhat true that there is a way to provide something like one point of 'inclusion', so at least the last two can be merged.
There is a
#pragma comment(lib, "foobar.lib")
(What does "#pragma comment" mean?)
instruction that may be a hint to the toolset saying which .lib file to include. If you put that into the header (in this case, probably foobar.h
), then just the fact that you #include that header, will make the build process pickup that lib and add it to linking phase..
..or so is the theory. As you may notice, that's a #pragma
, and also a comment
, so the process can simply ignore that or may simply not have that part implemented and that's 100% OK since it is not required by C++ standard. Fortunately, VisualStudio has support for this - actually iy understands that directive since a long time.
But! as I said, adding that directive is not enough. This directive only contains the filename, and it links the .h file with a .lib filename. You still need to ensure that the .lib is located somewhere in the library search paths, and that the .h file is somewhere in include search paths. If your linker settings point to some directories, you need to ensure that the lib file has been put in one of them, or else you will have to add the directory path to the settings, and similarly for .h/.hpp file.
That's relatively simple to overcome, just create an extra folder in your project, add that path to the search paths, and from now on, drop all additional .h/.lib files there.
If I may, I'd suggest something like ./3rdparty
and subfolders for specific libraries, like ./3rdpart/xml', './3rdparty/zlib
and so on. Add the `3rdparty' to both search paths and then, #include 'xml/tinyxml.h' etc. Otherwise you can get a filename clash. Of course, most of the libraries will actually not come with #pragma-comment-lib, so that part you will probably need to manually add.