Undefined symbol: _ZN7QString13toUtf8_helperERKS_ at run time


I have two projects using Qt. One is being developed with QtCreator, the other with eclipse. Both use the same Qt 5.3.1 libraries, both are compiled with GCC. However, when I run the program that is in eclipse, it crashes with the message Undefined symbol: _ZN7QString13toUtf8_helperERKS_.

A lookup showed that the code producing this error is

path.toStdString().c_str()     // path is a QString

and the exact location within qstring.h is

    QByteArray toLatin1() const & Q_REQUIRED_RESULT
    { return toLatin1_helper(*this); }
    QByteArray toLatin1() && Q_REQUIRED_RESULT
    { return toLatin1_helper_inplace(*this); }
    QByteArray toUtf8() const & Q_REQUIRED_RESULT
    { return toUtf8_helper(*this); } //                 <- here
    QByteArray toUtf8() && Q_REQUIRED_RESULT
    { return toUtf8_helper(*this); }
    QByteArray toLocal8Bit() const & Q_REQUIRED_RESULT
    { return toLocal8Bit_helper(constData(), size()); }
    QByteArray toLocal8Bit() && Q_REQUIRED_RESULT
    { return toLocal8Bit_helper(constData(), size()); }

Converting a QString to std::string in the other project (the one in QtCreator) works just fine. What might be the problem? Are there any compiler options missing in eclipse?

Edit: The compiler outputs for sample files from the two projects are (I added line breaks for readability):


Building file: ../src/mysource1.cpp
Invoking: GCC C++ Compiler
g++ -I"/home/username/myfolder/myproject1/src"
    -I/usr/local/Qt-5.3.1/include -I/usr/local/Qt-5.3.1/include/QtCore
    -I/usr/local/Qt-5.3.1/include/QtGui -I/usr/local/Qt-5.3.1/include/QtWidgets
    -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -fPIE -MMD -MP
    -MF"src/mysource1.d" -MT"src/mysource1.d" -o "src/mysource1.o"


    -DQT_CORE_LIB -I/usr/local/Qt-5.3.1/mkspecs/linux-g++ -I../myproject2
    -I../myproject2/src -I../myproject2/src/data -I../myproject2/src/dialogs
    -I../myproject2/src/widgets -I../myproject2/src/widgets/overlays
    -I../myproject2/src/widgets/tools -I/usr/local/Qt-5.3.1/include
    -I/usr/local/Qt-5.3.1/include/QtWidgets -I/usr/local/Qt-5.3.1/include/QtGui
    -I/usr/local/Qt-5.3.1/include/QtCore -I. -I. -o mainwind.o

Edit2: The linker outputs are as follows:


Invoking: GCC C++ Linker
g++ -L/usr/local/Qt-5.3.1/lib -o "myproject1" /*list of .o files*/
    -lQt5Core -lQt5Gui -lQt5Widgets -lgit2


g++ -Wl,-rpath,/usr/local/Qt-5.3.1/lib -o myproject2 /*list of .o files*/
    -L/usr/local/Qt-5.3.1/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread

qmake/cmake is the way to go. But it is interesting to see how come linker succeeds and then during run, we get an undefined reference( not an unmatched dll case ). Especially since there is no preprocessor guard for toUtf8_helper in QT github repo. One more try. Can you add -DQT_COMPILING_QSTRING_COMPAT_CPP so that, we take slightly different path with the toUtf8() in #else part and not invoke the missing toUtf8_helper method.

Cause: I assume this happened since QtCore dll got built with a set of preprocessor flags which was not matched during the build of your project. Then the header files like qstring.h included in your code, which has inline functions, guarded with preprocessor definitions, as in for toUtf8() which is called during toStdString() will take a different path.

Also note that trying to build QT project with out qmake/cmake files as in this question/answer is not advised, and is bound to cause more problems in projects.