VC++:代码在 VS2010 中有效,在 VS2013 中中断 [英] VC++: Code works in VS2010 and breaks in VS2013

查看:43
本文介绍了VC++:代码在 VS2010 中有效,在 VS2013 中中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与投票结束时提到的问题不同,这里的违规代码是 CRT 代码,不是我的.即使它有问题(我很确定它没有),我也无法修复它的来源.

unlike the case in the question noted in the vote-to-close, the offending code here is CRT code, not mine. Even if it has a problem (which I'm pretty sure it doesn't), I have no way of fixing its source.

我们有一些使用 CRT 内部结构的遗留内存泄漏跟踪代码(没什么特别的,本质上是 _CrtMemBlockHeader,它是 排序记录).在尝试从 VS2010 迁移到 VS2013 时,代码似乎会导致偶发的构建失败,并且可以将违规部分简化为:

We have some legacy memory-leak tracing code that uses some CRT internals (nothing too exotic, essentially _CrtMemBlockHeader which is sort-of documented). While trying to migrate from VS2010 to VS2013 the code seems to cause sporadic build failures, and the offending part can be reduced to this:

#include <windows.h>

#define _CRTBLD
#include <..\crt\src\dbgint.h>

#include <fstream>
void Func()
{
    std::ofstream myfile;
    myfile << 8;
}

即,仅这 10 行就可以在 VS2010 和 VS2013 中正常构建:

I.e., these 10 lines alone builds fine in VS2010 and in VS2013 give:

c:\程序文件 (x86)\microsoft Visual Studio12.0\vc\include\xlocnum(1105): 错误 C2491: 'std::numpunct<_Elem>::id' : 不允许定义 dllimport 静态数据成员

c:\program files (x86)\microsoft visual studio 12.0\vc\include\xlocnum(1105): error C2491: 'std::numpunct<_Elem>::id' : definition of dllimport static data member not allowed

我怀疑错误消息不准确 - id 确实有几个潜在的定义,第 1105 行没有一个定义.还有大量警告:

I suspect the error message is not accurate - there are indeed several potential definitions of id, none of them at line 1105. There is also a considerable spew of warnings:

1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xlocnum(1105): warning C4273: 'id' : inconsistent dll linkage
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xlocnum(80) : see previous definition of 'public: static std::locale::id std::numpunct<char>::id'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xlocnum(80) : while compiling class template static data member 'std::locale::id std::numpunct<_Elem>::id'
1>          with
1>          [
1>              _Elem=char
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xlocnum(1185) : see reference to function template instantiation 'const _Facet &std::use_facet<std::numpunct<_Elem>>(const std::locale &)' being compiled
1>          with
1>          [
1>              _Facet=std::numpunct<char>
1>  ,            _Elem=char
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xlocnum(1179) : while compiling class template member function 'std::ostreambuf_iterator<char,std::char_traits<char>> std::num_put<char,std::ostreambuf_iterator<char,std::char_traits<char>>>::do_put(_OutIt,std::ios_base &,_Elem,std::_Bool) const'
1>          with
1>          [
1>              _OutIt=std::ostreambuf_iterator<char,std::char_traits<char>>
1>  ,            _Elem=char
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(306) : see reference to class template instantiation 'std::num_put<char,std::ostreambuf_iterator<char,std::char_traits<char>>>' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(292) : while compiling class template member function 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(int)'
1>          c:\users\ofek\documents\visual studio 2013\projects\testcamsys2013\testcamsys2013\source.cpp(10) : see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(int)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\fstream(921) : see reference to class template instantiation 'std::basic_ostream<char,std::char_traits<char>>' being compiled
1>          c:\users\ofek\documents\visual studio 2013\projects\testcamsys2013\testcamsys2013\source.cpp(9) : see reference to class template instantiation 'std::basic_ofstream<char,std::char_traits<char>>' being compiled

现在我将 _CrtMemBlockHeader 的定义和它周围的一些宏直接粘贴到我的代码中.但仍然 - 谁能看到什么坏了?

For now I'm pasting the definitions of _CrtMemBlockHeader and some macros around it directly to my code. But still - can anyone see what broke?

我意识到它没有得到完全支持,但可以希望:有没有更强大的使用 _CrtMemBlockHeader 的方法?

I realize it is not fully supported, but one can hope: is there a more robust way of using _CrtMemBlockHeader?

推荐答案

在我的系统上看这个错误,它似乎只与 #define _CRTBLDfstream<有关/code> 标题.包含的 dbgint.h 无关紧要(您可以注释掉 #include 并仍然得到相同的错误.

Looking at this error on my system, it seems to be only related to the #define _CRTBLD and the fstream header. The included dbgint.h is irrelevant (you can comment out that #include and still get the same error.

所以,这似乎是 fstream 标头中的一个问题.更改包含顺序会消除编译错误:

So, this seems to be a problem in the fstream header. Changing the order of inclusion removes the compilation errors:

#include <windows.h>
#include <fstream>

#define _CRTBLD
#include <..\crt\src\dbgint.h>

也许这有帮助?

这篇关于VC++:代码在 VS2010 中有效,在 VS2013 中中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