我在 Windows 上的 tdm-mingw g++ 4.4.0 中使用 -std=gnu++0x 参数构建了一个应用程序。
它使用的是 ofstream 对象,当我构建时,它给出了以下链接错误:
c:\opt\Noddler/main_func.cpp:43: undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string const&, std::_Ios_Openmode)'
它在使用默认的旧标准时正确构建。
这是唯一的错误,尝试与 -lstdc++ 链接没有帮助。有人以前经历过吗?我可以得到任何建议吗?
编辑: 我正在创建这样一个 ofstream 对象:
std::string filename = string("noddler\\") + callobj.get_ucid() + "_" + callobj.gram_counter() + ".grxml";
ofstream grxml_file(string("C:\\CWorld\\Server\\Grammar\\") + filename);
...
grxml_file.close();
编译正常,但没有链接。
最佳答案
我猜你有这样的代码:
string fname = "foo.txt";
ifstream ifs( fname );
尝试将其更改为:
ifstream ifs( fname.c_str() );
如果您使用的头文件与您链接到的库有些不协调,则可能会发生这种情况。如果这不起作用,请发布导致问题的代码。
关于c++ - g++ 中的 fstream 链接错误与 -std=gnu++0x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1165071/