关于VS2010编译警告LNK4221

最近研究duilib,准备把里面自定义的一些工具类如CDuiString什么的用ATL的替换掉,于是遇到久仰大名的 
warning C4251: xxx  needs to have dll-interface to be used by clients of class xxx
这个其实很久前就遇到过,当时做法是改用该类型的指针,现在不想这么做了,既然dll中导出类成员变量不合适,那就将duilib作为静态库使用吧。进行如下设置
434089-20160909100107613-1141611128.png
  434089-20160909100108816-2075243154.png
 Debug下编译木有问题,当换成Release后
434089-20160909100109504-271751450.png
出现如下错误
  434089-20160909100110019-651157206.png
 文件目录中确实这两个文件不在同一级目录,好吧,改成 #include "../stdafx.h" 试试,然而这次主角登场了 434089-20160909100110488-646825333.png
 从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 的预编译头文件选项
434089-20160909100111738-222749966.png
 发现是空白的,那么把它设为 /Yu,然后改回 #include "stdafx.h".
OK ALL DONE!




转载于:https://www.cnblogs.com/mforestlaw/p/5855476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值