Results 1 to 10 of 10

Thread: undefined reference to QString::toStdWString

  1. #1
    Join Date
    May 2013
    Posts
    5

    Default undefined reference to QString::toStdWString

    I use Qt 5.0.2 msvc verion, downloaded from qt-project. After installation, I create a new qt console application, and write the following code:

    #include <QtCore/QString>

    int main(int argc, char *argv[])
    {
    QString s;
    s.toUtf8();
    s.toStdWString();

    return 0;
    }


    when I compile it , I received the following link error:

    error LNK2019: undefined reference "__declspec(dllimport) public: class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > __thiscall QString::toStdWString(void)const " (__imp_?toStdWString@QString@@QBE?AV?$basic_string @GU?$char_traits@G@std@@V?$allocator@G@2@@std@@XZ)

    Where am I wrong? Thanks for any help!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: undefined reference to QString::toStdWString

    Post your project file, please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2013
    Posts
    5

    Default Re: undefined reference to QString::toStdWString

    Thanks for your reply.
    I test it again, and find when I use QtCreator, it can link with no error.But when I use VS, it failed. Here is my vs project file:

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
    <Configuration>Debug</Configuration>
    <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
    <Configuration>Release</Configuration>
    <Platform>Win32</Platform>
    </ProjectConfiguration>
    </ItemGroup>
    <PropertyGroup Label="Globals">
    <ProjectGuid>{CEF8489E-810A-4DD9-A225-518653A308C3}</ProjectGuid>
    <Keyword>Qt4VSv1.0</Keyword>
    </PropertyGroup>
    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.pr ops" />
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug| Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Releas e|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    </PropertyGroup>
    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
    <ImportGroup Label="ExtensionSettings">
    </ImportGroup>
    <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug| Win32'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform). user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$( Platform).user.props')" Label="LocalAppDataPlatform" />
    </ImportGroup>
    <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Releas e|Win32'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform). user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$( Platform).user.props')" Label="LocalAppDataPlatform" />
    </ImportGroup>
    <PropertyGroup Label="UserMacros" />
    <PropertyGroup>
    <_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Debug| Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Debug| Win32'" />
    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Debug| Win32'" />
    <CodeAnalysisRuleSet Condition="'$(Configuration)|$(Platform)'=='Releas e|Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
    <CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Releas e|Win32'" />
    <CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Releas e|Win32'" />
    <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug| Win32'">$(SolutionDir)$(Platform)\$(Configuration) \</OutDir>
    <OutDir Condition="'$(Configuration)|$(Platform)'=='Releas e|Win32'">$(SolutionDir)$(Platform)\$(Configuratio n)\</OutDir>
    </PropertyGroup>
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug| Win32'">
    <ClCompile>
    <PreprocessorDefinitions>UNICODE;WIN32;QT_CORE_LIB ;QT_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    <Optimization>Disabled</Optimization>
    <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
    <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
    <AdditionalIncludeDirectories>.;$(QTDIR)\include;. \GeneratedFiles\$(ConfigurationName);$(QTDIR)\incl ude\QtCore;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    <TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
    </ClCompile>
    <Link>
    <SubSystem>Console</SubSystem>
    <OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
    <AdditionalLibraryDirectories>$(QTDIR)\lib;%(Addit ionalLibraryDirectories)</AdditionalLibraryDirectories>
    <AdditionalDependencies>kernel32.lib;user32.lib;sh ell32.lib;uuid.lib;ole32.lib;advapi32.lib;ws2_32.l ib;qtmaind.lib;Qt5Cored.lib;%(AdditionalDependenci es)</AdditionalDependencies>
    <GenerateDebugInformation>true</GenerateDebugInformation>
    </Link>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Releas e|Win32'">
    <ClCompile>
    <PreprocessorDefinitions>UNICODE;WIN32;QT_NO_DEBUG ;NDEBUG;QT_CORE_LIB;QT_DLL;%(PreprocessorDefinitio ns)</PreprocessorDefinitions>
    <DebugInformationFormat>
    </DebugInformationFormat>
    <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
    <AdditionalIncludeDirectories>.;$(QTDIR)\include;. \GeneratedFiles\$(ConfigurationName);$(QTDIR)\incl ude\QtCore;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    <TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
    </ClCompile>
    <Link>
    <SubSystem>Console</SubSystem>
    <OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
    <AdditionalLibraryDirectories>$(QTDIR)\lib;%(Addit ionalLibraryDirectories)</AdditionalLibraryDirectories>
    <AdditionalDependencies>kernel32.lib;user32.lib;sh ell32.lib;uuid.lib;ole32.lib;advapi32.lib;ws2_32.l ib;qtmain.lib;Qt5Core.lib;%(AdditionalDependencies )</AdditionalDependencies>
    <GenerateDebugInformation>false</GenerateDebugInformation>
    </Link>
    </ItemDefinitionGroup>
    <ItemGroup>
    <ClCompile Include="main.cpp" />
    </ItemGroup>
    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    <ImportGroup Label="ExtensionTargets">
    </ImportGroup>
    <ProjectExtensions>
    <VisualStudio>
    <UserProperties UicDir=".\GeneratedFiles" MocDir=".\GeneratedFiles\$(ConfigurationName)" MocOptions="" RccDir=".\GeneratedFiles" lupdateOnBuild="0" lupdateOptions="" lreleaseOptions="" Qt5Version_x0020_Win32="Qt 5.0.2 msvc-dynamic" />
    </VisualStudio>
    </ProjectExtensions>
    </Project>

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefined reference to QString::toStdWString

    <AdditionalLibraryDirectories>$(QTDIR)\lib;%(Addit ionalLibraryDirectories)</AdditionalLibraryDirectories>
    Do you have QTDIR defined as an environment variable? Does it point to the path where you have your installation of Qt5?

    Try removing the preprocessor definition "QT_DLL".

    Please learn how to use [CODE] tags when posting something like this in the future.

  5. #5
    Join Date
    May 2013
    Posts
    5

    Default Re: undefined reference to QString::toStdWString

    I am sorry for not using the [code] tag, next time I will try to use it.
    I removed the QT_DLL, but still failed with the same error.

    I think the problem is a little strange because the QString toUTF8() method can link sucessfully, but the toStdWString() method can not link.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefined reference to QString::toStdWString

    This might be the source of your problem: From the docs for QString::toStdWString():

    This operator is only available if Qt is configured with STL compatibility enabled.
    (but in this case, I would think that your program wouldn't even compile).


    Did you build your Qt distribution from sources for Visual Studio? If not, did you download the right version of Qt for your VS installation?

    And this line from your project file is strange if you're using Qt5:

    Qt Code:
    1. <Keyword>Qt4VSv1.0</Keyword>
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2013
    Posts
    5

    Default Re: undefined reference to QString::toStdWString

    Thanks for your reply.
    I did not compile the Qt 5 myself, I downloaded it from qt-project. The reason why it shows "Qt4VSv1.0" maybe I previously installed the Qt 4 version VS Add-in but then I uninstalled it and install the Qt 5 version.


    But finally I work it out. I write it down here to anyone who may have met the same problem like me.

    Just change the VC compile flag to /Zc:wchar_t

    After that, it compiles perfectly.

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: undefined reference to QString::toStdWString

    but you didn't have a compiler error in the first place

    If you use 'the' (there are actually multiple versions) msvc Qt download then it is crucial that you are using the same version of studio.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #9
    Join Date
    May 2013
    Posts
    5

    Default Re: undefined reference to QString::toStdWString

    I am sorry I did not say it clearly. I mean after set /Zc:wchar_t, QString's toStdWString method can link successfully.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefined reference to QString::toStdWString

    Just change the VC compile flag to /Zc:wchar_t
    Yes, this is crucial. But usually the Qt VS add-in does this automatically when you create a new project or import a .pro file. Did you set up an ordinary VC++ console (i.e. non-Qt) project instead, and add the Qt stuff in later? That might explain it, because ordinary VC++ projects treat wchar_t differently fro Qt.

    The projects will compile just fine without the option set - the problem comes in linking because the built-in vs. non-built-in wchar_t types are different so they name mangle differently.

Similar Threads

  1. Replies: 3
    Last Post: 23rd March 2011, 22:39
  2. undefined reference
    By digidas in forum Newbie
    Replies: 9
    Last Post: 19th May 2010, 13:04
  3. Undefined reference to crt
    By derektaprell in forum Installation and Deployment
    Replies: 0
    Last Post: 20th October 2009, 08:34
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. Undefined reference
    By Salazaar in forum Newbie
    Replies: 12
    Last Post: 23rd May 2007, 10:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.