问题
Edit: 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.
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;
}
I.e., these 10 lines alone builds fine in VS2010 and in VS2013 give:
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
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
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?
I realize it is not fully supported, but one can hope: is there a more robust way of using _CrtMemBlockHeader?
回答1:
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.
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>
Perhaps this helps?
来源:https://stackoverflow.com/questions/26233236/vc-code-works-in-vs2010-and-breaks-in-vs2013