Conversion of std::wstring to QString throws linker error

12,372

Solution 1

Edit your Visual Studio project settings and under C/C++ -> Language set the option Treat wchar_t was Built-in Type to No.

Solution 2

Best solution to the problem is to set the option "Treat wchar_t was Built-in Type" to No. However in some cases this might not be possible.

For example xerces_c is compiled with wchar_t as Built-in Type. If you need to use both xerces_c then you must either recompile QT or xerces_c to match a common Built-in Type setting.

Windows uses UTF16 charset so does QT for unicode strings. Thus, the alternative solution below might be a life saver.

/*! 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
}

Solution 3

This even may happen if QtCore4.lib is linked correctly. Make sure that the VS option "Treat wchar_t as built in type" is turned OFF.

Share:
12,372

Related videos on Youtube

Eternal Learner
Author by

Eternal Learner

Updated on October 15, 2020

Comments

  • Eternal Learner
    Eternal Learner over 3 years

    Hi I need to convert a std::wstring to QString and I tried the most obvious way of

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

    I got the error:

    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 &)" (__imp_?fromStdWString@QString@@SA?AV1@ABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@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)" (?deleteDir@FileHandler@@QAEXAAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@HI_N1@Z) filehandler.obj

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

    I also tried using the method QString::fromWCharArray:

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

    The error I get is

    Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl QString::fromWCharArray(wchar_t const*,int)" (__imp_?fromWCharArray@QString@@SA?AV1@PB_WH@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)" (?deleteDir@FileHandler@@QAEXAAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@HI_N1@Z) filehandler.obj

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

    How do I proceed to resolve this issue?

  • neuviemeporte
    neuviemeporte almost 11 years
    I, <a href="qt-project.org/forums/viewthread/23714">and a few other people</a> got this error with the option set to No. It was setting it to Yes that got the thing to compile, but not sure if it even works during runtime, or what it actually changes.
  • IInspectable
    IInspectable almost 10 years
    @neuviemeporte Whether the Visual Studio setting should be "Yes" or "No" depends on the setting that was used when compiling the Qt library. If you fire up Dependency Walker and compare the type list in the export with the type list in the linker error you should see a mismatch (wchar_t vs. unsigned short). You have to adjust the setting of your application to match. This will not have any effect on the run time behavior; it merely instructs the compiler to produce a properly typed import entry for the linker to resolve.
  • Hummingbird
    Hummingbird about 7 years
    hi, old post but i am getting same error in qt creator [qt 5.8.2] after linking cpprest. where can i set such settings in qt creator?

Related