Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: What does #pragma comment(lib, "XXX") actually do with "XXX"?

Tags:

c++

pragma

My background is C# but I have to maintain some legacy (MS) C++. In that codebase I stumpled across:

#pragma comment(lib, "OtherLib700.lib") 

where 700 is some versioning. Besides the lib is a DLL with the same name.

I first thought that the program would be dependant upon the DLL but after removing it from the system the program still works. There exists a newer version of the DLL, though, which is named OtherLib900...

It seems as though the program 'included' the code of the lib so that it's no longer dependant upon the external DLL. (Or that the program 'automatically' uses the newer DLL...)

Which one is correct? Is there are way to further confirm that 'assumption'?

like image 634
steglig Avatar asked Aug 30 '12 14:08

steglig


People also ask

What does /= in coding C mean?

/= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. C /= A is equivalent to C = C / A.

What does the operator do in C?

C language supports a rich set of built-in operators. An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations.

What does &= mean in C?

It means to perform a bitwise operation with the values on the left and right-hand side, and then assign the result to the variable on the left, so a bit of a short form.


1 Answers

That pragma is used to link against the specified .lib file. This is an alternative to specifying the library in the external dependencies field in project settings.

Mostly, it's used to support different versions:

#ifdef USE_FIRST_VERSION #pragma comment(lib, "vers1.lib") #else #pragma comment(lib, "vers2.lib") #endif 

When your application uses a dynamically-linked library, a lib file tells you information about what symbols are exported in the dll. So basically you only need the lib to compile & link, but you need the dll to run the program, as it contains all the binary code.

You say there's an associated dll, which usually indicates the lib file only contains linking info, and no code. You should get a run-time error if the associated dll was not found. You can check with MSVS if a different version of the dll was loaded or if it was loaded from a different place.

like image 176
Luchian Grigore Avatar answered Oct 13 '22 21:10

Luchian Grigore