5

Is it possible to automatically link a c++ static library in Visual Studio 2008? Or, is there an improvement to use from the standard approach?

I am developing a set of c++ libraries and linking/testing them is quite a pain. The usual approach is to specify the .lib files in the test clients, but now the list has grown quite large (my own libs, opencv, boost, and others) and I'm always missing something as I switch between debug and release modes, gpu and non-gpu, etc. When I open the linker in project properties the list scrolls on for some time.

I was hoping that I could automatically specify that if a client #includes something that the project should also link to a specified .lib (debug/release). Is this possible or is there an alternate approach that will help manage linkage with minimal user interaction?

Steve
  • 3,957
  • 2
  • 26
  • 50

1 Answers1

5

Use #pragma comment(lib, "name_of_the_library.lib"). Other useful options for #pragma comment can be found at the MSDN page.

With regard to Debug vs. Release configuration: usually a _DEBUG preprocessor macro is used to distinguish. Visual C++ header certainly use it for the same purpose as you want; e.g. this is from VC++ 2010 use_ansi.h file:

#ifdef _DEBUG
#pragma comment(lib,"msvcprtd")
#else   /* _DEBUG */
#pragma comment(lib,"msvcprt")
#endif  /* _DEBUG */
Alexey Kukanov
  • 12,479
  • 2
  • 36
  • 55
  • 1
    While it might not look like it at first glance, this is exactly what you're looking for, @Steve. – John Dibling May 26 '11 at 19:28
  • 1
    @Steve, that KB article relates to Visual C++ 4.0 - not something that you would want to use nowadays :) – Alexey Kukanov May 26 '11 at 19:31
  • This looks good. I'm digging through trying to find what the best preprocessor branch to detect for debug and release mode will be since I have more configuration names than "Debug" and "Release". OpenCV, for instance, links to opencv_core220d.lib in debug and opencv_core220.lib in release. This is relevant: http://stackoverflow.com/questions/2290509/debug-vs-ndebug – Steve May 26 '11 at 19:31