问题
I'm attempting to perform a link of previously generated .obj files (using the latest version of MSVC).
When those .obj's were created, the source code specified:
#pragma comment(lib, ...)
As such, the linker is attempting to link against static libraries specified in the source. Is there a way to instruct the linker to ignore these libraries, so I can specify my own?
e.g., if a piece of source did the following:
#pragma comment(lib, foo.lib)
At link time, I'd like the linker to ignore 'foo.lib', and instead link against 'bar.lib'.
回答1:
Why not change the source (the #pragma
directives)?
From comment MSDN page:
lib
Places a library-search record in the object file. This comment type must be accompanied by a commentstring parameter containing the name (and possibly the path) of the library that you want the linker to search. The library name follows the default library-search records in the object file; the linker searches for this library just as if you had named it on the command line provided that the library is not specified with /nodefaultlib. You can place multiple library-search records in the same source file; each record appears in the object file in the same order in which it is encountered in the source file.
If the order of the default library and an added library is important, compiling with the /Zl switch will prevent the default library name from being placed in the object module. A second comment pragma then can be used to insert the name of the default library after the added library. The libraries listed with these pragmas will appear in the object module in the same order they are found in the source code.
You can also use the NODEFALTLIB linker option to stop foo.lib
from being linked, and specify bar.lib
as you would other lib files (i.e. via the Linker Properties pane).
Otherwise, rename bar.lib
to foo.lib
(a cheeky solution).
来源:https://stackoverflow.com/questions/860109/ignore-pragma-commentlib