FINISHEDC++
这是第一篇学习C++的笔记,在进入C++学习之前,需要先解决开发环境的问题。
这里采用以下配置进行C++的开发:
- VSCode IDE with C++ plug-in
- Bazel
- clang-format
0. 环境准备
安装VSCode [必须]
准备 .clang-format文件 clang-format -style=google -dump-config >.clang-format [可选]
准备 .gitignore 文件 [可选]
准备 .gitmessage文件 [可选]
1. VSCode环境基本配置
选择插件C/C++,然后写一个简单的demo
#include <iostream>int main(){int a = 1;std::cout << a << std::endl;return 0;}
要进行C++代码的调试,在Run > Start Debugging,或F5,选择C++(GDB/LLDB)
在生成的launch.json右下角点击Add Configuration
VSCode提供了两种调试方式: Launch 和 Attach。
- 以launch方式启动的时候,vscode直接从本地项目启动并自动的为我们自动添加一个调试器,这也是为什么我们能在launch模式下打断点的原因。这是我们最经常用的。
- 而想以attach方式启动,通常需要一个已经在跑的项目,比如一个web服务,这个服务很可能是在远程或者在本地但是重启速度很慢的那种项目,因为这个web服务是在远程的我们没法打断点,但是可以通过attach启动的方式在这个远程的服务外面包裹一层调试器,达到调试的目的。
这里选择GCC GDB Launch方式,要想在Launch模式下调试,我们需要先对代码进行编译。
2. Bazel编译
这里采用Bazel进行编译,写一个简单的BUILD.bazel文件。
cc_binary(name = "hello",visibility = ["//visibility:public"],)
bazel build -c dpg //src:hello
NOTE: 在build过程中开启debugging的build模式,具体详见bazel help build查看。
编译完成后,项目路径如下
在bazel-bin/src/路径下,有生成的可执行文件hello。
3. 配置launch.json
在launch.json中,将”program”的路径改为上面生成的hello文件的路径
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "${workspaceFolder}/bazel-bin/src/hello","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}
这个launch.json文件中需要配置的内容后面用到的时候再说。
4. 调试
在VSCode中设置断点,F5启动调试即可
Demo文件
Dev_Env.zip
