VS2008 到 VS 2010 的迁移 - 重大变化?

发布于 2024-10-13 03:27:45 字数 869 浏览 7 评论 0原文

我在将 C++ 代码从 VS2008 迁移到 VS2010 时遇到了问题。到目前为止我找不到原因的解释,非常感谢您的帮助。

我们有一个自定义内存分配器,它驻留在 dll 中。在代码的其余部分中,我们使用预处理器将分配重定向到我们的函数。下面的简单代码在 VS2008 中可以正确编译,但在 VS2010 中却不能。

在 stdafh.h 中:

#define free my_free
#include <string>

在 VS2010 中我得到:

1>d:\program files\microsoft visual studio 10.0\vc\include\xdebug(62): error C3861: 'free': identifier not found

来自线路:

template<class _Ty>
    void __CLRCALL_OR_CDECL _DebugHeapDelete(_Ty *_Ptr)
    {   // delete from the debug CRT heap even if operator delete exists
    if (_Ptr != 0)
        {   // worth deleting
        _Ptr->~_Ty();
        // delete as _NORMAL_BLOCK, not _CRT_BLOCK, since we might have
        // facets allocated by normal new.
        free(_Ptr);

任何帮助或想法将受到高度赞赏!

摩西

I encountered a problem when migrating our C++ code from VS2008 to VS2010. I can't find an explanation for the reason of it so far and will appreciate your help.

We have a custom memory allocator and it resides in a dll. In the rest of the code we use the preprocessor to redirect the allocations to our functions. Thie following simple code compiles correctly in VS2008, but does not in VS2010.

in stdafh.h:

#define free my_free
#include <string>

In VS2010 I get:

1>d:\program files\microsoft visual studio 10.0\vc\include\xdebug(62): error C3861: 'free': identifier not found

Coming from the line:

template<class _Ty>
    void __CLRCALL_OR_CDECL _DebugHeapDelete(_Ty *_Ptr)
    {   // delete from the debug CRT heap even if operator delete exists
    if (_Ptr != 0)
        {   // worth deleting
        _Ptr->~_Ty();
        // delete as _NORMAL_BLOCK, not _CRT_BLOCK, since we might have
        // facets allocated by normal new.
        free(_Ptr);

Any help or ideas will be highly appreciated!

Moshe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2024-10-20 03:27:45

根据 C++ ISO 标准,第 17.4.3.1.1.2 节:

包含标头的翻译单元不应包含任何定义在该标头中声明或定义的名称的宏。这样的翻译单元也不能为与关键字词法相同的名称定义宏。

这意味着 #define 库函数名称具有其他含义是不合法的。我想这恰好可以在 VS2008 中工作,但是当迁移到 VS2010 时,编译器作者提出了一个不能正常工作的实现。

如果您想重新定义 free 的功能,我建议您通过更传统的渠道来完成此操作,方法是将代码链接到您自己的 C 库实现,从而更改默认行为。

According to the C++ ISO standard, section 17.4.3.1.1.2:

A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.

This means that it's not legal to #define a library function name to mean something else. I guess this just happened to work in VS2008, but when migrating to VS2010 the compiler authors came up with an implementation in which this does not work properly.

If you want to redefine what free does, I suggest doing it through a more conventional channel by linking the code against your own implementation of the C library that changes the default behavior.

甜嗑 2024-10-20 03:27:45

经过使用 /P 选项创建预处理器文件并研究它们的一些调查后,我找到了问题的根本原因。

xdebug 标头将自己描述为“Microsoft 的调试堆支持标头”,包含以下几行:

#pragma push_macro("free")
#undef free

这显然挫败了我们重新定义它的尝试。所以这对编译器来说并不是什么新鲜事,只是简单的 #undef 恰好发生在我们尝试重新定义的函数中

After some investigation using the /P option to create the preprocessor files and studying them I've found the root cause of the issue.

The xdebug header, which describes itself as "debug heap support header for Microsoft", contains the following lines:

#pragma push_macro("free")
#undef free

which obviously defeat our attempts to redefine it. So this is not something new to the compiler, just plain #undef that happens to occur with the functions we try to redefine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文