Undefined character: _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 compiled using GCC. However, when I run an eclipse program, it crashes with the Undefined symbol: _ZN7QString13toUtf8_helperERKS_ message Undefined symbol: _ZN7QString13toUtf8_helperERKS_ .

The search showed that the code generating this error,

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

and exact location in qstring.h

 #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 to another project (the one in QtCreator) works fine. What could be the problem? Are there any compiler options in eclipse?

Edit: Compiler output for sample files from two projects (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 
+5
source share
4 answers

qmake/cmake is the way to go. But it’s interesting to see how the linker works, and then during startup we get an undefined link (and not an unsurpassed case with dll). Especially since there is no preprocessor protector for toUtf8_helper in QT github repo . Another attempt. Can you add -DQT_COMPILING_QSTRING_COMPAT_CPP so that we change the path a bit with toUtf8() in #else and not call the missing toUtf8_helper method.

Reason: I assume this happened after the QtCore dll was built with a set of preprocessor flags that were not matched during the build of your project. Then header files like qstring.h included in your code that have built-in functions protected by preprocessor definitions, as for toUtf8() , which is called during toStdString() , will have a different path.

Also note that trying to create a QT project with qmake/cmake files, as in this question / answer, is not recommended, and is associated with big problems in the projects.

+6
source

Undefined symbol: _ZN7QString13toUtf8_helperERKS_ most likely caused by:

Linking in eclipse is done incorrectly (legacy Qt libraries) due to the lack of -Wl,-rpath,/usr/local/Qt-5.3.1/lib or LD_LIBRARY_PATH . The Eclipse build environment is not configured as the QtCreator build environment.

You write that you checked the dynamic linking of your application with ldd , but you must add the missing linker option -Wl,-rpath,/usr/local/Qt-5.3.1/lib in eclipse.

(Another alternative is to use LD_LIBRARY_PATH, but it sounds like both build environments are equal. Does ldd affect LD_LIBRARY_PATH, like the application does. 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++

+2
source

I assume that you want to convert QString to const char * using the QString.toStdString () function. c_str (). This is not a reliable way to do this. I don’t know what the documents say, but I tried to get the file location using this function and it gave me something strange. Check if the const char * value has the value specified in QString. If so, then you have two options.

  • either do it in two steps. those. QString().toStdString() and in the next step convert it to const char* . Although it is not clean, but it works. (The main problem is to have a β€œdeep copy.” When you convert a QString to std :: string, Qt creates a temporary object and directly tries to read this temporary object. Using this method, you will create two separate objects.
  • Use QString().toAscii.constData() or QString():toUtf8().constData() ( QString():toUtf8().constData() )
0
source

In appearance you do not have a compilation error, but there is a link. You did not tell us the binding command you are using, you must check to see if they are different. To use QString methods correctly, you need to bind to QtCore.

What build system are you using? Try it from the command line? What if you compile it yourself? I read some of your comments, you seem to have checked the executables with ldd , is the output of both of them the same?

In addition, these flags are: -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB , they probably matter, you should try to define them in the Eclipse project as well.

Qt libraries can be difficult to use without using your own system without proper assembly (for example, the Eclipse project does not have the appropriate definitions), especially if you intend to use QtQuick or are planning on the future of Android. I would go with qmake or CMake. If your plan is to support various IDEs, CMake is worth it, as it can generate projects for most of them.

0
source

Source: https://habr.com/ru/post/1210146/


All Articles