How to set qt5 path with cmake find_package on Windows? - qt

I am adding QT like this:
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
...
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick ${OpenCV_LIBS})
but cmake finds some python artifacts instead of expected C:\QT...
How can I change this? Tried to find some examples, but didn't find any clear instruction, which subfolder of C:\QT\ I should specify (any where).

find_package search order is following:
Search in cache variables:
CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH
Search in environment variables: <package>_DIR, CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH.
Search in the HINTS option.
Search the PATH environment variable.
And in some more "desperate" places. More about that here.
With that in mind there are several ways to provide a proper version to QT:
Have an environment variable pointing to the proper version of QT (e.g. QTDIR). And use it in the CMake files:
like set(CMAKE_PREFIX_PATH "$ENV{QTDIR}")
or find_package(Qt5 HINTS "$ENV{QTDIR}" COMPONENTS Core Quick REQUIRED)
Have an environment variable explicitly named Qt5_DIR pointing to the proper version of QT. Then no additional changes to CMake files are required.
Make sure that required version of Qt is the first one to be found in the PATH environment variable, for example, for windows C:\Qt\Qt5.10.1\5.10.1\msvc2017_64

You can set click the Add Entry button in CMake Gui and add a new variable called Qt5_DIR, select its type as PATH and its value to something like C:\Qt\5.11.0\msvc2017_64\lib\cmake\Qt5 where 5.11.0 is the Qt version. This folder must contain Qt5Config.cmake that CMake needs to set things up correctly.

My workaround was to put desired QT to the top of PATH variable. It has to be in PATH if you want CMAKE to find it.

Related

How to make CMake use AUTOMOC without find_package?

Currently, I have Qt dlls and include headers as a dependency in my repo. I wanted to make use of CMake's CMAKE_AUTOMOC functionality providing a moc.exe binary along with dlls and include files. CMake allows to set a custom path to binary but it fails to use CMAKE_AUTOMOC functionality.
CMake Warning (dev) in myproj/CMakeLists.txt:
AUTOGEN: No valid Qt version found for target myproj_QT. AUTOMOC
disabled. Consider adding:
find_package(Qt<QTVERSION> COMPONENTS Core)
to your CMakeLists.txt file.
I don't want to use find_package all stuff like that. I want to make dependency management as much straightforward and explicit as it can be. Is there a way to enable automoc/autouic/autorcc features with my approach?
Adding that code helped me. Thanks #Tsyvarev for pointing out
set(PROGRAM_EXTENSION)
if (WIN32)
set(PROGRAM_EXTENSION .exe)
endif()
set(AUTOMOC_EXECUTABLE ${CMAKE_SOURCE_DIR}/3rdparty/qt5/bin/${OS}/moc${PROGRAM_EXTENSION})
add_executable(Qt5::moc IMPORTED)
set_target_properties(Qt5::moc PROPERTIES IMPORTED_LOCATION "${AUTOMOC_EXECUTABLE}")
set(Qt5Core_VERSION_MAJOR 5)
set(Qt5Core_VERSION_MINOR 15)
set(CMAKE_AUTOMOC TRUE)
Qt5CoreMacros.cmake must be included before

why windeplotqt failed:Warning: Unable to read \mkspecs\qconfig.pri?

I am developing a project in QT, when I want to use Windeployqt to wrap my exe.
error:Warning: Unable to read \mkspecs\qconfig.pri comes out
I execute Eindeployqt in "QT5.1.1 for Desktop(Mingw 4.8.3)", I checked the qconfig.pri exsited in
C:\Qt\Qt5.1.1\5.1.1\mingw48_32\mkspecs\qconfig.pri
I have added environment variable, can anyone help on this?
Usually you would do this with environment variable
QMAKESPEC=C:\SOMEPATH\qt\mkspecs\win32-g++
or in your case probably:
QMAKESPEC=C:\Qt\Qt5.1.1\5.1.1\mingw48_32\mkspecs
though normally it should be a folder below mkspecs specifying your platform (win32-g++ in my first example).
The windeploy tool will use the paths provided from qmake.
Assuming that you are in the bin folder of your qt installation.
All "important" paths can be seen with:
qmake -query
In your case you will see that all paths have the wrong location.
You have to add a qt.conf file.
https://doc.qt.io/qt-5/qt-conf.html
with the following content:
[Paths]
Prefix = ..
Now, running qmake -query should return correct paths. Also the windeploy tool should be able to find the correct paths.

How to add library paths in Qt Creator like LIBPATH in Visual Studio?

