0、写在前面

本文将详细介绍如何优雅地使用 VS Code 搭建 C++ 的调试环境。对熟练者来说,VS Code 是一个界面现代化,值得一试的轻量编辑器,而对于入门者来说则可以在理解 C++ 程序编译的同时获得一个漂亮流畅的学习环境。

1、VS Code 的下载 & 安装

1.1、下载

首先在 VS Code 官网下载对应平台的安装包。

Download Visual Studio Code – Mac, Linux, Windows

VS Code 官网
VS Code 官网

可以看到,对于 Windows 平台一共有三种类型的下载方式。其中,User Installer 指的是将 VS Code 安装在用户目录(User/AppData 文件夹)内,而 System Installer 则指的是将 VS Code 安装在 ProgramFiles 内。

安装在两个位置的主要区别在于,如果一份 Windows 设置了多个登录账户,那么由 User Installer 安装的 VS Code 只有执行安装程序的账户能够使用,而使用 System Installer 安装的则可以在所有账户使用。

最后一个 .zip 不用多说,就是早些年很流行的“绿色版”。但我一直比较反感绿色版,因为绿色版的软件一般没有统一的卸载程序,会导致最后不想再使用某个软件而删除后硬盘里会留下大量由它创建的临时文件和注册表项(赛博洁癖)。当然,这也是那些老的国产破解软件的问题了,一般现在的绿色版软件在这方面都做的还算不错。

1.2、安装

若下载的不是绿色版,则直接打开 .exe 文件即可与运行安装程序。

VS Code 安装
VS Code 安装

接着的步骤都按照个人习惯处理即可。

VS Code 安装
VS Code 安装

另外在这一步,强烈推荐把 Other 内的复选框全部选上,以方便日后开发。

2、安装 C++ 编译器套件(MinGW)

2.1、下载

由于 VS Code 只是一款自带插件功能的文本编辑器,因此编译和调试需要交由其他软件完成。首先在 MinGW 官网获取最新版本 MinGW 的 Sourceforge 链接。

Downloads [mingw-w64]

MinGW 官网
MinGW 官网

2.2、安装

下载后运行 .exe 文件即可开始安装。

选择安装部件
选择安装部件

需要在选择安装部件时注意一些事项。

  1. 如果你需要进行跨平台开发,那么 Threads 最好选择 posix 。
  2. i686 特指在 pentun II 以后的 Intel 系列 CPU ,及 K7 以后等级的 CPU。而 x86_64 中的 x 则代表未指定,即 i686 也同样包括在 x86_64 内。
选择安装路径
选择安装路径

在选择安装路径时同样需要注意,路径中最好不要包括中文字符。

2.3、配置环境变量

安装完成后,则开始配置环境变量。环境变量起到了类似 Linux 固定 lib 路径的功能,能让 IDE(此时是 VS Code & 插件)方便的找到工具链的位置而不需要每次手动指定。

按下 Win+R 打开运行窗口,并输入 SystemPropertiesAdvanced.exe 后回车打开系统属性。

运行窗口
运行窗口

接着在弹出的窗口中点击右下角的环境变量。

系统属性窗口
系统属性窗口

然后在弹出的窗口中双击 Path

环境变量窗口
环境变量窗口

然后点击新建,把刚刚 MinGW 的安装路径下的 bin 文件夹填入。

编辑环境变量窗口
编辑环境变量窗口

最后点击3次完成,关闭所有系统属性窗口,重启电脑。

重启完成后,按下 Win+R 输入 cmd 打开命令提示符,接着输入 gcc -v 检查安装是否成功。

命令提示符
命令提示符

3、配置 VS Code

3.1、安装插件

首先打开 VS Code ,并在界面中选择右侧的“拓展”菜单(图标看起来是三个粘连的方块和一个漂浮的方块,或按下 Ctrl+Shift+X ),并搜索 C/C++ Extension Pack ,点击安装。(或者直接点击 C/C++ Extension Pack – Visual Studio Marketplace

安装 C/C++ Extension Pack
安装 C/C++ Extension Pack

安装完成后,最好重启一次 VS Code 来确保插件生效。(Windows style 做法)

3.2、创建工程

对于 VS Code 来说,所有的工作都必须在文件夹或工作区(带有特殊文件的文件夹)内进行。

因此,我们首先需要在容易找到的位置(例如桌面)新建一个文件夹,并双击进入它。接着在文件夹内右键,选择“使用 Code 打开”。(若没有这个选项,那么则需要手动打开 VS Code 并在右上角 File 中选择 Open Folder 选择文件夹)

打开文件夹
打开文件夹

然后在 VS Code 的文件浏览器(Ctrl+Shift+E)中点击左侧的加号按钮新建一个 test.cpp 文件。

新建 test.cpp
新建 test.cpp

接着向 test.cpp 中填入示例代码,然后按下 Ctrl+S 保存。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
   vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

   for (const string& word : msg)
   {
      cout << word << " ";
   }
   cout << endl;
}

