这里写了一个基于Qt5的控制台的小代码:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char** argv)
{
QCoreApplication a(argc, argv);
qDebug() << "Application started";
int testValue = 42;
qDebug() << "Test value:" << testValue;
return a.exec();
}
工程使用了CMake工程:
cmake_minimum_required(VERSION 3.15)
project(WSLwithVS)
set(CMAKE_AUTOMOC ON)
find_package(QT NAMES Qt5 Qt6)
find_package(Qt${QT_VERSION_MAJOR} CONFIG COMPONENTS Core REQUIRED)
add_executable(WSLwithVS main.cpp)
target_link_libraries(WSLwithVS Qt${QT_VERSION_MAJOR}::Core)
确保WSL里面有编译器、Ninja、gdb、qtbase5-dev和常用的中文字体fonts-wqy-zenhei、fonts-wqy-microhei
sudo apt install gcc g++ ninja-build cmake gdb qtbase5-dev fonts-wqy-zenhei fonts-wqy-microhei
开始编译调试
-
右键点击
CMakeLists.txt
文件,选择配置"WSLwithVS"我这里遇到一点小问题在
1> CMake 生成完毕
列出现了错误,我暂时的解决方法:cd ~/.vs/WSLwithVS ninja -C out/build/
-
在使用ninjia生成完毕后重复步骤1,输出结果如下
1> 已为配置“Qt-Debug”启动 CMake 生成。 1> 在 /usr/bin/cmake 上找到 cmake 可执行文件。 1> /usr/bin/cmake -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_CXX_FLAGS:STRING="-DQT_QML_DEBUG" /home/lxy/.vs/WSLwithVS/CMakeLists.txt; 1> [CMake] -- Configuring done 1> [CMake] -- Generating done 1> [CMake] -- Build files have been written to: /home/lxy/.vs/WSLwithVS/out/build 1> [CMake] 1> 已提取 CMake 变量。 1> 已提取源文件和标头。 1> 已提取代码模型。 1> 已提取包含路径。 1> CMake 生成完毕。
-
设置断点、启动调试
调试控制台信息:=thread-group-added,id="i1" GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word". Warning: Debuggee TargetArchitecture not detected, assuming x86_64. =cmd-param-changed,param="pagination",value="off" [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Breakpoint 1, main (argc=32767, argv=0x1) at ../../main.cpp:5 5 { Loaded '/lib64/ld-linux-x86-64.so.2'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libQt5Core.so.5'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libstdc++.so.6'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libgcc_s.so.1'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libc.so.6'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libpthread.so.0'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libz.so.1'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libicui18n.so.66'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libicuuc.so.66'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libdl.so.2'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libpcre2-16.so.0'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libdouble-conversion.so.3'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libglib-2.0.so.0'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libm.so.6'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libicudata.so.66'. Symbols loaded. Loaded '/lib/x86_64-linux-gnu/libpcre.so.3'. Symbols loaded. Breakpoint 2, main (argc=1, argv=0x7fffffffe508) at ../../main.cpp:8 8 qDebug() << "Application started";