title: qt cross compilation for raspberry pi: Cannot find GLESv2
body: <p>I am trying to compile <code>qt 5.9</code> for my raspberry pi 3 compute module and I have been following mainly the online guide here (<a href="https://wiki.qt.io/RaspberryPi2EGLFS" rel="nofollow noreferrer">https://wiki.qt.io/RaspberryPi2EGLFS</a>). So, I have set the pi system roots to the mount point <code>/mnt/pi-rootfs</code>. I have also set the symlinks to be relative using the script <code>sysroot-relativelinks.py</code>. </p>
  
  <p>The qtbase configuration was called with the following argument:</p>
  
  <pre><code>./configure -opengl es2 -device linux-rpi3-g++ -device-option 
  CROSS_COMPILE=/home/osboxes/Downloads/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf- 
  -sysroot /mnt/pi-rootfs -opensource -confirm-license -optimized-qmake 
  -reduce-exports -release -make libs -prefix /usr/local/qt5pi 
  -hostprefix /usr/local/qt5pi
  </code></pre>
  
  <p>This configures qt but no openGL and I have the message:</p>
  
  <pre><code>ERROR: Feature 'opengles2' was enabled, but the pre-condition 'config.win32 || (!config.watchos &amp;&amp; !features.opengl-desktop &amp;&amp; libs.opengl_es2)' failed.
  
  ERROR: The OpenGL functionality tests failed!
  You might need to modify the include and library search paths by editing QMAKE_INCDIR_OPENGL[_ES2],
  QMAKE_LIBDIR_OPENGL[_ES2] and QMAKE_LIBS_OPENGL[_ES2] in the mkspec for your platform.
  
  Check config.log for details.
  </code></pre>
  
  <p>Looking at the config file, I have:</p>
  
  <pre><code>&gt; /home/osboxes/Downloads/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ -c -march=armv8-a -mtune=cortex-a53 -mfpu=crypto-neon-fp-armv8 -mfloat-abi=hard --sysroot=/mnt/pi-rootfs --sysroot=/mnt/pi-rootfs -O2 -Wall -W -fPIC  -I. -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I/home/osboxes/Downloads/qt5/qtbase/mkspecs/devices/linux-rpi3-g++ -o opengles2.o opengles2.cpp
  &gt; opengles2.cpp:44:25: fatal error: GLES2/gl2.h: No such file or directory
  &gt;    #include &lt;GLES2/gl2.h&gt;
  &gt;                          ^
  &gt; compilation terminated.
  &gt; Makefile:179: recipe for target 'opengles2.o' failed
  &gt; make: *** [opengles2.o] Error 1
   =&gt; source failed verification.
  test config.gui.libraries.opengl_es2 FAILED
  </code></pre>
  
  <p>So, it does not set the paths correctly, so I edited the <code>mkspecs</code> file for <code>linux-rpi3-g++</code>. So, the relevant part of the file looks like:</p>
  
  <pre><code>QMAKE_LFLAGS           += -Wl,-rpath-link,$$[QT_SYSROOT]/opt/vc/lib
  
  VC_LIBRARY_PATH         = /opt/vc/lib
  VC_INCLUDE_PATH         = /opt/vc/include
  
  # terrible, they do not appear to resolve "=" in rpath!
  VC_LINK_LINE            = -L=$${VC_LIBRARY_PATH} -Wl,-rpath-link,$$[QT_SYSROOT]$${VC_LIBRARY_PATH}
  
  QMAKE_LIBDIR_OPENGL_ES2 = =$${VC_LIBRARY_PATH}
  QMAKE_LIBDIR_EGL        = $$QMAKE_LIBDIR_OPENGL_ES2
  QMAKE_LIBDIR_OPENVG     = $$QMAKE_LIBDIR_OPENGL_ES2
  
  QMAKE_INCDIR_EGL        = \
                          $${VC_INCLUDE_PATH} \
                          $${VC_INCLUDE_PATH}/interface/vcos/pthreads \
                          $${VC_INCLUDE_PATH}/interface/vmcs_host/linux
  
  QMAKE_INCDIR_OPENGL_ES2 = $${QMAKE_INCDIR_EGL}
  
  QMAKE_LIBS_OPENGL_ES2   = $${VC_LINK_LINE} -lGLESv2
  
  # The official opt vc EGL references GLESv2 symbols: need to link it
  QMAKE_LIBS_EGL          = $${VC_LINK_LINE} -lEGL -lGLESv2
  </code></pre>
  
  <p>So, I tried various things which all included adding <code>$$[QT_SYSROOT]</code> prefix at various places for example at <code>VC_LIBRARY_PATH</code> and <code>VC_INCLUDE_PATH</code> and even though I got it to find the include path it could not like to the GLES2 libraries (I have verified the libs are indeed at the correct location: <code>/mnt/pi-rootfs/opt/vc/lib</code>).</p>
  
  <p>I wonder if someone has suggestions on how to solve this hopefully without editing qt mkspecs files and doing other such hacks.</p>
  
  <p><strong>EDIT</strong>
  So, I modified the mkspecs file as follows:</p>
  
  <pre><code>QMAKE_LFLAGS           += -Wl,-rpath-link,$$[QT_SYSROOT]/opt/vc/lib
  
  VC_LIBRARY_PATH         = $$[QT_SYSROOT]/opt/vc/lib  # prepended
  VC_INCLUDE_PATH         = $$[QT_SYSROOT]/opt/vc/include #prepended
  
  # terrible, they do not appear to resolve "=" in rpath!
  VC_LINK_LINE            = -L=$${VC_LIBRARY_PATH} -Wl,-rpath-link,$$[QT_SYSROOT]$${VC_LIBRARY_PATH}
  
  QMAKE_LIBDIR_OPENGL_ES2 = =$${VC_LIBRARY_PATH}
  QMAKE_LIBDIR_EGL        = $$QMAKE_LIBDIR_OPENGL_ES2
  QMAKE_LIBDIR_OPENVG     = $$QMAKE_LIBDIR_OPENGL_ES2
  
  QMAKE_INCDIR_EGL        = \
                          $${VC_INCLUDE_PATH} \
                          $${VC_INCLUDE_PATH}/interface/vcos/pthreads \
                          $${VC_INCLUDE_PATH}/interface/vmcs_host/linux
  
  QMAKE_INCDIR_OPENGL_ES2 = $${QMAKE_INCDIR_EGL}
  
  #QMAKE_LIBS_OPENGL_ES2   = $${VC_LINK_LINE} -lGLESv2  #original
  QMAKE_LIBS_OPENGL_ES2   = -lGLESv2  #changed
  
  # The official opt vc EGL references GLESv2 symbols: need to link it
  #QMAKE_LIBS_EGL          = $${VC_LINK_LINE} -lEGL -lGLESv2 #original
  QMAKE_LIBS_EGL          = -lEGL -lGLESv2  #changed
  </code></pre>
  
  <p>And this results in the linking error when trying to find gles:</p>
  
  <pre><code>&gt; /home/osboxes/Downloads/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ -Wl,-rpath-link,/mnt/pi-rootfs/opt/vc/lib -Wl,-rpath-link,/mnt/pi-rootfs/usr/lib/arm-linux-gnueabihf -Wl,-rpath-link,/mnt/pi-rootfs/lib/arm-linux-gnueabihf -mfloat-abi=hard --sysroot=/mnt/pi-rootfs --sysroot=/mnt/pi-rootfs -Wl,-O1 -fuse-ld=gold -o opengles2 opengles2.o   -L=/mnt/pi-rootfs/opt/vc/lib -lGLESv2 
  &gt; /home/osboxes/Downloads/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld.gold: error: cannot find -lGLESv2
  &gt; opengles2.o:opengles2.cpp:function main: error: undefined reference to 'glUniform1f'
  &gt; opengles2.o:opengles2.cpp:function main: error: undefined reference to 'glClear'
  &gt; collect2: error: ld returned 1 exit status
  &gt; Makefile:65: recipe for target 'opengles2' failed
  </code></pre>
  
  <p>The path <code>-L=/mnt/pi-rootfs/opt/vc/lib</code> is correct and I verify that this is where the glesv2 library resides.</p>