PS: This was published on my Blog here.

PS:这已发布在我的Blog 此处 。

C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.

C ++是一种静态类型的,自由形式的(通常)已编译的,多范例的,中级通用的中级编程语言。

In simple terms, C++ is a sophisticated, efficient, general-purpose programming language based on C.

简而言之,C ++是基于C的复杂,高效,通用的编程语言。

It was developed by Bjarne Stroustrup in 1979.

它是由Bjarne Stroustrup于1979年开发的。

One of C++'s main features is the compiler. This is used to compile and run C++ code.

编译器是C ++的主要功能之一。 这用于编译和运行C ++代码。

A compiler is a special program that processes statements written in a particular programming language like C++ and turns them into machine language or "code" that a computer's processor uses.

编译器是一种特殊程序,用于处理以特定编程语言(如C ++)编写的语句,并将其转换为计算机处理器使用的机器语言或“代码”。

I actually wrote this article because I had a C++ assignment which required using a compiler. As usual, everyone was using the CodeBlocks IDE and Visual Studio IDE. But I was already used to Visual Studio Code for all my programming stuff.

我之所以写这篇文章,是因为我有一个C ++任务,需要使用编译器。 像往常一样,每个人都在使用CodeBlocks IDE和Visual Studio IDE 。 但是我已经习惯了Visual Studio Code来编写所有编程内容。

I then set out to find a way of compiling C++ directly inside my own VsCode Editor, hence this article :).

然后,我开始寻找在我自己的VsCode编辑器中直接编译C ++的方法,因此本文:)。

In this article, I'll show you how to set up your compiler in VsCode and give you some links to some of the best C++ resources.

在本文中,我将向您展示如何在VsCode中设置编译器,并为您提供一些最佳C ++资源的链接。

