1

The following code fails to link in Visual Studio:

#include <qstring.h>

int main(int argc, char *argv[])
{
  const auto qstr = QString::fromWCharArray(L"Hello world!");

  auto wstr = new wchar_t[qstr.length() + 1];
  const auto wlen = qstr.toWCharArray(wstr);
  wstr[wlen] = L'\0';

  return 0; // 'wstr' not deleted for simplification
}

error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __thiscall QString::toWCharArray(unsigned short *)const " (__imp_?toWCharArray@QString@@QBEHPAG@Z) referenced in function _main

error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl QString::fromWCharArray(unsigned short const *,int)" (__imp_?fromWCharArray@QString@@SA?AV1@PBGH@Z) referenced in function _main

cbuchart
  • 10,847
  • 9
  • 53
  • 93

1 Answers1

3

The project is set to not treat wchar_t as a built in type (No /Zc:wchar_t-).

enter image description here

Changing the value to Yes /Zc:wchar_t solves the linking errors. It also applies to QString::toStdWString and QString::fromStdWString.


I'm documenting this issue I usually find when migrating from old projects.

cbuchart
  • 10,847
  • 9
  • 53
  • 93