首页 > 解决方案 > 将 QString 与 wchar_t 一起使用时无法解析的外部符号

问题描述

以下代码无法在 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
}

错误 LNK2019:未解析的外部符号“__declspec(dllimport) public: int __thiscall QString::toWCharArray(unsigned short *)const” (__imp_?toWCharArray@QString@@QBEHPAG@Z) 在函数 _main 中引用

错误 LNK2019:未解析的外部符号“__declspec(dllimport) public: static class QString __cdecl QString::fromWCharArray(unsigned short const *,int)”(__imp_?fromWCharArray@QString@@SA?AV1@PBGH@Z) 在函数 _main 中引用

标签: c++qtqstringunresolved-externalwchar-t

解决方案


该项目设置为不wchar_t视为内置类型 ( No /Zc:wchar_t-)。

在此处输入图像描述

更改值以Yes /Zc:wchar_t解决链接错误。它也适用于QString::toStdWStringQString::fromStdWString


我正在记录从旧项目迁移时通常会发现的这个问题。


推荐阅读