根据 doc.qt.io,像
qInfo
和 qDebug
这样的函数需要包含 <QtLogging>
。与大多数文档页面不同,没有 CMake/qmake 的详细说明;根据此页面所在的类别,我假设此标头是 Qt Core 的一部分。
但是,我的编译器无法使用基本的 CMake 设置找到此特定标头。作为 Qt 初学者,我是否遗漏了什么,或者文档是否有误?
# On Debian 12
apt install g++ cmake qt6-base-dev
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(qtlogging_test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 REQUIRED COMPONENTS Core)
qt_standard_project_setup()
qt_add_executable(qtlogging_test main.cpp)
target_link_libraries(qtlogging_test PRIVATE Qt6::Core)
main.cpp:
#include <QObject> // Check for broken environment
#include <QtLogging>
int main() {
(void) (qInfo());
return 0;
}
$ cmake -B build && cmake --build build
-- The CXX compiler identification is GNU 12.2.0
[...]
-- Found Threads: TRUE
[...]
-- Found WrapAtomic: TRUE
[...]
-- Build files have been written to: /tmp/fj/build
[ 25%] Automatic MOC and UIC for target qtlogging_test
[ 25%] Built target qtlogging_test_autogen
[ 50%] Building CXX object CMakeFiles/qtlogging_test.dir/qtlogging_test_autogen/mocs_compilation.cpp.o
[ 75%] Building CXX object CMakeFiles/qtlogging_test.dir/main.cpp.o
/tmp/fj/main.cpp:2:10: fatal error: QtLogging: No such file or directory
2 | #include <QtLogging>
| ^~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/qtlogging_test.dir/build.make:90: CMakeFiles/qtlogging_test.dir/main.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/qtlogging_test.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
这是官方 Debian 12 dpkg 软件包特有的问题。
<QtLogging>
是在 Qt 6.4.2 和 Qt 6.5 之间引入的最新添加项,Debian 12 附带 Qt 6.4.2。