Question
a) How do we add library paths where the project should look for depended libraries in Qt Creator?
b) How are the settings in project >> Run >> Build Environment related to similar in .pro file? Does the environment variable listed there applies to .pro file as well (well they don't) so what are they exactly?
Context/Details:
Visual Studio has a various environment variables for folders where a project looks for include files, library files or executable files etc. This is rather confusing in Qt Creator and I havne't seen good documentation on it.
The only thing which is obvious is INCLUDEPATHvariable which points to the directories where to look for the include files (.h)
However how do I set the library paths, the path where it should look for dependent libraries/dlls etc? I can specify the exact library with LIBS variable in .pro file, there don't seem to equivalent of LIBPATH variable where it should look for other libraries if not found in current folder.
I have worked around this be adding library path the following way basically using LIBS variable but dropping the library file name and that seems to work and add the path but I don't see this documented anywhere.
LIBS += -L"$$_PRO_FILE_PWD_/Xerces/bin/"
But what makes things more interesting is the settings in Projects >> select 'Run' from current configuration and expand the Run Environment settings.
''
Here there is LIB variable and LIBPATH variable but there are clearly not .pro environment available. It also says here that these settings are local to user and saved in .pro.user file which perhaps suggest it's a different way to set but it doesn't say how to set them in .pro file but it does suggest to set them there if want to apply for all users!
Likewise there are DEPENDPATH AND VPATH and it is not really clearly what they are used for.
I don't have enough Rep to add comments to your question, So am adding my comment in form of answer. I am pretty new to Qt and have been developing Qt GUI application on Linux.
I set this LD_LIBRARY_PATH environment variable with the path to my Qt libraries. Am not sure how much it will be helpful to you since you are using visual studio on windows.
in Projects property go to Build Environment and add a variable with the libs path, example NAME_LIBPATH. So in the .pro file add the following:
# your lib configuration
LIBS += -L$$(NAME_LIBPATH) \
-llibname

Qt $$[...] - configuration option that were set when Qt was built

I'm looking at this Qt5 .pro file. It has the following entry:
DESTDIR = $$[QT_INSTALL_PLUGINS]/ms_plugins
I know that this means the compiled module's output (a plugin) should go into the ms_plugins subfolder in the Qt5 plugins install location. I have verified that the plugin does indeed go into that location.
My questions are:
Where is QT_INSTALL_PLUGINS defined?
On a related note, is there a similar variable that holds the
location of the build directory. Typically, something like
build_Desktop_Qt_5_2_1_clang_64bit-Debug on my Mac.
Any documentation of all available variables?
EDIT: I've found some description of these variables here, although, I still don't see where they're defined.
EDIT2: Mostly for future visitors. The documentation mentions The special $$[...] operator can be used to access various configuration options that were set when Qt was built:. So in order to figure out what QT_INSTALL_PLUGINS is we can put the following in a .pro file:
message(Plugins: $$[QT_INSTALL_PLUGINS])
QT_INSTALL_PLUGINS is one of the built-in properties of qmake. The manual of qmake in Qt 4.8 talks about qmake's built-in properties but does not mention QT_INSTALL_PLUGINS specifically. The manual of qmake in Qt 5 shows a much longer list of built-in properties including QT_INSTALL_PLUGINS. If you take a look at the source of qmake you can see that the value of a built-in property is determined by calling QLibraryInfo::location() (source, doc).
The location of the build directory can be found in a variable called OUT_PWD: OUT_PWD specifies the full path leading to the directory where qmake places the generated Makefile.
You can find the documentation of all available variables here.
QT_INSTALL_PLUGINS is a persistent property of qmake. You can print its value on the command line using
qmake -query QT_INSTALL_PLUGINS
To change this location, use qmake -set <property> <value>. Type qmake -help for more information.

How can I set where a Qt app finds a Qt module?

I would like to include libQtGui.so.4 libQtNetwork.so.4 and libQtCore.so.4 in the same directory as where my app resides. How would I make Qt understand this? y purpose is to have a standalone app that uses shared libraries
Setting the LD_LIBRARY_PATH environment variable is one option. For example:
export LD_LIBRARY_PATH=/path/to/dir/with/libs:$LD_LIBRARY_PATH
Another option is to set the RPATH of your Qt application during linking. Setting the RPATH to the value "$ORIGIN" will cause the dynamic linker to look in the same directory as your Qt application at runtime. For example, if using qmake, add the following snippet to your project file:
unix:!mac{
QMAKE_LFLAGS += -Wl,--rpath=\\\$\$ORIGIN
QMAKE_LFLAGS += -Wl,--rpath=\\\$\$ORIGIN/lib
QMAKE_LFLAGS += -Wl,--rpath=\\\$\$ORIGIN/libs
QMAKE_RPATH=
}
This will set the RPATH to "$ORIGIN:$ORIGIN/lib:$ORIGIN/libs", meaning that the dynamic linker will first look in the location of your Qt application, then in a lib subdirectory at its location, then in a libs subdirectory at its location, and finally in any system defined locations.
UNIX / Linux is going to look in LD_LIBRARY_PATH (if set) first before looking in the system standard libs. So if you set that, you can indeed override. Just like setting the PATH on Windows. Same effect. The ordering matters.
You can add ./ or . to LD_LIBRARY_PATH as well.
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
LD_LIBRARY_PATH and QMAKE_RPATH never worked for me. Instead, I set QMAKE_RPATHDIR in my .pro file. For example after having built and installed (make install) Qt, it has been placed in /usr/local/Trolltech/Qt-4.8.5/lib/. I then write the following in my .pro file:
QMAKE_RPATHDIR += /usr/local/Trolltech/Qt-4.8.5/lib/
Note 1: Relative paths seem not to work. Prefer absolute paths.
Note 2: When you then make, you can see that the following option is given to the linker: -Wl,-rpath,/usr/local/Trolltech/Qt-4.8.5/lib/
Note 3: To be sure that the binary links dynamically to the correct library, you can display the version of Qt at runtime delivered by qVersion().

Resources