What is the best way to eliminate MS Visual C++ Linker warning : "warning LNK4221"?

Adisak picture Adisak · Nov 30, 2009 · Viewed 7.3k times · Source

I have a CPP source file that uses #if / #endif to compile out completely in certain builds. However, this generates the following warning.

warning LNK4221: no public symbols found; archive member will be inaccessible

I was thinking about creating a macro to generate a dummy variable or function that wouldn't actually be used so this error would go away but I want to make sure that it doesn't cause problems such as using the macro in multiple files causing the linker to bomb on multiply defined symbols.

What is the best way to get rid of this warning (without simply suppressing the warning on the linker command line) ?

FWIW, I would be interested in knowing how to do it by suppressing the warning on the linker command line as well but all my attempts there appear to be simply ignored by the linker and still generate the error.

One other requirement: The fix must be able to stand up to individual file builds or unity build (combine CPP file builds) since one of our build configurations is a bulk build (like a unity build but groups of bulk files rather than a single master unity file).

Answer

Pavel Minaev picture Pavel Minaev · Nov 30, 2009

Use an anonymous namespace:

namespace { char dummy; };

Symbols within such namespace have external linkage, so there will be something in the export table. On the other hand, the namespace name itself will be distinct (you can think of it as "randomly generated") for every translation unit, so no clashes.