Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Specify .LIB in Header for Visual Studio 2008 C++

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?

like image 403
Steve Avatar asked May 26 '11 19:05

Steve


People also ask

How do I link a header file in Visual Studio?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.

How do I add an external library to Visual Studio?

How to Add an External C++ Library to Your Project Using the Visual Studio IDE. Step 1: Go to the website of the library. Step 2: Download the zip file that contains all the code. Step 3: Unzip the zip file to your computer.

What is #pragma comment lib?

#pragma comment( lib, "emapi" ) The following pragma causes the compiler to place the name and version number of the compiler in the object file: C Copy.


1 Answers

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 */
like image 126
Alexey Kukanov Avatar answered Sep 28 '22 03:09

Alexey Kukanov