How to eliminate Warning LNK4221?

后端 未结 5 2163
伪装坚强ぢ
伪装坚强ぢ 2021-02-18 23:46

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
相关标签:
5条回答
  • 2021-02-19 00:18

    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.

    0 讨论(0)
  • 2021-02-19 00:23

    This was kind of just my particular problem but if your .cpp doesn't define any new functions or has empty bodies for constructor functions, you will get this warning. I used an initialization list for my constructor so the body was empty.

    0 讨论(0)
  • 2021-02-19 00:31

    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.

    0 讨论(0)
  • 2021-02-19 00:32

    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!

    0 讨论(0)
  • 2021-02-19 00:34

    See this: https://docs.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

    0 讨论(0)
提交回复
热议问题