这个问题在这里已有答案:
以下代码无法在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)在function _main中引用
1 回答
该项目设置为不将
wchar_t
视为内置类型(No /Zc:wchar_t-
) .将值更改为
Yes /Zc:wchar_t
可解决链接错误 . 它也适用于QString::toStdWString
和QString::fromStdWString
.我正在记录我从旧项目迁移时经常发现的这个问题 .