Hi, 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: https://www.mail-archive.com/cmake%40cmake.org/msg51188.html https://www.mail-archive.com/cmake%40cmake.org/msg51170.html 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. Ghyslain This has been driving me crazy for a while now. Occurs for both OSX and Linux. OSX Mavericks 10.9.5, openSUSE Linux 12.3 CMake 3.1.1 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 with cmake. 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. main.cpp: #include <QApplication> #include <QPushButton> int main(int argc, char **argv) { QApplication app (argc, argv); QPushButton button ("Hello world !"); button.show(); return app.exec(); } CMakeLists.txt: cmake_minimum_required(VERSION 2.8.11) project(helloworld) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) find_package(Qt5Widgets REQUIRED) add_executable(helloworld main.cpp) target_link_libraries(helloworld Qt5::Widgets) main.pro: SOURCES = main.cpp CONFIG += qt warn_on release QT += core widgets gui Thanks in advance.
-- Powered by www.kitware.com Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Kitware offers various services to support the CMake community. For more information on each offering, please visit: CMake Support: http://cmake.org/cmake/help/support.html CMake Consulting: http://cmake.org/cmake/help/consulting.html CMake Training Courses: http://cmake.org/cmake/help/training.html Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Follow this link to subscribe/unsubscribe: http://public.kitware.com/mailman/listinfo/cmake
