How can I detect if the used version of Qt is statically built in a cmake project?
My goal is to make my project files independent from the version of Qt that is used to build it, and there are a few places where I need to differ between dynamic and static builds.
It works like for every other library in cmake, see here:
find_package(Qt5 COMPONENTS Core REQUIRED)
get_target_property(MY_QT_TARGET_TYPE Qt5::Core TYPE)
if(MY_QT_TARGET_TYPE STREQUAL STATIC_LIBRARY)
message("Qt was built as a static library!")
endif()
Related
I have a Qt Quick project that uses CMake as its build system. I mostly went with the Qt Creator-generated CMake project, so I have
set(CMAKE_AUTORCC ON)
in my root CMakeLists.txt.
I recently learned about ahead-of-time compiling for QML resources which is supposed to yield quite a bit of startup time improvement. The documentation that describes how to use this with CMake says that I need to do the following:
find_package(Qt5 COMPONENTS Quick Core Network)
find_package(Qt5QuickCompiler)
qtquick_compiler_add_resources(RESOURCES example.qrc)
add_executable(myapp ${SRC_LIST} ${RESOURCES})
qt5_use_modules(myapp Quick Core Network)
but as far as I see, qtquick_compiler_add_resources does nothing but produce a list of C++ files which don't exist. I assume those would be produced by the QtQuick compiler, but as it is, CMake doesn't run because it can't find those sources. What is the missing bit?
Sorry, a bit late to the party but have to you tried removing the set(CMAKE_AUTORCC ON) ?
I have almost the same implementation and the <qmlfile_name>_qml.cpp files are generated and compiled correctly.
In my implementation I have :
a different CMake property name (resource_files)
I am not using AUTORCC
I'm auto-generating the qrc/qmldir files in the build directory (but that shouldn't matter)
I added the qml files list in the add_executable parameter list because I want to see them in my IDE.
I recently am struggling by trying to debug Qt sources with my MINGW,QtCreator,Qt installation.
I did create a sample app, deployed it with the windeployqt tool and let it execute but when I am trying to debug, let s say step into one of the fancy Qt classes and methods I did obtain assembly code. I am plagued by assemply code also when trying to step over the
QApplication a(argc, argv);
I did build Qt from sources so the sources should not be a problem, I did not move the sources nor removed them.
When I used the objdump tool against qt libraries (debug version) to understand where the sources had to be I discovered that the libs were pointing to the correct source location.
I did also tried to play with the qtcreator qt source mappings but I did lost only a whole day by trying to understand what the heck those mappings do expect and either this try did not get me nothing.
In short, I have qt sources where they should be but gdb is for some reason not getting it.
This is my CMakelists.txt make file which one am importing in QtCreator:
cmake_minimum_required (VERSION 2.6)
project(sampleapp2)
#set(DEPS -lglu32 -lopengl32 -lgdi32 -luser32)
set(QT_DEPS "")
find_package(Qt5Widgets)
if(Qt5Widgets_FOUND)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
set(QT_DEPS ${QT_DEPS} ${Qt5Widgets_LIBRARIES})
endif()
find_package(Qt5Core)
if(Qt5Core_FOUND)
include_directories(${Qt5Core_INCLUDE_DIRS})
set(QT_DEPS ${QT_DEPS} ${Qt5Core_LIBRARIES})
endif()
find_package(Qt5Gui)
if(Qt5Gui_FOUND)
include_directories(${Qt5Gui_INCLUDE_DIRS})
set(QT_DEPS ${QT_DEPS} ${Qt5Gui_LIBRARIES})
endif()
#print qt deps (if any has been found), if not do exit
if(QT_DEPS)
message(STATUS "Qt dependencies are: " ${QT_DEPS})
else()
message(STATUS "Qt dependencies not found, exiting...")
exit()
endif()
#set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CXXFLAGS -g)
set(QMAKE_CXXFLAGS -g)
set(GUI_TYPE WIN32)
set(LIB_SOURCES maindialog.cpp)
set(EXE_SOURCES main.cpp)
set(UI_LIB sampleapp2ui)
QT5_WRAP_CPP(sampleapp2ui_hdr_moc maindialog.h)
QT5_WRAP_UI(sampleapp2ui_form_hdr maindialog.ui)
set(LIB_SOURCES ${LIB_SOURCES} ${sampleapp2ui_hdr_moc} ${sampleapp2ui_form_hdr})
add_library(${UI_LIB} SHARED ${LIB_SOURCES})
#qt5_use_modules(${UI_LIB} Core Gui Widgets)
target_link_libraries(${UI_LIB} ${QT_DEPS})
add_executable(sampleapp2 ${GUI_TYPE} ${EXE_SOURCES})
target_link_libraries(sampleapp2 ${UI_LIB} ${QT_DEPS})
#target_link_libraries(sampleapp2 ${UI_LIB})
#qt5_use_modules(sampleapp2 Core Gui Widgets)
Another wired thing is that I completely can not jump in the maindialog.cpp definitions which is something I should be able to - the maindialog.cpp is not part of any qt library - it is my own main dialog wrapped around the qt resources that I am using to specialize it.
Thank you in advice!
From this documentation it is not clear whether it is possible to build an executable that uses shared libraries once Qt (> 5.0) has been rebuilt in static mode.
I need to build both executables that use shared libraries and executables which are usable without them. Do I need to have two separate installations of Qt for that, or is it possible to use a flag to specify the desired behaviour?
Is it possible to configure QtCreator for this purpose?
Platform: Windows 7, MinGW32/GCC 4.8
I am currently using Qt on Windows 7 in the (almost) exact configuration as you described. Only thing is that you will have to create 2 different compilation Kits for each purpose.
For static compile I have mingw32 compiler and for shared compile I have MSVC2010 32 bit SDK.
EDIT:
Yes I have a statically built version of Qt 4.8.4 (qmake) and I have a Qt 5.0.0 MSVC2010 32bit (SDK) (normally built). I have created 2 separate 'Kits' using these two versions and having different compilers as mentioned. You need to include the following line to the .pro file. It gets ignored during shared build.
#CONFIG += staticlib
I am new to QT. I have just installed Qt 4.7.4 (32 bit) which uses mingw compiler. I have few questions.
Please try to answer these :--
I have created a gui application in QT creator. With build configuration For each QT version one debug & one release now it created a build folder geny-build-desktop-Qt_4_8_1_for_Desktop_-MinGW_Qt_SDK__Debug
Inside this folder three makefiles are there :---
Makefile
Makefile.Debug
Makefile.Release
Inside this folder two folder are there :---
debug ---> contains compiled .exe file
release ---> empty
Q-1> How can I change settings in QT creator to compile my project for the release build ?
On the left toolbar of Qt Creator you will find the button to set the build for your project, set it to release.
With your settings a folder named geny-build-desktop-Qt_4_8_1_for_Desktop_-MinGW_Qt_SDK__Release will be created with a release subfolder in it, containing your compiled binary.
Anyway, it doesn't have to do with static build, it is simply a release build of your binary, still depending on Qt dynamic libraries. The difference is that the debug build is linked to the debug libraries (for example: Qt5Cored.dll), while the release build is linked to the release libraries (for example: Qt5Core.dll).
Release builds are usually optimized and don't include debug symbols, so they are also smaller.
If you think about static linking Qt to your program (i.e. getting a binary which not depends on Qt dynamic libraries) you should be aware that you should get a commercial license.
Try using the CTRL + T combination.
I'm using QtCreator and I got two projects, each with an own PRO file.
The Viewer is a normal Qt app.
The other one, let's call it Utilities, is supposed to be a static library which is used in the Viewer.
However when I compile and run the Viewer (everything compiles fine), I get an error message telling me Utilities.dll was not found.
This is the first part of the PRO-file for Utilities:
QT -= core gui
TARGET = Utilities
TEMPLATE = lib
CONFIG += staticlib warn_on $$(QMAKESPEC)
Shouldn't it work that way?
Why is it compiling the Utilities.a file fine but asking me for the DLL at runtime? It's supposed to be a static library :(
Since I did not specify some needed libraries in Utilities.pro, it apparently did not link as you'd expect for a static library.
My platform is W7-x64, latest Qt SDK and MingW 4.4 bundled with it.
Hope anyone can help :/
Thanks!
The CONFIG += staticlib request your target library to be a static lib not the entire dependency chain ... I am pretty sure your .a lib will require mingw dlls to run ...
If you want to reduce dependencies try the MSVC tool chain (Qt provide pre-compiled package for that) you will end with one or two mandatory dll dependencies. Or you can link again static mingw SDK (you will probably have to build it your-self ...).