Question:
If I declare a class member as an fstream and initialise it within a public method,
Compiling and running in DEBUG is OK but not in an OPT build. In an OPT build I receive a linker error to the tune of un-initialised data, basically the fstream m_file, see below. However, If I initialise the fstream in a constructor list (ouch!) and assign it to (0) then OPT will build, link and run fine, DEBUG on the other hand is (rightly so) busted. Although DEBUG will compile, when you run the app the fstream will throw an assert failure.
What to do, is this a compiler/linker bug or am I missing the obvious?
I?ve pulled my code out into the simple example below. I'm running on WIN32 using intel7 and .NET
The compiler options for DEBUG and OPT are as follows
DEBUG
C++OPTS += -Zi -Od -GX -MDd -Qinline_debug_info
OPT
C++OPTS += -G6 -GX -Zi -O3 -Ob2 -MD
Sample code
-----------------------
#include
#include
#include
#include
class import
{
public:
import();
~import();
void open(std::string & filename);
private:
std::fstream m_file;
};
import::import()
//: m_file(0) // unhash this line to get things working under opt
{
}
import::~import()
{
}
void import::open(std::string & filename)
{
m_file.clear(); // only realy needed if using the initialised fstream hack! (OPT)
m_file.open( filename.c_str(), std::ios::in );
if ( m_file.fail() )
{
std::cerr<<"failed!
";
}
else
{
std::cerr<<"success!
";
m_file.close();
}
}
int main (int argc, char *argv[] )
{
if ( argc == 2 )
{
import my_import;
my_import.open( std::string(argv[1]) );
}
else
{
std::cerr<< "Error: No file argument!
";
}
return 1;
}
----------------
The linker error I get in OPT without initialising the fstream is as follows
----------------------------------------------------------Creating library C:/users/bob/dev/bin/allmain/WIN32.OPT.INTEL7/import_stream.lib and object C:/users/bob/dev/bin/allmain/WIN32.
OPT.INTEL7/import_stream.exp
stream_eg.obj : error LNK2019: unresolved external symbol "?use_facet@?$@V?$codecvt@DDH@std@@@std@@YAABV?$codecvt@DDH@1@ABVlocale@
1@@Z" (?use_facet@?$@V?$codecvt@DDH@std@@@std@@YAABV?$codecvt@DDH@1@ABVlocale@1@@Z) referenced in function "public: void __thiscal
l import::open(class std::basic_string,class std::allocator > &)" (?open@import@@QAEXAAV?
$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
C:/users/bob/dev/bin/allmain/WIN32.OPT.INTEL7/import_stream.exe : fatal error LNK1120: 1 unresolved externals
make: *** [import_stream.exe] Error 96
----------------------------------------------------------
Possible Compiler Bug regarding I/O streams
For more complete information about compiler optimizations, see our Optimization Notice.


