2

I have a simple Qt-5 console application that I am developing in Visual Studio 2015. I can use Qt objects such as QStrings so I'm sure Qt is installed correctly, but I need to convert a QString to a char* so I can pass it to a library function.

I followed this answer: QString to char* conversion

So I now have code that looks like:

QString myStr = "SomeString";
QByteArray ba = myStr.toLatin1();
const char *c_str = ba.data();

But when I build the solution I get the following error:

Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: class QByteArray __thiscall QString::toLatin1(void)const & " (__imp_?toLatin1@QString@@QGBE?AVQByteArray@@XZ) referenced in function _main    

The problem seems to be the toLatin1() call, but I have no idea why the linker can't find it, especially since I can use other QString methods with no problem.

Community
  • 1
  • 1
Huggzorx
  • 144
  • 1
  • 13
  • 5
    This could be a case of mismatching compiler toolchain and Qt libs. – hyde Oct 25 '16 at 14:53
  • Ah. It does look like I have a version of Qt compiled for Vis Studio 2013. I'll try updating it and see if that helps. – Huggzorx Oct 25 '16 at 16:04

2 Answers2

2

The linker error shows you the "mangled" C++ symbol name, which contains the class and parameters, encoded. There is no standard on C++ symbol name mangling, so different compilers do it differently. Even different versions of "same" compiler may change this (among other things, there is no standard C++ ABI), and Microsoft has done this several times with Visual C++.

As a result, any C++ libraries you use must be compiled with same C++ compiler toolchain (or a compatible compiler, but on Windows there's a lot of... unfortunate diversity, here).

This applies to Qt as well. This is why you find several downloads of Qt offline installer for Windows, and why the online installer shows so many different Qt versions with same version number but different toolchain name. If you find pre-built Qt for the compiler you want to use, great! Install that and be happy.

If not you have two options:

  • Build Qt libs yourself using your chosen compiler. This is not hard, instructions are good, but it can be a bit tedious on Windows, as there are a few pre-requisites like Python, which you need to get separately.
  • Switch compiler toolchain, so that you get a pre-built Qt libs for it (Qt Online installer can install MinGW toolchains matching the Qt libs it has for MinGW, which is rather convenient, if you don't have requirement to use something else).
hyde
  • 60,639
  • 21
  • 115
  • 176
0

As suggested by hyde, this turned out to be a mismatch between the Qt libs I was using and my version of Visual Studio. For VS2015, I had to go to the All Downloads page on the Qt website and specifically use Qt 5.7.0 for Windows (VS 2015).

Updating the Qt version I was using in my project settings got rid of the linker errors.

Huggzorx
  • 144
  • 1
  • 13