Search code examples
qtcmakecmake-gui

CMake: Set user-defined path for library


I'm using Windows and I want to create a CMakeLists.txt that allows me to build a Qt application.

I've installed Qt in a folder that's not present in PATH enviornment variable and I want to specify the folder when I invoke CMake.

I want to create a variable that can be set when CMake is invoked from both command line and gui, when I set the Qt library path. With this I can run CMake and compile project with Qt stored in any folder. How can I do it?

This is my CMakeFile:

cmake_minimum_required (VERSION 3.0)

project (myproject)

set (project_name myproject)
set (project_major_version 0)
set (project_minor_version 1)
set (project_fix_version 0)
set (project_version ${project_major_version}.${project_minor_version}.${project_fix_version})
set (output_dir "build")

# Source files
include_directories (${cmake_current_source_dir})
file (GLOB project_src "*.cpp")

# Project settings
set (cmake_automoc on)
find_package (qt5widgets)
add_executable (myproject ${project_src})
target_link_libraries (${project_name} qt5::widgets)

Solution

  • Qt5 Documentation states:

    In order for find_package to be successful, Qt 5 must be found below the CMAKE_PREFIX_PATH, or the Qt5<Module>_DIR must be set in the CMake cache to the location of the Qt5WidgetsConfig.cmake file. The easiest way to use CMake is to set the CMAKE_PREFIX_PATH environment variable to the install prefix of Qt 5.