You can use #pragma comment(lib...) to specify linker options in Microsoft C++

Article translations Article translations
Article ID: 153901 - View products that this article applies to.
This article was previously published under Q153901
Expand all | Collapse all

On This Page

SYMPTOMS

The comment pragma allows the user to insert comments into an object file or executable file. The lib specifier allows the user to pass this comment to the linker to specify additional libraries to link when using the object module. Some users utilize the lib comment pragma to add linker options as well as library names, such as:
   #pragma comment(lib, "MSVCRT -VERBOSE")
				
This line, under Visual C++ 2.x, would pass the string to the linker as is, resulting in the addition of the /VERBOSE linker option as well as the MSVCRT.LIB library. This behavior was not specified to work in this manner. The compiler wouldn't put quotes around comment string, so spaces would delimit the text.

In Visual C++ 4.x and later, the compiler correctly puts quotes around the comment string thereby causing the above code to generate the following error:
LINK : fatal error LNK1104: cannot open file "MSVCRT -VERBOSE.lib"

RESOLUTION

Use the following new pragma to specify linker options:
#pragma comment(linker, "<linker options>")
				

STATUS

This behavior is by design.

MORE INFORMATION

Sample code to reproduce the behavior:

// Compile options needed: none
// Compile the code in Visual C++ 4.x or later
// test.c

#pragma comment(lib,"MYLIBRARY -VERBOSE")

void main(void)
{
}
				
You will receive the following error message:
LINK : fatal error LNK1104: cannot open file "MYLIBRARY -VERBOSE.lib"

REFERENCES

For more information about#pragma directives , see the following MSDN Web site:
Pragma Directives

Properties

Article ID: 153901 - Last Review: June 18, 2012 - Revision: 5.0
APPLIES TO
  • Microsoft Visual C++ 4.0 Standard Edition
Keywords: 
kbcompiler kbprb kbusage KB153901
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com