Anyone know of a generic guide for using third party C++ libraries, specifically with Visual Studio 2010? I'm thinking "winging it" isn't cutting it, I want a resource to learn more about how these things are usually prepared, and used.

I swear I saw a mention of a library system for C++ on a TR somewhere..

Generally speaking, when you download and use 3rd party libraries you just need to set the compiler up to look for additional headers in the library's 'include' directory, and you point the linker to it's 'lib' directory to allow it to link your program using the provided .lib's/.dll's or .so's.

Some libraries contain pre-compiled binaries in their 'lib' directory. Others may require you to build them from source before you can use them. Typically the owners/creators of any given library will provide instructions on this.

In VS, you can set up the search-paths for directories containing additional header and library files on a per-project basis in the project settings.
e.g.
Select 'Project->Properties' in the main menu to bring up the project properties page.
Under 'configuration properties' select the 'C/C++->General' tab and select the 'Additional include directories' field. Then click on the '...' icon to open another window which will allow you to add the paths to the 'include' directory of your 3rd party library to a list. You can also alter the include order by moving entries up or down the list.

Then under the 'Linker->General' tab you can do a similar thing with the 'Additional Library Directories' item and add paths to the 'lib' directories of any 3rd party libraries you want to use.

Alternatively, you can set up the search paths as global paths in 'Tools->Options' under the 'Projects and solutions->VC++ Directories' item:
Select the 'show directories for' drop down and select the 'include files' option to allow you to add the paths to any folders containing additional library headers. Again, you can also move entries up and down the list to alter the include order if necessary.

Once that's done select 'library files' in the drop-down to allow you to add the paths to any lib directories.

Setting libraries up in the global settings will allow you to use those libraries in any of your projects without having to specify them in the project properties. Personally I prefer to use the per-project settings to include additional libraries rather than the global settings, but whatever floats your boat!

The instructions above should work for VS2003, VS2005 and VS2008. And as long as VS2010 isn't too different, it should probably work there too! (I'm on 2008 at work ATM)

The only place where you may come unstuck from time to time is with the include order. If you are using several external libraries, you might have to resolve conflicts by moving one library's include folders above anothers in the list of additional headers. You can do this in the properties pages as mentioned above.

I can't think of anything else offhand, but I'm sure somebody else will chip in if there's anything I've forgotten to mention!

Comments
Good post.
..

I have not heard or seen any book or resources online which are a "generic guide to using external libraries in C++". I think the problem with writing such a guide is the shear amount of possibilities. There are APIs, header-only libraries, source-file-only libraries, different programming paradigms, some have special build tools (like qmake for Qt), etc.

Generally, you need to follow the instructions on the website of the library as best as you can. And remember that some libraries are not extremely well-written, and the author(s) might be overstating the portability of the code (if you hear things like "my lib should compile with any standard C++ compiler", don't trust that because "should compile" means it wasn't really tested, and a "standard C++ compiler" doesn't really exist in practice).

To add to JasonHippy's post, if using binaries, make sure that the binary was not only compiled for the correct platform, but also that it was compiled with the same compiler that you are using. Be aware that there are special steps to take if you need to use a binary that was compiled with another compiler, and it may not be successful at all, in which case, you need to recompile from source code.

A few more pieces of advice:
1) Don't introduce external dependencies which only solve trivial problems, prefer to write your own code from scratch if it's simple enough. I'm not saying you should reinvent the wheel all the time, I'm simply saying that if you have a problem that you could solve by coding it with not so much effort, don't use an external library for it.
2) Use trusted external libraries. There are external libraries like Boost, OpenCV, many GUI tools, SDL, OpenGL, GSL, etc. which have a long history of serving many people for many projects, and these tend to be more portable, easier to use, and more bug-free. So, for a given problem, if you have a trustworthy library that solves half of the problem (leaving the other half for you to solve) and an untrustworthy library that solves the entire problem, prefer the trustworthy one and code the rest yourself.
3) If you have to use an external library which seems to be somewhat untrustworthy, then you should isolate it from the rest of your code. This means that either you write a thin-wrapper API for the external library and/or you PImpl it. The point of this is that you want to avoid polluting all your code with usage of an external library. It is better to have all your code using a thin-wrapper that you have programmed which, in turn, uses the external library. If later you find another library that would be better, or if you realize that this external library is too buggy to be useful, it is easy to reimplement the thin-wrapper such that it uses another library or uses code that you wrote. And also, many poorly written libraries tend to create problems when mixed with other code, so you want to seal the code that uses it (PImpl is good for that, it creates a fire-wall between the external library and your code).

Comments
Good post.

Not usually, most libraries do not provide you with the source code so recompiling it will be impossible. In other cases when you get an open source library you will also get the source code. Check the download file to see if it already contains the binary DLL, if not then you will probably have to compile it.

Edited 5 Years Ago by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.