[CMake] Linking to Qt static libraries fails using cmake but not qmake (both OSX and Linux)
ghleclerc at gmail.com
Tue Mar 10 13:42:44 EDT 2015
I have just run into this problem a few weeks ago. My explanation might not be perfect, but from what I understand, it is related to the fact that the libraries which Qt depends upon have to be available for static linking to your application. Those dependencies are “magically” brought in by qmake, but the CMake find package for Qt does not bring those in. There have been some attempts, from what I gather, but nothing official for static linking of Qt applications with CMake. Thus, my approach has been to find the system libraries myself using CMake and add them to the target_link_libraries() command. Not much fun or robust (from my perspective), but works. I have been able to statically compile and run the application on a colleague’s computer (where Qt was not installed at all).
There is also the concern of the plugins, mainly the platform plugins concept of Qt, which have to be compiled into your application. If I understand correctly, when you request static linking from qmake (+= static), qmake creates a cpp file and adds it to your project. This file simply contains code to insure that the plugins are compiled and can be linked against. For CMake, you have to manually add these includes. I do it in the main.cpp file or whatever file your main is in.
Here are links to the earlier discussion:
Stephen Kelly is much more knowledgable then me on this. He usually responds pretty fast to the list (he helped me a lot on that).
Hope my explanations are not too confusing and that they help a little.
Best of luck.
This has been driving me crazy for a while now. Occurs for both OSX and
OSX Mavericks 10.9.5, openSUSE Linux 12.3
Qt 5.4.1 (both shared and static builds, compiled from source)
Using cmake to compile and link the simple Qt HelloWorld app against Qt
shared libraries works just fine.
Using the same cmake file and specifying the Qt static libraries instead,
it fails in the linking (OSX: Undefined symbols for architecture x86_64,
ld: symbol(s) not found for architecture x86_64?; LINUX: undefined
reference to ?, collect2 error: ld returned 1 exit status)
Message would seem to point towards static libraries not compiled 64 bit.
But, if I use qmake to generate the makefile to use with the Qt static
libraries, it compiles and links with no problem. No symbol problem here.
Seems to indicate the static libraries are fine.
Since using qmake seems to work fine I can?t pin down what the problem is
Can anyone shed some light on this?
Source, CMakeLists.txt, and qmake .pro contents below. I?ve also attached
failed linking output from OSX and linux cmake/make.
int main(int argc, char **argv)
QApplication app (argc, argv);
QPushButton button ("Hello world !");
SOURCES = main.cpp
CONFIG += qt warn_on release
QT += core widgets gui
Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the CMake