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
下载
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和自定义命令。
- 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.tutorialspoint.com/cplusplus/cpp_useful_resources.htm
https://www.tutorialspoint.com/cplusplus/cpp_useful_resources.htm
学分 (Credits)
Thank you for reading!
感谢您的阅读!
翻译自: https://www.freecodecamp.org/news/how-to-compile-your-c-code-in-visual-studio-code/