Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to eliminate Warning LNK4221?

Tags:

c++

linker

I am working on a project using c++/windows forms (visual studio 2010), we have 4 projects:

  • 1 project containing GUI windows forms {managed code} and this is the exe project
  • other 3 projects {non-managed code} and all are static libraries.
  • in the 4 projects we don`t use precompilied headers stdafx.h , and common language runtime support is the Pure MSIL Common Language Runtime Support (/clr:pure).
  • every project include the other 3 projects as additional include directories , and link library dependencies set to yes.

We have:

Warning LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library

This warning appeared for the 3 static libraries projects in the same object files (.NETFramework,Version=v4.0.AssemblyAttributes.obj).

We want to eliminate it, but after some search, most topics speak about the precompiled headers to be a reason while we don not use it.

Any new ideas about why this warning exist and how to eliminate it?

like image 872
Mai Saad Avatar asked Dec 31 '13 08:12

Mai Saad


4 Answers

Disclaimer: This solution is indeed terrible, and this code should not be added to production build! It is only useful to "hide" the warning.

A better solution would be to remove the file in question from the compilation, as it is anyway useless.

Original post:

I had a the problem with this warning originating from several dependencies, and I found that it was caused by some translation unit being empty, i.e. empty source files.

These files actually had a content but it was deactivated for visual studio so I just added at the beginning:

__declspec( dllexport ) void getRidOfLNK4221(){}

And now my project compiles without any warning :)

Hope it helps, even if this is a late answer!

like image 57
Geoffroy Avatar answered Oct 20 '22 07:10

Geoffroy


I had a c++ static library with an empty module in it, linking which in non-optimized mode gave me that LNK4221 warning. (in Visual Studio) I went to Librarian->All Options, and added "/ignore:4221" to Additional Options. So, the warning has disappeared.

like image 44
Vladimir Shutow Avatar answered Oct 20 '22 09:10

Vladimir Shutow


You get this warning because you use only template classes in cpp file. To get rid of it, insert one simple function with no template.

like image 35
Myfero Avatar answered Oct 20 '22 07:10

Myfero


See this: https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-warning-lnk4221

In summary, the file exports nothing, so linker does not link, as it thinks it's a waste of effort

like image 1
Raghuram Sheshadri Avatar answered Oct 20 '22 08:10

Raghuram Sheshadri