Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined symbol: _ZN7QString13toUtf8_helperERKS_ at runtime

Tags:

c++

gcc

eclipse

qt

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

#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP)
    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):

Eclipse

Building file: ../src/mysource1.cpp
Invoking: GCC C++ Compiler
g++ -I"/home/username/myfolder/myproject1/src"
    -I"/home/username/myfolder/myproject1/src/dialogs"
    -I"/home/username/myfolder/myproject1/src/ignore"
    -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"
    "../src/mysource1.cpp"

QtCreator

g++ -c -pipe -g -Wall -W -D_REENTRANT -fPIE -DQT_WIDGETS_LIB -DQT_GUI_LIB
    -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
    ../myproject2/src/mainwind.cpp

Edit2: The linker outputs are as follows:

Eclipse

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

QtCreator

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
like image 716
Anton Poznyakovskiy Avatar asked Dec 31 '14 00:12

Anton Poznyakovskiy


2 Answers

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.

like image 94
kiranpradeep Avatar answered Oct 14 '22 15:10

kiranpradeep


Undefined symbol: _ZN7QString13toUtf8_helperERKS_ is likely caused by:

Linking in eclipse is made to wrong (outdated Qt libraries) due to missing -Wl,-rpath,/usr/local/Qt-5.3.1/lib or LD_LIBRARY_PATH. Eclipse build environment is not setup as the QtCreator build environment.

You write you have checked dynamic linking of your app using ldd but you should add missing linker option -Wl,-rpath,/usr/local/Qt-5.3.1/libin eclipse as well.

(Another alternative would be to use LD_LIBRARY_PATH, but it is sound to keep both build environments equal. ldd is influenced by LD_LIBRARY_PATH as is the app. LD_LIBRARY_PATH might not be exported correctly?)

Also add compiler flags -D_REENTRANT -fPIE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/local/Qt-5.3.1/mkspecs/linux-g++

like image 29
user2672165 Avatar answered Oct 14 '22 17:10

user2672165