2011-04-11 46 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。一旦我選擇了正確的版本,它就可以正常工作。