-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
177 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
************************************************************************************************************************ | ||
4.添加你自己的程序 | ||
************************************************************************************************************************ | ||
|
||
你只需要在 ``CMakeLists.txt`` 中用 ``add_code(程序名 源文件1 源文件2...)`` 添加即可, 具体参考 ``请读我`` 文件. | ||
|
||
在 ``src`` 文件夹中我给了两个程序示例, 请对照 ``CMakeLists.txt`` 中是怎么添加它们的: | ||
|
||
- ``example_single``: 单个源文件组成的程序. | ||
- ``example_multiple``: 多个源文件组成的程序. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
************************************************************************************************************************ | ||
调试程序 | ||
************************************************************************************************************************ | ||
|
||
======================================================================================================================== | ||
选择运行任务 | ||
======================================================================================================================== | ||
|
||
VSCode 只是通用的编辑器, 并不知道你会如何进行调试或运行, 所以需要在 ``.vscode/launch.json`` 中手动配置运行任务. 教程中已经对此进行配置, 以下展示如何使用. | ||
|
||
从左侧侧边栏切换到 :menuselection:`运行和调试`, 点击上方 :menuselection:`▶播放按钮` 右侧下拉框, 则如图所示: | ||
|
||
.. figure:: 调试和运行侧边栏.png | ||
|
||
图中展示了三个我已配置好的运行任务, 你可以点击 :menuselection:`▶播放按钮` 或按快捷键 :KBD:`F5` 运行选择的任务: | ||
|
||
:menuselection:`LLDB: debug the active target` | ||
使用 LLDB 软件对 CMake *当前预运行的目标* 进行调试, 在此即对选择的 C++ 程序进行调试. | ||
|
||
:menuselection:`CMake: debug configuration` | ||
对 CMake 的配置即 ``CMakeLists.txt`` 进行调试. | ||
|
||
:menuselection:`CMake: clean and debug configuration` | ||
清理之前的 CMake 配置缓存, 并对 CMake 的配置即 ``CMakeLists.txt`` 进行调试. | ||
|
||
======================================================================================================================== | ||
调试 test | ||
======================================================================================================================== | ||
|
||
根据上面的描述, 要调试 test, 我们将 *当前预生成的目标* 和 *当前预运行的目标* 均设置为 :menuselection:`[test]`, 点击 :menuselection:`运行和调试 --> ▶播放按钮` 或按快捷键 :KBD:`F5` 即可. | ||
|
||
当然, 在调试前, 我们需要在 test 程序对应的源文件 :cpp:`test/main.cpp` 中设置断点: | ||
|
||
1. 点开 ``test/main.cpp``. | ||
2. 选择要中断的行, 通过 :KBD:`左击` 行号左边空白区域设置断点, 例如设置在第 7 行 :cpp:`int x{};` 处. | ||
|
||
.. figure:: 设置断点.png | ||
|
||
设置断点 | ||
|
||
.. hint:: | ||
|
||
更多调试相关内容见于 :ref:`断点调试的使用 <question_board:debugger>`. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
************************************************************************************************************************ | ||
观察诊断信息 | ||
************************************************************************************************************************ | ||
|
||
观察 ``int main()`` 函数内的 ``int x{};``, ``x`` 下方应有黄线. | ||
|
||
- 黄色的信息为警告, 表示代码写得可能不够好, 如何处理见于 :doc:`/appendix/about_warnings/main`. | ||
- 红色的信息为错误, 表示代码无法运行. | ||
|
||
.. figure:: 诊断信息示例.png | ||
|
||
诊断信息 |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
************************************************************************************************************************ | ||
3.检验配置结果和实际使用 | ||
************************************************************************************************************************ | ||
|
||
打开 ``test/main.cpp``, 以下操作均在 ``test/main.cpp`` 中进行. | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
:caption: 目录 | ||
|
||
diagnostics/main.rst | ||
run/main.rst | ||
debug/main.rst |
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
************************************************************************************************************************ | ||
运行程序 | ||
************************************************************************************************************************ | ||
|
||
======================================================================================================================== | ||
CMake 状态栏 | ||
======================================================================================================================== | ||
|
||
如果配置成功, 在 VSCode 下方状态栏将会显示类似这样的一行: | ||
|
||
.. figure:: cmake工具列表.png | ||
|
||
此处我们只关注从 :menuselection:`ⓘ[Debug]` 起的右边部分. | ||
|
||
------------------------------------------------------------------------------------------------------------------------ | ||
构建相关 | ||
------------------------------------------------------------------------------------------------------------------------ | ||
|
||
:menuselection:`ⓘ[Debug]` | ||
当前所选择的构建配置, 指示按什么配置来生成编译程序, 点击可以切换. | ||
|
||
.. admonition:: 不同的构建配置介绍 | ||
:class: dropdown | ||
|
||
Debug | ||
调试版本, 一般由程序员用于调试. 它不对代码做任何优化并且会记录代码与程序间的对应关系 (称为调试信息), 从而允许实际地逐行执行代码. | ||
|
||
Release | ||
发行版本, 即最终发布给用户的版本. 基于 as-if 规则, 它只需要保证程序运行起来就像代码看起来那样 (as-is), 因而可以对代码进行尽可能的优化. | ||
|
||
需要注意的是, 针对程序中的有符号数溢出、下标越界等未定义行为, 它怎么优化都可以, 因而即使炸掉你的电脑也是可能的, 所以不要让代码中有未定义行为. | ||
|
||
RelWithDebInfo | ||
具有调试信息的发行版本. 由于未定义行为等的存在, 我们可能遇到调试版本能正常运行, 但发行版本出现问题的情况, 此构建配置即用于调试这种情况. | ||
|
||
由于发行版本对代码进行优化, 调试过程中可能出现一下跳过好几行的情况, 这是正常的. | ||
|
||
MinSizeRel | ||
以最小大小为首要目标进行优化的发行版本. | ||
|
||
**喜欢谈优化的新手请先知道有不同的构建配置; 抱怨程序慢的人请先切换到 Release 构建配置.** | ||
|
||
:menuselection:`⚒[Clang 17.0.6 arm64-...]` | ||
当前选择的工具包或配置档案, 指示用什么软件来生成编译程序, 此处即 :doc:`/configure/main` 中选择的编译器 Clang. | ||
|
||
------------------------------------------------------------------------------------------------------------------------ | ||
编译生成 | ||
------------------------------------------------------------------------------------------------------------------------ | ||
|
||
:menuselection:`⚙生成` | ||
点击即可对 *当前预生成的目标* 进行编译生成. | ||
|
||
:menuselection:`[all]` | ||
*当前预生成的目标*. 点击可以切换, :menuselection:`[all]` 表示编译生成所有目标. | ||
|
||
.. figure:: cmake选择生成目标.png | ||
|
||
点击后弹出的选择窗口, 展示目前项目中所有可用生成目标. | ||
|
||
------------------------------------------------------------------------------------------------------------------------ | ||
运行 | ||
------------------------------------------------------------------------------------------------------------------------ | ||
|
||
:menuselection:`▶播放按钮` | ||
点击即可对 *当前预运行的目标* 进行运行, 也可以用快捷键 :KBD:`Shift`:KBD:`F5` 进行运行. | ||
|
||
:menuselection:`[test]` | ||
*当前预运行的目标*, 点击可以切换. 在运行前会先对 *当前预生成的目标* 和 *当前预运行的目标* 进行生成. | ||
|
||
.. warning:: | ||
|
||
该按钮初次配置时可能不会显示, 点击一次 :menuselection:`▶播放按钮`, 在弹出的选项中随便选一个作为 *当前预运行的目标* 即可. | ||
|
||
======================================================================================================================== | ||
运行 test | ||
======================================================================================================================== | ||
|
||
根据上面的描述, 要运行 test, 我们将 *当前预生成的目标* 和 *当前预运行的目标* 均设置为 :menuselection:`[test]`, 点击 :menuselection:`▶播放按钮` 或按快捷键 :KBD:`Shift`:KBD:`F5` 即可. | ||
|
||
.. hint:: | ||
|
||
CMake Tools 扩展除状态栏提供的功能外, 还提供了非常多的功能, 建议通过 `VSCode CMake 拓展用户手册`_ 学习. |