[CMake] Linker problems with compiling static QT lib with cmake

Balske at t-online.de Balske at t-online.de
Thu Jul 29 08:57:07 EDT 2010


Hello,

I am trying to compile a simple QT HelloWorld.cxx with cmake. I have a statically compiled qt installed as well as a non static version.
What I want is a statically compiled HelloWorld. Cmake allows me to choose my static build of QT (using cmake-gui) by choosing the qmake
binary in the static qt lib folder. When I do this it automatically changes the other entries like QT_LIBRARY_DIR to the static version. Looks great 
so far.
Here the problems begin though:
If I just generate my Makefile and make without adding CMAKE_EXE_LINKER_FLAGS "-static", everything seems to work and my HelloWorld binary ist beeing created. ldd reveals though that it has been linked against the shared version of the lib:

> ldd ./HelloWorld
> libQtGui.so.4 => /usr/lib/libQtGui.so.4 (0x00110000)
> ...
> ...

This struck me as strange, as all entries in my generated CMakeCache.txt point to my static install of QT which certanly is nowhere near /usr/lib (in /usr/lib my nonstatic qt is installed). The result is, I have compiled a working HelloWorld binary which is linked against my nonstatic version of QT which cmake shouldn t even know about after changing the qmake binary path in the cmake configure process.

This is not the real problem though, since I want to compile it with CMAKE_EXE_LINKER_FLAGS "-static". If I generate my Makefile with -static option, make throws the following linker error:

canning dependencies of target HelloWorld
[100%] Building CXX object CMakeFiles/HelloWorld.dir/HelloWorld.cxx.o
Linking CXX executable HelloWorld
/usr/bin/ld: cannot find -lQtGui
collect2: ld returned 1 exit status
make[2]: *** [HelloWorld] Error 1
make[1]: *** [CMakeFiles/HelloWorld.dir/all] Error 2
make: *** [all] Error 2

Somehow the linker cannot find the QtGui module, which is in my statically compiled folder of QT under lib/libQtGui.a.
Any ideas/solutions? I really have no idea at this point since everything looks fine in the cmake config/generation process. 
My CMakeLists.txt looks like this:


PROJECT(HelloWorld)

cmake_minimum_required(VERSION 2.6)

FIND_PACKAGE(Qt4)

IF(QT4_FOUND)
        INCLUDE(${QT_USE_FILE})
ELSE(QT4_FOUND)
MESSAGE(FATAL_ERROR
"Qt4 not found. Please set QT4_DIR.")
ENDIF(QT4_FOUND)

ADD_EXECUTABLE(HelloWorld HelloWorld.cxx )
TARGET_LINK_LIBRARIES(HelloWorld QtGui)




More information about the CMake mailing list