解决Visual Studio 2010/2012的RC4011 warnings

如果在vc10/11工程的rc文件中有以下任意一行代码:

#include <winuser.h>
#include <richedit.h>

那么vc将会给出一对警告:

C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(54): warning RC4011: identifier truncated to '_CRT_SECURE_CPP_OVERLOAD_STANDA'
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(76): warning RC4011: identifier truncated to '_CRT_SECURE_CPP_OVERLOAD_SECURE'

其原因是vc提供了一个宏_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_MEMORY,用来自动将代码中的memcpy替换成memcpy_s。
memcpy_s在被提出时,确实感觉这样子代码是安全了,但是两个函数的参数不一致,导致修改量上去了,而且移植性也没有了,于是vc好心地提供这个宏用来自动替换。
那么为什么会有上面的警告呢?

问题出在RC,也就是resource compiler,基于历史原因,它会自动将长度超过31的宏截断,应该是RC只用了char[32]来存诸的缘故,于是我们就看到让人不舒服的警告了。

解决办法很简单:

针对#include <winuser.h>,将其替换为<windows.h>。

针对#include <richedit.h>,稍微麻烦一点。
据我们所知,rc包含.h的目的通常只是为了一些#define,对其中的函数神马的并不在意。
那么如警告中描述所示,问题是出在string.h,同时我们包含richedit其实也只是为了得到RICHEDIT_CLASS,那么事情好办了,修改如下:

#ifdef RC_INVOKED
#define _INC_STRING
#endif

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"

如MSDN中所描述:

To conditionally compile your code with the RC compiler, surround code that RC cannot compile with #ifndef RC_INVOKED and #endif.

而string.h中是这样定义的:

#pragma once

#ifndef _INC_STRING
#define _INC_STRING

...

#endif

好的,你懂的,我就不多说什么了。

:D

 

posted @ 2013-09-12 14:32  千里马肝  阅读(1776)  评论(0编辑  收藏  举报