c++cvisual-c++pragmalinker-warning

How do we disable a linker warning with a #pragma warning?


We can disable a compiler warning with

#pragma warning(disable:4966)

How can we disable a linker warning with a #pragma directive? Something like:

// Disable linker warning LNK4221
#pragma warning(disable:4221)

Solution

  • Linker warning cannot be put in a source file, as source files (.CPP/.C/.H etc.) are for the consumption of the compiler and not for the linker. The compilation would produce object files out of one or more files (via one or more "translation units"). If you put #prama disable_linker_warning in some source file - to which .OBJ file (part of linking) the linker setting should go?

    Linker settings are global to the project - since .OBJ files are linked to produce final PE image, and linker warnings would be applied then. You can choose the linker warnings in the project settings page.

    The preprocessor directives won't be saved and later propagated to the linker. It may lead to multiple path ambiguities (due to multiple translation units) and other complexities. Such linker feature may not be reliable.

    Note that in VC++ when you select property of a single source file, you don't see any "Linker" setting.

    enter image description here