2011-04-11 214 views
13

您好我需要一个std::wstringQString转换和我试过的转换为std :: wstring的到QString的抛出链接错误

std::wstring wideString; 
QString qtString = QString::fromStdWString(wideString); 

最明显的方式,我得到了错误:

Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl QString::fromStdWString(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" ([email protected]@@[email protected][email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@@Z)

referenced in function "public: void __thiscall FileHandler::deleteDir(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >&,int,unsigned int,bool,bool)" ([email protected]@@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@[email protected]) filehandler.obj

Error 3 fatal error LNK1120: 1 unresolved externals C:\Users\Documents\Visual Studio 2008\Projects\product_dev\deletefiles\Debug\FileHandler.exe

我也尝试过使用该方法QString::fromWCharArray

qstring temp = QString::fromWCharArray(const_cast<wchar_t*>(wideString.c_str()),wideString.size()); 

呃我得到的是

Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl QString::fromWCharArray(wchar_t const*,int)" ([email protected]@@[email protected][email protected])

referenced in function "public: void __thiscall FileHandler::deleteDir(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >&,int,unsigned int,bool,bool)" ([email protected]@@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@[email protected]) filehandler.obj

Error 3 fatal error LNK1120: 1 unresolved externals C:\Users\Documents\Visual Studio 2008\Projects\product_dev\deletefiles\Debug\FileHandler.exe 1

我该如何着手解决这个问题?

回答

17

编辑Visual Studio项目设置,并在C/C++ - >语言设置选项视wchar_t是内置类型来没有

+2

我,and a few other people得到此错误与选项设置为*否*。它将它设置为*是*,它可以编译,但不知道它在运行时甚至可以工作,或者它实际上发生了什么变化。 – neuviemeporte 2013-07-03 00:36:10

+2

@neuviemeporte Visual Studio设置应该是“是”还是“否”取决于编译Qt库时使用的设置。如果启动Dependency Walker并将导出中的类型列表与链接器错误中的类型列表进行比较,则应该看到不匹配('wchar_t'与'unsigned short')。您必须调整您的应用程序的设置以匹配。这对运行时行为没有任何影响;它只是指示编译器为链接器生成一个正确类型的导入条目来解决。 – IInspectable 2014-04-24 12:53:41

+0

嗨,旧帖子,但我在qt创建者[qt 5.8.2]后链接cpprest后得到同样的错误。我可以在哪里设置qt创建者的设置? – Hummingbird 2017-03-27 18:03:41

-1

QtCore库没有被链接到你的应用程序。检查您的项目设置并确保QtCore4.lib位于包含库的列表中,并且路径已正确设置以查找它。

1

如果QtCore4.lib链接正确,甚至可能发生这种情况。 确保将VS选项“Treat wchar_t as built in type”关闭。

0

添加

QT += core 

.pro文件。这导致了其他答案告诉你要做的事情。 (一定要重新运行QMAKE)

22

该问题的最佳解决方案是将选项“Treat wchar_t is Built-in Type”设置为No. 但是在某些情况下,这可能是不可能的。

例如,使用wchar_t作为内置类型编译xerces_c。如果您需要同时使用xerces_c,则必须重新编译QT或xerces_c以匹配常见的内置类型设置。

Windows使用UTF16字符集,所以QT用于unicode字符串。因此,下面的alternative solution可能是一种救命。

/*! Convert a QString to an std::wstring */ 
std::wstring qToStdWString(const QString &str) 
{ 
#ifdef _MSC_VER 
    return std::wstring((const wchar_t *)str.utf16()); 
#else 
    return str.toStdWString(); 
#endif 
} 

/*! Convert an std::wstring to a QString */ 
QString stdWToQString(const std::wstring &str) 
{ 
#ifdef _MSC_VER 
    return QString::fromUtf16((const ushort *)str.c_str()); 
#else 
    return QString::fromStdWString(str); 
#endif 
} 
0

的boost ::文件系统::路径也似乎需要项目设置“治疗WCHAR为内置型= YES”,那么vahapt的解决方案是唯一一个我可以去上班。

0

我看到一个类似的问题,但这些解决方案都没有工作。我终于意识到我正在编译Visual Studio 2017,但是链接到使用Visual Studio 2013编译的Qt。一旦我选择了正确的版本,它就可以正常工作。