至此,一个简易的示例工作文件夹就创建完成了。

3.3、配置编译器

接下来为 VS Code 创建 tasks.json 文件来告诉 VS Code 如何编译程序。tasks.json 会引导 VS Code 去调用 g++ 编译器来生成对应源代码的可执行文件。

依次点击 Terminal -> Configure Default Build Task,在弹出的菜单里选择带有 g++ 字样的选项。

g++
g++

可以看到,VS Code 在工作目录下新建了一个 .vscode 文件夹和一个 tasks.json 文件。

接下来开始编译 test.cpp 。

  1. 返回 test.cpp ,按下 Ctrl+Shift+B 开始编译。(tasks.json 默认指定编译活动的文件)
  2. 编译开始后,VS Code 下方的 Terminal 会开始显示 g++ 输出。如果编译成功,最后的输出结果应该包含 Build finished successfully. 字样。
  3. 点击 Terminal 右端的加号,新建一个 PowerShell ,接着在 PowerShell 中执行 ./test.exe 即可查看输出
Terminal
Terminal

编译器的配置到这里便完成了。但需要注意的是,如果工程中包含多个 .cpp 文件需要编译,那么需要将 tasks.json 内的 ${file} 参数替换为 “${workspaceFolder}/*.cpp”。同时,如果希望自定义生成的可执行文件的名称,可以通过改动 “${fileDirname}/${fileBasenameNoExtension}” 参数实现。

3.4、配置调试器

只有编译对于开发来说是远远不够的。因此,接下来我们创建 launch.json 来使得 VS Code 能够在按下 F5 后调用 GDB 调试器来调试程序。依次点击 Run -> Add Configuration ,然后在下拉菜单中选择带有 (GDB/LLDB)的选项。

Add Configuration
Add Configuration

可以看到,VS Code 在工作目录下新建了一个 launch.json 文件。 需要注意的是,如果在 tasks.json 中修改过生成可执行文件的名称,那么在 launch.json 的 program 参数中也要做出相同的改动。

接下来开始调试 test.cpp。

  1. 返回 test.cpp ,按下 F5 开始调试。

现在,VS Code 已经支持断点,单步调试以及监视变量的功能。

3.5、自定义 C/C++ 拓展

如果需要控制 C/C++ 拓展的其他部分(例如 编译器路径,头文件路径,C++ 标准等等),则需要创建 c_cpp_properties.json 文件。

按下 Ctrl+Shift+P ,输入 C/C++ ,选择 C/C++: Edit Configurations (JSON)

Edit Configurations (JSON)
Edit Configurations (JSON)

可以看到,VS Code 在工作目录下新建了一个 _cpp_properties.json 文件。

-1、完全重新安装 VS Code

有时因为一些误操作,将 VS Code 的配置文件搞得一团糟,也因为没有什么重要的业务所以不想潜下心一个一个慢慢修复的话,可以参考以下步骤。因为如果只是单纯的卸载 VS Code ,重新安装后它还是会很贴心地帮你重新加载之前那一团糟的配置文件。(但如果登陆了账户并开启同步的话,就要在新安装登录后选择使用本地配置文件覆盖云配置文件)

下面目录使用了 Windows 变量,可以直接复制黏贴在文件资源管理器的地址栏。

  1. 删除 %HOMEPATH%\.vscode
  2. 删除 %APPDATA%\Code

完成后,再安装 VS Code 便相当于全新安装。

128 个评论

  1. I was curious if you ever considered changing
    the page layout of your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content
    so people could connect with it better. Youve got an awful lot of text for only having 1
    or 2 images. Maybe you could space it out better?

    Mia
  2. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to
    my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

  3. Hey I know this is off topic but I was wondering if you knew of any widgets I could
    add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe
    you would have some experience with something like this.
    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward
    to your new updates.

    Visit my website; http://www.fotosombra.com.br

  4. It’s appropriate time to make some plans for the future and it’s time
    to be happy. I have read this publish and if I may I wish to counsel you few attention-grabbing issues or
    suggestions. Perhaps you could write subsequent articles relating to this article.

    I wish to learn even more issues about it!

    Also visit my blog post :: ketogenic diets

  5. Hiya, I am really glad I’ve found this information. Nowadays bloggers
    publish just about gossips and net and this is actually irritating.
    A good website with exciting content, this is what I need.
    Thank you for keeping this website, I will be visiting it.
    Do you do newsletters? Can’t find it.

    Also visit my web blog :: yeast infection

  6. I enjoy you because of your entire efforts on this website.
    My niece take interest in participating in investigations and it is easy to see why.
    My spouse and i notice all of the dynamic medium you
    render powerful guidelines through your
    website and as well as welcome participation from some others about this
    matter plus my daughter is actually learning a great deal.
    Have fun with the remaining portion of the year. You have been performing a very
    good job.

    Also visit my homepage :: facial skin care

  7. Oh my goodness! Amazing article dude! Many thanks, However I am having troubles with your RSS.
    I don’t understand the reason why I cannot join it.
    Is there anyone else getting identical RSS problems? Anyone who knows the
    solution will you kindly respond? Thanks!!

  8. I want to point out my affection for your kindness in support
    of individuals who actually need help with this theme. Your very own commitment to passing the solution along was especially functional and has
    frequently allowed many people just like me to get to their targets.
    Your entire useful information implies a great
    deal a person like me and far more to my mates.
    With thanks; from each one of us.

    Here is my homepage – diet pill

  9. Hi, I think your website could be having internet browser compatibility issues.
    Whenever I take a look at your blog in Safari, it looks fine however when opening in IE, it’s got some overlapping issues.
    I just wanted to give you a quick heads up! Besides that,
    excellent website!

    Here is my page 23.95.102.216

  10. First of all I want to say excellent blog! I had a quick question which I’d like to ask if you do not mind.
    I was curious to know how you center yourself and clear your thoughts
    prior to writing. I’ve had trouble clearing my thoughts in getting my
    thoughts out. I truly do take pleasure in writing but it just
    seems like the first 10 to 15 minutes are generally lost just trying to
    figure out how to begin. Any recommendations or tips?
    Appreciate it!

    My web site … http://www.comptine.biz/

  11. Howdy! I could have sworn I’ve visited this site
    before but after looking at many of the posts I realized it’s new to me.

    Anyhow, I’m definitely delighted I found it and
    I’ll be book-marking it and checking back frequently!

    Feel free to surf to my page :: Amelie

  12. I precisely needed to say thanks yet again. I do not know
    the things that I might have undertaken without the pointers shown by you
    about this field. It had been a fearsome problem
    in my opinion, nevertheless noticing this professional mode you resolved that made
    me to jump with joy. Now i am grateful for the information as well as believe you find out what an amazing weight loss
    job you are always getting into educating others through
    a site. More than likely you haven’t met all of us.

  13. I think this is among the most important information for me.
    And i’m glad reading your article. But wanna observation on some normal
    things, The web site taste is wonderful, the articles is truly great : D.
    Excellent job, cheers

    my web blog – Tessa

  14. Have you ever thought about including a little bit more than just your articles?
    I mean, what you say is important and everything. However think of if you added some great graphics or videos to give
    your posts more, “pop”! Your content is excellent but with pics and videos, this website could undeniably be one
    of the best in its field. Terrific blog!

  15. With havin so much written content do you ever run into any problems of plagorism or copyright infringement?

    My blog has a lot of exclusive content I’ve either authored myself or outsourced but
    it seems a lot of it is popping it up all over the internet without my authorization. Do you know any
    solutions to help reduce content from being ripped off?
    I’d certainly appreciate it.

    Feel free to surf to my web page: free fire unlimited diamond generator

  16. Hello there, just became aware of your blog through Google, and found that it is really informative.

    I am going to watch out for brussels. I?ll appreciate if you continue this in future.
    Numerous people will be benefited from your writing.
    Cheers!

    Also visit my web site; drug use

  17. Right here is the perfect webpage for anyone who wants to
    find out about this topic. You understand a whole lot its almost hard to argue with you (not that I
    personally would want to?HaHa). You definitely put a fresh spin on a topic that’s been discussed for many years.
    Wonderful stuff, just great!

    Here is my homepage; boost libido

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注