title: Cannot use libclang with Qt
body: <p>I encountered a strange bug when I tried to use libclang in a Qt application.</p>
  
  <p><strong>test.cpp</strong></p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QMainWindow&gt;
  
  #include &lt;clang-c/Index.h&gt;
  
  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();
  }
  </code></pre>
  
  <p><strong>test.pro</strong></p>
  
  <pre><code>QT += core widgets
  
  TARGET = test
  TEMPLATE = app
  
  SOURCES += test.cpp
  
  LIBS += -lclang
  </code></pre>
  
  <p>Shell commands and output:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>If I manually run g++ without using qmake, I get the same error:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <ul>
  <li>If I comment the <code>w.show();</code> line the program compiles and runs even if it enters the main loop without the window shown.</li>
  <li>If I comment the <code>CXIndex index = clang_createIndex(0, 0);</code> and <code>Q_UNUSED(index)</code> lines, the program compiles and runs. It enters the main loop with the window visible.</li>
  <li>I also compiled this with clang and I get the same error message.</li>
  <li>I searched the web and I found only this result with a similar error message but I don't know if and how it can help me: <a href="http://comments.gmane.org/gmane.comp.compilers.llvm.devel/34647" rel="noreferrer">http://comments.gmane.org/gmane.comp.compilers.llvm.devel/34647</a> .</li>
  </ul>
  
  <p>I am using Qt 5.1 and ArchLinux, I have the <code>clang</code> package (version 3.3) installed which includes the libclang headers and the files /usr/lib/libclang.so and /usr/lib/libclang.a.</p>
  
  <p>What is the reason why this program does not work and how can I fix it?</p>
  
  <hr>
  
  <p><strong>Update:</strong> I've found <a href="https://qt-project.org/forums/viewthread/24154" rel="noreferrer">this page</a>.
  Running <code>LIBGL_ALWAYS_INDIRECT=1 ./test</code> works well, but I want more than this. I shouldn't have to set that environment variable to be able to run my program.</p>