Reputation: 15641
There are two versions of Qt installed on my Ubuntu - 5.2 (default) and 5.4 (in /opt/Qt/5.4/gcc_64):
CMakeLists.txt:
project(testproject)
find_package(Qt5Core HINTS /opt/Qt/5.4/gcc_64 REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main Qt5::Core)
main.cpp:
#include <QDebug>
int main()
{
qDebug() << "runtime version: " << qVersion() << " compiled with: " << QT_VERSION_STR << endl;
return 0;
}
Running the program:
cmake . && make clean && make && LD_LIBRARY_PATH=/opt/Qt/5.4/gcc_64/lib ./main
Output:
runtime version: 5.4.0 compiled with: 5.2.1
How to tell inside CMake to use Qt 5.4 instead of default Qt 5.2? I've tried several options for HINTS
in find_package
but none of them looks to work.
Upvotes: 2
Views: 4289
Reputation: 7482
I took a look through the CMake files generated by an installation of Qt5, and no where in those files are hints being ingested from the caller. These CMake files all use relative paths once one of them is picked up.
That is, if you're looking for the core library, then all of the dependencies that version of the core library will be the correct version. So the goal is to get it to pick the right CMake module when you call find_package
, and there are a couple of ways to do that using CMake level hints.
CMAKE_PREFIX_PATH
You can set the prefix path to the base directory your Qt is installed to. The base directory is the directory containing lib/
and bin/
. In your case, this might be something like this:
export CMAKE_PREFIX_PATH=/opt/Qt/5.4/gcc_64:$CMAKE_PREFIX_PATH
and then from the same shell session run your cmake commands.
Qt5Core_DIR
in your CMakeLists.txt
This requires setting a variable that points to the right CMake root module you want your Qt to be found from:
set(Qt5Core_DIR /opt/Qt/5.4/gcc_64/lib/cmake/Qt5Core)
find_package(Qt5Core REQUIRED)
Of course, the issue with this is that if you wanted to find another module, you'd have to set the specific Qt5<MODULE>_DIR
variable before your find_package
call.
Upvotes: 4