关于VS2010编译警告LNK4221

最近研究duilib,准备把里面自定义的一些工具类如CDuiString什么的用ATL的替换掉,于是遇到久仰大名的 
warning C4251: xxx  needs to have dll-interface to be used by clients of class xxx
这个其实很久前就遇到过,当时做法是改用该类型的指针,现在不想这么做了,既然dll中导出类成员变量不合适,那就将duilib作为静态库使用吧。进行如下设置
 
 Debug下编译木有问题,当换成Release后
出现如下错误
 
 文件目录中确实这两个文件不在同一级目录,好吧,改成 #include "../stdafx.h" 试试,然而这次主角登场了
 从msdn搜索到是这么说的

Linker Tools Warning LNK4221

Visual Studio 2015
 

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

Consider the following two code snippets.

// a.cpp
#include <atlbase.h>
// b.cpp
#include <atlbase.h>
int function()
{
   return 0;
}

To compile the files and create two object files, run cl /c a.cpp b.cpp at a command prompt. If you link the object files by running link /lib /out:test.lib a.obj b.obj, you will receive the LNK4221 warning. If you link the objects by running link /lib /out:test.lib b.obj a.obj, you will not receive a warning.

No warning is issued in the second scenario because the linker operates in a last-in first-out (LIFO) manner. In the first scenario, b.obj is processed before a.obj, and a.obj has no new symbols to add. By instructing the linker to process a.obj first, you can avoid the warning.

A common cause of this error is when two source files specify the option /Yc (Create Precompiled Header File) with the same header file name specified in the Precompiled Header field. A common cause of this problem deals with stdafx.h since, by default, stdafx.cpp includes stdafx.h and does not add any new symbols. If another source file includes stdafx.h with /Yc and the associated .obj file is processed before stdafx.obj, the linker will throw LNK4221.

One way to resolve this problem is to make sure that for each precompiled header, there is only one source file that includes it with/Yc. All other source files must use precompiled headers. For more information about how to change this setting, see /Yu (Use Precompiled Header File).



意思是除了 stdafx.cpp 外,其它包含了 stdafx.h 的源文件如果预编译项选择了 /Yc 就会出现这个警告。
我们知道一个项目中一般只将 stdafx.cpp 的预编译头文件选项设置为 /Yc 其他源文件如果要使用预编译头文件则设为 /Yu 。
于是查看  XUnzip.cpp 的预编译头文件选项
 发现是空白的,那么把它设为 /Yu,然后改回 #include "stdafx.h".
OK ALL DONE!




原文地址:https://www.cnblogs.com/mforestlaw/p/5855476.html