先决条件 (Prerequisites)

  • Prior knowledge of C++

    C ++的先验知识

    (I assume you're learning C++, about to start learning, or just reading this for fun. This article is not a C++ 101 tutorial – some understanding of C++ is needed.)

    (我假设您正在学习C ++,即将开始学习,或者只是为了好玩而阅读。这篇文章不是C ++ 101教程,需要对C ++有一些了解。)

  • Visual Studio Code Editor

    Visual Studio代码编辑器

    Download

    下载

    here and read the setup docs for Windows, Linux and Mac

    在这里并阅读Windows , Linux和Mac的安装文档

  • Internet connection (!important)

    互联网连接(!重要)

免责声明! (Disclaimer!)

I will be using a Windows OS throughout this article, but I'll provide links to resources that will help those using other operating systems.

在整篇文章中,我将使用Windows操作系统,但是我将提供指向资源的链接,这些资源将帮助那些使用其他操作系统的人。

Now let's get started!

现在开始吧!

下载并安装C ++编译器 (Download and install a C++ compiler)

  • Head to www.mingw.org and click the “Download/Installer” link to download the MinGW setup file, or click here for Windows, here for Linux, and here for Mac

    团长www.mingw.org并点击“下载/安装”链接下载MinGW的安装文件,或点击这里为Windows, 此为Linux,并在这里为Mac

MinGW, a contraction of "Minimalist GNU for Windows", is a minimalist development environment for native Microsoft Windows applications.

MinGW是Windows的Minimallist GNU的缩写,是用于本机Microsoft Windows应用程序的简约开发环境。

  • After downloading, install MinGW and wait for the “MinGW Installation Manager” to show up.下载后,安装MinGW并等待“ MinGW Installation Manager”出现。
  • When the “MinGW Installation Manager” shows up, click on mingw32-gcc-g++ then select “Mark for Installation”

    当出现“ MinGW Installation Manager”时,单击mingw32-gcc-g++然后选择“标记为安装”

  • In the menu at the top left corner, click on “Installation  > Apply Changes”在左上角的菜单中,单击“安装>应用更改”
  • Wait and allow to install completely. Ensure you have a stable internet connection during this process.等待并允许完全安装。 在此过程中,请确保您具有稳定的Internet连接。

编辑PATH环境变量以包含C ++编译器所在的目录 (Edit your PATH environment variable to include the directory where the C++ compiler is located)

PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting. - Wikipedia

PATH是类Unix操作系统,DOS,OS / 2和Microsoft Windows上的环境变量,它指定可执行程序所在的一组目录。 通常,每个执行进程或用户会话都有其自己的PATH设置。 - 维基百科

After installing MinGW, it can be found in  C:\MinGW\bin. Now you have to include this directory in your environment variable PATH. If you've been using computers for a while now you should know how to do this already, but if you don't, here are a few resources:

安装MinGW后,可以在C:\MinGW\bin找到它。 现在,您必须将此目录包括在环境变量PATH中。 如果您已经使用计算机一段时间,那么您应该已经知道该怎么做了,但是如果您不使用计算机,那么这里有一些资源:

  • Click here for a Windows OS guide

    单击此处获取Windows操作系统指南

  • Click here for Linux

    单击此处获取Linux

  • Click here for a Mac OS guide

    单击此处查看Mac OS指南

在VS Code中安装Code Runner扩展 (Install Code Runner extension in VS Code)

Now we have our compiler set up, let's install Code Runner

现在我们已经设置好编译器,让我们安装Code Runner

Code Runner allows you to Run code snippet or code file for multiple languages:

使用代码运行器,您可以运行多种语言的代码段或代码文件:

C, C++, Java, JavaScript, PHP, Python, Perl, Perl 6, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F# Script, F# (.NET Core), C# Script, C# (.NET Core), VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml Script, R, AppleScript, Elixir, Visual Basic .NET, Clojure, Haxe, Objective-C, Rust, Racket, AutoHotkey, AutoIt, Kotlin, Dart, Free Pascal, Haskell, Nim, D, Lisp, Kit, and custom command.

C,C ++,Java,JavaScript,PHP,Python,Perl,Perl 6,Ruby,Go,Lua,Groovy,PowerShell,BAT / CMD,BASH / SH,F#脚本,F#(.NET Core),C#脚本,C#( .NET Core),VBScript,TypeScript,CoffeeScript,Scala,Swift,Julia,Crystal,OCaml脚本,R,AppleScript,Elixir,Visual Basic .NET,Clojure,Haxe,Objective-C,Rust,Racket,AutoHotkey,AutoIt,Kotlin ,Dart,Free Pascal,Haskell,Nim,D,Lisp,Kit和自定义命令。

  • Click here to download

    点击这里下载

  • Or search in VsCode marketplace tab或在“ VsCode市场”选项卡中搜索
  • After installing restart VsCode安装后重启VsCode
  • Open your C++ file in Vscode. Here's a basic hello world program below:在Vscode中打开您的C ++文件。 以下是一个基本的hello world程序:
#include <iostream>
using namespace std;
int main()
{cout << "Hello world!";return 0;
}

Save this file as test.cpp

将此文件另存为test.cpp

使用代码运行器运行代码 (Run your code using Code Runner)

  • Use the shortcut Ctrl+Alt+N

    使用快捷键Ctrl+Alt+N

  • Or press F1 and then select/type Run Code或按F1键,然后选择/键入“运行代码”
  • Or right-click the Text Editor and then click Run Code in the editor context menu或右键单击“文本编辑器”,然后在编辑器上下文菜单中单击“运行代码”

The code will run and the output will be shown in the Output Window. Open the output window with `Ctrl+ shortcut.

代码将运行,输出将显示在“输出”窗口中。 使用Ctrl +快捷键打开输出窗口。

停止运行代码 (To stop the running code)

  • Use the shortcut Ctrl+Alt+M

    使用快捷键Ctrl+Alt+M

  • Or press F1 and then select/type Stop Code Run或按F1键,然后选择/键入Stop Code Run
  • Or right-click the Output Channel and then click Stop Code Run in the context menu或右键单击“输出通道”,然后在上下文菜单中单击“停止代码运行”

Hurray, you just successfully set up your C++ environment in VsCode!

嗨,您刚刚在VsCode中成功设置了C ++环境!

结论 (Conclusion)

Here's a quick hint: By default, VsCode's output terminal is read-only. If you're running code that requires user input like:

快速提示:默认情况下,VsCode的输出终端为只读。 如果您正在运行需要用户输入的代码,例如:

#include <iostream>
using namespace std;const double pi = 3.14159; void calculate()
{double area; double radius;cout<<"Enter Radius: "<<endl; cin>>radius;area = pi * radius * radius; cout<<"area is: "<<area<<endl;}int main()
{calculate(); return 0;
}

you won't be able to type into the terminal, Cannot edit in read-only terminal.To fix this, you need to manually enable read-write.

您将无法在终端中键入“ Cannot edit in read-only terminal 。要解决此问题,您需要手动启用读写功能。

  • In VsCode, Go to File > Preference > Setting.在VsCode中,转到“文件”>“首选项”>“设置”。
  • In the User tab on the left panel, find the extensions section在左侧面板的“用户”标签中,找到扩展部分
  • Scroll and find 'Run Code Configuration'滚动并找到“运行代码配置”
  • Scroll and find a checkbox Run in Terminal (Whether to run code in Integrated Terminal) Check the box.

    滚动并找到一个复选框, Run in Terminal (是否在集成终端中运行代码)复选框。

OR

要么

  • In your setting.json file, add:

    在您的setting.json文件中,添加:

"code-runner.runInTerminal": true

Hurray, you're done and ready to roll :).

万岁,您已经准备就绪,可以滚动:)。

C ++资源 (C++ resources)

Here are some C++ resources you can use to get started with learning C++

以下是一些C ++资源,您可以用来开始学习C ++。

  • https://www.learncpp.com/

    https://www.learncpp.com/

  • https://www.codecademy.com/learn/learn-c-plus-plus

    https://www.codecademy.com/learn/learn-c-plus-plus

  • https://www.udemy.com/free-learn-c-tutorial-beginners/

    https://www.udemy.com/free-learn-c-tutorial-beginners/

  • https://www.sololearn.com/Course/CPlusPlus/

    https://www.sololearn.com/课程/ CPlusPlus /

  • https://www.youtube.com/watch?v=vLnPwxZdW4Y

    https://www.youtube.com/watch?v=vLnPwxZdW4Y

  • https://www.tutorialspoint.com/cplusplus/cpp_useful_resources.htm

    https://www.tutorialspoint.com/cplusplus/cpp_useful_resources.htm

  • https://makeawebsitehub.com/learning-c/

    https://makeawebsitehub.com/learning-c/

学分 (Credits)

  • MinGW Project

    MinGW项目

  • Code Runner by Jun Han

    代码亚军由韩军

Thank you for reading!

感谢您的阅读!

翻译自: https://www.freecodecamp.org/news/how-to-compile-your-c-code-in-visual-studio-code/

如何在Visual Studio Code中编译C ++代码相关推荐

  1. java代码如何与界面联系在一起_如何在Visual Studio Code 中编写Java代码

    本文将展示如何在Visual Studio Code中用Java编写和运行一个简单的Hello World程序. 首先您必须在本地开发环境中安装Java SE开发工具包(JDK) Visual Stu ...

  2. 在 Visual Studio Code 中添加自定义的代码片段

    无论是那个编辑器,如果能够添加一些自定义代码片段,能够大大提升代码的输入效率. 本文介绍如何在 Visual Studio Code 中添加自定义代码片段. 本文内容 Visual Studio Co ...

  3. vscode 注释多行代码_如何在Visual Studio Code中注释多行?

    我找不到在Visual Studio Code中注释和取消注释多行代码的方法. 是否可以使用某些快捷方式在Visual Studio Code中注释和取消注释多行? 如果是,该怎么办? 当其中一行已被 ...

  4. Latex002 | 详细教程:LaTeX 编译器哪个好?——如何在 Visual Studio Code 中全流程编写 LaTeX(上篇)

    一.LaTeX 一般用的编译器 一开始配置 LaTeX 环境的时候,我没有想太多,和大多数人一样直接去网上搜了编译器的选择.在互联网所提供给我们的海量资源中,以下这些是我认为值得一试的一些选择: Wi ...

  5. 在Visual Studio Code中查找并​​替换为换行符

    本文翻译自:Find and replace with a newline in Visual Studio Code I am trying out the new Microsoft Visual ...

  6. 如何在Visual Studio 2010中使用CppUTest建立TDD的Code Kata的环境

    CppUTest 是一个功能全面的测试框架,是为了支持在多种操作系统上开发嵌入式软件而特别设计的.CppUTest的宏被设计成不需要了解C++也可以写测试用例.这使得C程序员更容易用这个测试框架. 那 ...

  7. vscode中如何创新建php文件,php – 如何在Visual Studio Code,UNIX中的所有文件中创建所有行结尾(EOL)?...

    我使用Windows 10 home,我通常使用Visual Studio Code(VSCODE)来编辑Linux Bash脚本以及PHP和JavaScript. 我没有开发任何专门用于Window ...

  8. ubuntu下使用visual studio code来编译和调试C++

    最近想在linux上编译c++代码,自己却一直习惯window上的IDE.以前公司要我写Linux代码的时候,我一般都是用eclipse + CDT,而eclipse这东西吧,我个人感觉因为加载组件太 ...

  9. 解决OpenSSL加入到在Visual Studio 2019中编译返回LNK2019错误

    文章目录 小结 问题和解决 Error LNK2019 Error LNK1104 Warning C4996 参考 小结 碰到了OpenSSL加入到在Visual Studio 2019中编译返回L ...

最新文章

  1. nginx rails 详细站点配置入门教程
  2. 关于android 1.6全部的权限介绍
  3. 为什么黑客用python-为什么大多数黑客都使用Python!
  4. Hadoop历代版本及其特点
  5. mysql触发器的简单写法
  6. pythontkinter控件单选框怎么判断是否被选中_Python GUI编程(Tkinter)Radiobutton单选框控件...
  7. CSP2020 赛前总结
  8. Facebook图片存储架构技术全解析
  9. 多商户商城源码_多商户商城APP开发解决方案
  10. vue实现5秒后自动隐藏_王者荣耀:玩家选中单妲己被5楼疯狂嘲讽,发出战绩后,秒变舔狗...
  11. c语言中队列的作用,循环队列的实现(C语言)
  12. windows10误删Administrator用户的家目录之后
  13. 如何做好一位合格qc_如何做好现场QC,一位老QC的经验分享
  14. html之响应式(自适应)网页设计
  15. 家用小型UPS不间断电源—供电系统的保险
  16. 【淘宝】图片放大 代码
  17. 界面可视化(四):图像在PyQT5上的显示
  18. 不想进大厂的程序员是没有梦想的,2021年阿里、腾讯,java架构师指南百度云
  19. Spring自定义数据源配置不当引起的Mybatis拦截器Interceptors 失效/不生效
  20. 计算机网络——第4章网络层(下)

热门文章

  1. 运行时异常 检查时异常
  2. PHP SSL certificate: unable to get local issuer certificate的解决办法
  3. 【仿去哪儿登录】UITextField输入时检查输入框是否为空
  4. Ubuntu安装tomcat
  5. 【布局】圣杯布局双飞翼布局
  6. 在MySQL和PostgreSQL之外,为什么阿里要研发HybridDB数据库?
  7. [译稿]同步复制提议 2010-09
  8. c++11 多线程 1c++ concurrency in action
  9. android学习笔记之十服务(Service)
  10. Android中Handler