Unable to use libclang with Qt

I had a strange error when I tried to use libclang in a Qt application.

test.cpp

#include <QApplication> #include <QMainWindow> #include <clang-c/Index.h> int main (int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow w; w.show(); CXIndex index = clang_createIndex(0, 0); Q_UNUSED(index) return a.exec(); } 

test.pro

 QT += core widgets TARGET = test TEMPLATE = app SOURCES += test.cpp LIBS += -lclang 

Commands and output shell commands:

 $ ls test.cpp test.pro $ qmake $ make g++ -c -pipe -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt/mkspecs/linux-g++ -I. -I/usr/include/qt -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore -I. -o test.o test.cpp g++ -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,-O1 -o test test.o -lclang -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread $ ./test Two passes with the same argument (-alloca-hoisting) attempted to be registered! Segmentation fault 

If I manually run g ++ without using qmake, I get the same error:

 $ g++ -fPIE test.cpp -o test -I/usr/include/qt -I/usr/include/qt/QtWidgets -lQt5Widgets -lclang $ ./test Two passes with the same argument (-alloca-hoisting) attempted to be registered! Segmentation fault 
  • If I comment out the line w.show(); , the program compiles and runs, even if it enters the main loop without the window shown.
  • If I comment on the lines CXIndex index = clang_createIndex(0, 0); and Q_UNUSED(index) , the program compiles and runs. It enters the main loop with a visible window.
  • I also compiled this with clang and I get the same error message.
  • I searched on the Internet and I found only this result with a similar error message, but I don’t know if and how it can help me: http://comments.gmane.org/gmane.comp.compilers.llvm.devel / 34647 .

I use Qt 5.1 and ArchLinux, I have the clang package (version 3.3) installed, which includes the libclang headers and the /usr/lib/libclang.so and / usr / lib / libclang.a files.

What is the reason why this program does not work and how to fix it?


Update: I found this page . Doing LIBGL_ALWAYS_INDIRECT=1 ./test works well, but I want more than that. I did not need to set this environment variable to run my program.

+6
source share
1 answer

I can answer part of your question about what is going wrong, I don’t know how to fix it.

Firstly, removing CXIndex index = clang_createIndex(0, 0); wouldn’t fix things if you didn’t have -Wl,--as-needed removal was only fixed because the linker noticed that you didn’t actually call libclang and therefore didn’t actually link your program without it CXIndex index = clang_createIndex(0, 0); .

The reason for the break is that any Mesa backend that you use (either ATI or NVIDIA) is also associated with clang. It seems that when your program is first loaded and dynamic links are allowed, the linker goes and loads the libclang links and other libclang LLVM files and runs the constructors for global objects, namely, how LLVM automatically registers it in passes. Thus, at this stage, all built-in LLVM passes are registered, then QT is launched and creates an OpenGL context, so Mesa loads the corresponding DRI server and, as it happens on your system, the backend uses clang / LLVM, and for some reason it seems that all of these constructors start up again, and LLVM notices that the “two” passes (actually the same pass that tries to register twice) have the same name and interrupt your program.

As I said, I don’t know why the constructors work twice, and I don’t know how to stop it. Try requesting the mesa-users mailing list; if you do not get an answer, try mesa-dev

Mesa mailing lists: http://mesa3d.org/lists.html

EDIT: you must make sure that your copy of Mesa is linked to the same version of LLVM that you are trying to use, if it does not fix the pass record, it will be the least of your problems.

Try ls /usr/lib64/llvm/libLLVM-?.?.so , if you have two things, you have two versions of libLLVM that are not a problem based on it, but if you are linking one version and Mesa links to another version that can explain things.

+2
source

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


All Articles