PDA

View Full Version : undefined reference to QString::toStdWString



isml
2nd May 2013, 12:17
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!

wysota
2nd May 2013, 14:07
Post your project file, please.

isml
2nd May 2013, 15:13
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.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|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.prop s" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).u ser.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.prop s" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).u ser.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)'=='Release|Win32'">AllRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRules Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PreprocessorDefinitions>UNICODE;WIN32;QT_CORE_LIB;QT_DLL;%(PreprocessorDef initions)</PreprocessorDefinitions>
<Optimization>Disabled</Optimization>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<AdditionalIncludeDirectories>.;$(QTDIR)\include;.\GeneratedFiles\$(Configuratio nName);$(QTDIR)\include\QtCore;%(AdditionalInclude Directories)</AdditionalIncludeDirectories>
<TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>kernel32.lib;user32.lib;shell32.lib;uuid.lib;ole32 .lib;advapi32.lib;ws2_32.lib;qtmaind.lib;Qt5Cored. lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PreprocessorDefinitions>UNICODE;WIN32;QT_NO_DEBUG;NDEBUG;QT_CORE_LIB;QT_DL L;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<DebugInformationFormat>
</DebugInformationFormat>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<AdditionalIncludeDirectories>.;$(QTDIR)\include;.\GeneratedFiles\$(Configuratio nName);$(QTDIR)\include\QtCore;%(AdditionalInclude Directories)</AdditionalIncludeDirectories>
<TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>kernel32.lib;user32.lib;shell32.lib;uuid.lib;ole32 .lib;advapi32.lib;ws2_32.lib;qtmain.lib;Qt5Core.li b;%(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>

d_stranz
2nd May 2013, 17:56
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</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.

isml
3rd May 2013, 15:18
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.

d_stranz
3rd May 2013, 18:57
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:


<Keyword>Qt4VSv1.0</Keyword>

isml
4th May 2013, 04:50
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.

amleto
4th May 2013, 12:07
but you didn't have a compiler error in the first place :confused:

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.

isml
4th May 2013, 13:29
I am sorry I did not say it clearly. I mean after set /Zc:wchar_t, QString's toStdWString method can link successfully.

d_stranz
5th May 2013, 00:19
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.