c++ - Qt5_DIR variable disappears from CMake cache when invoked on command line - Stack Overflow

admin2025-04-25  3

I have a large CMake build using the Qt library. When I invoke this build on the command line or within VSCode, I found that the Qt5_DIR variable is not set in the cmake cache directly after configuring the project.

For example, after using the following command the Qt5_DIR is missing from the cache:

cmake -DQt5_DIR=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5 -S .. -B .

This causes a problem if I later try to change a variable using the CMake GUI because I get an error about Qt5_DIR not being set. If I do set it explicitly in the CMake GUI, then the variable persists until I execute the command line version (or build within VSCode) again.

Is there a way I can get this variable to persist when cmake is invoked on the command line?

*Tested using CMake 3.24.3 and 3.31.4.


Update: I reproduced this problem in a very simple project

  1. Create CMakeLists.txt and fill it with the following code:

    cmake_minimum_required(VERSION 3.24)

     project(testprog)
    
     variable_watch("Qt5_DIR")
    
     if(POLICY CMP0020)
       cmake_policy(SET CMP0020 NEW)
     endif()
     if(POLICY CMP0043)
       cmake_policy(SET CMP0043 NEW)
     endif()
    
     #necessary for QT stuff
     set(CMAKE_INCLUDE_CURRENT_DIR ON)
     set(CMAKE_AUTOMOC ON)
     set(CMAKE_AUTORCC ON)
    
    
     find_package(Qt5 COMPONENTS Widgets Core SerialPort QML Quick LinguistTools Sql REQUIRED)
    
     #set qt5 include dirs as 'system' dirs
     include_directories(SYSTEM ${Qt5Widgets_INCLUDE_DIRS})
     include_directories(.)
    
     qt5_wrap_ui(UI_HEADERS
             mainwindow.ui)
    
    
     SET(PROG_SOURCES
         mainwindow.cpp
         main.cpp
         ${UI_HEADERS}
     )
    
    
     add_executable(testprog WIN32 ${PROG_SOURCES})
     qt5_use_modules(testprog Widgets Core SerialPort QuickWidgets Sql)
    
  2. Create the following files in the folder with CMakeLists.txt (they can be empty): main.cpp mainwindow.cpp mainwindow.ui

  3. Create a sub-folder called 'build' and change directory to there.

  4. On the command line from within 'build' run the following cmake command:

    cmake "-DCMAKE_CONFIGURATION_TYPES=Debug;Release;MinSizeRel;RelWithDebInfo" -DQt5_DIR=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5 -G "Visual Studio 17 2022" -A x64 ..

Which produces the following output:

-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.41.34123.0
-- The CXX compiler identification is MSVC 19.41.34123.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Debug Log at CMakeLists.txt:21 (find_package):
  Variable "Qt5_DIR" was accessed using READ_ACCESS with value
  "C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5".


CMake Debug Log at CMakeLists.txt:21 (find_package):
  Variable "Qt5_DIR" was accessed using READ_ACCESS with value
  "C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5".


-- Configuring done (4.0s)
-- Generating done (0.2s)
-- Build files have been written to: C:/Users/Documents/git/testcmakeqt/build
  1. Observe cache contents

    cmake -L

which shows that Qt5_DIR is missing:

-- Cache values
CMAKE_INSTALL_PREFIX:PATH=C:/Program Files/testprog
Qt5Core_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Core
Qt5Gui_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Gui
Qt5LinguistTools_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5LinguistTools
Qt5Network_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Network
Qt5QML_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Qml
Qt5Qml_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Qml
Qt5QuickWidgets_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5QuickWidgets
Qt5Quick_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Quick
Qt5SerialPort_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5SerialPort
Qt5Sql_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Sql
Qt5Widgets_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Widgets
  1. Configure the project using the CMake GUI instead and observe that Qt5_DIR is still present after specifying it there.

    cmake -L

output:

-- Cache values
CMAKE_BACKWARDS_COMPATIBILITY:STRING=2.4
CMAKE_CONFIGURATION_TYPES:STRING=Debug;Release;MinSizeRel;RelWithDebInfo
CMAKE_INSTALL_PREFIX:PATH=C:/Program Files/Project
EXECUTABLE_OUTPUT_PATH:PATH=
LIBRARY_OUTPUT_PATH:PATH=
Qt5Core_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Core
Qt5Gui_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Gui
Qt5LinguistTools_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5LinguistTools
Qt5Network_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Network
Qt5QML_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Qml
Qt5Qml_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Qml
Qt5QuickWidgets_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5QuickWidgets
Qt5Quick_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Quick
Qt5SerialPort_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5SerialPort
Qt5Sql_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Sql
Qt5Widgets_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5Widgets
Qt5_DIR:PATH=C:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5
转载请注明原文地址:http://www.anycun.com/QandA/1745562791a90919.html