当前位置: 首页 > 编程日记 > 正文

如何在Visual Studio Code中编译C ++代码

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/

相关文章:

敏捷冲刺每日报告四(Java-Team)

第四天报告&#xff08;10.28 周六&#xff09; 团队&#xff1a;Java-Team 成员&#xff1a; 章辉宇&#xff08;284&#xff09; 吴政楠&#xff08;286&#xff09; 陈阳&#xff08;PM&#xff1a;288&#xff09; 韩华颂&#xff08;142&#xff09; 胡志权&#xff08;1…

终止forEach的循环

上代码&#xff1a; let list[1,2,3] try {list.forEach(item > {if (item1) {console.log(等于1就跳出循环)throw new Error("EndIterative");}}) } catch (e) {}

大学可以学前端开发_所有开发人员在大学中应该学习的东西

大学可以学前端开发忘记“代码行” (Forget About "Lines of Code") Source资源 As a developer, youll hear a lot of crazy, unbelievable theories about what "lines of code" signify. Believe none of them. Lines of code is a ridiculous metric …

20162325 金立清 S2 W8 C17

20162325 2017-2018-2 《程序设计与数据结构》第8周学习总结 教材学习内容概要 二叉查找树是一棵二叉树&#xff0c;对于其中的每个结点&#xff0c;左子树上的元素小于父结点的值&#xff0c;而右子树上的元素大于等于父结点的值。 最有效的二叉树是平衡的&#xff0c;所以每次…

H5画布不显示图片的问题解决

在onReady 执行 <template><view class""><canvas style"" canvas-id"myCanvas" id"myCanvas"></canvas><!-- <view class"container"><img :src"tempFilePath" /></…

javascript十六进制数字和ASCII字符之间转换

var hex"0x29";//十六进制 var charValue String.fromCharCode(hex);//生成Unicode字符 var charCode charValue.charCodeAt(0);//获取指定字符的十进制表示. var hexOri"0x"charCode.toString(16);;//将int值转换为十六进制 alert("hex:&q…

html5编写网页代码_freeCodeCamp.org的未来-从向世界传授语言到编写代码的5年经验...

html5编写网页代码freeCodeCamp went live in October 2014. In the five years since, weve done quite a bit.freeCodeCamp于2014年10月上线。在此之后的五年中&#xff0c;我们做了很多工作。 In this article, well explore:在本文中&#xff0c;我们将探讨&#xff1a; …

程序员眼中的英文单词是这样的

来源&#xff1a;Jackie Han英语中一个单词可能有很多不同的意思。很多中国开发者外语本来就不好&#xff0c;概念是往往先入为主。甚至在不清楚一般意义的情况下&#xff0c;先记住了特定环境中的意思。 转载于:https://www.cnblogs.com/agileai/p/5166982.html

linux系统无法挂载U盘

插上U盘 [ 2407.650440] usb 1-3.3: new high speed USB device number 7 using s5p-ehci [ 2407.887332] usb 1-3.3: New USB device found, idVendor0951, idProduct1666, bcdDevice0100 [ 2407.894249] usb 1-3.3: New USB device strings: Mfr1, Product2, SerialNumber3 […

小程序输入框上推页面不上推

样式问题&#xff0c;把样式去掉就行

面试:你了解中兴吗_HTTP简介:您需要了解的所有内容

面试:你了解中兴吗In this article, I will walk you through how the world wide web works at a fundamental level.在本文中&#xff0c;我将向您介绍基本的万维网工作原理。 The core technology is HTTP - Hypertext Transfer Protocol. Its the communication protocol …

img-responsive class图片响应式

在BootStrap中&#xff0c;给<img>添加 .img-responsive样式就可以实现图片响应式。1<img src"..." class"img-responsive">转载于:https://www.cnblogs.com/zouyun/p/7761393.html

小程序开发卡券

前期准备 小程序内领取卡券 1.开发者须有一个有卡券权限的公众号&#xff08;服务号&#xff09;和认证后的小程序账号&#xff1b; 2.开发者须申请一个开放平台账号&#xff0c;并将小程序和公众号绑定在同一个开放平台账号下&#xff0c;关于开放平台的介绍请参照&#xff1…

php学习之道:WSDL具体解释(三)

通过声明方式定义绑定&#xff08;binding&#xff09;属性 假设你在服务中採用SOAP binding。你能够使用JAX-WS来指定一定数量的属性binding。这些属性指定相应你在WSDL中指定的属性。某些设置。比方參数类型&#xff0c;能够约束你实现的方法。这些设置也影响声明的效用。 SO…

什么是棉绒,它如何节省您的时间?

One of the biggest challenges in software development is time. It’s something we can’t easily get more of, but linting can help us make the most out of the time we have.时间是软件开发中最大的挑战之一。 这是我们无法轻易获得的更多东西&#xff0c;但是棉绒可…

可持久化线段树(主席树)【舰娘系列】【自编题】

[pixiv] https://www.pixiv.net/member_illust.php?modemedium&illust_id60083619 向大(hei)佬(e)势力学(di)习(tou) 前段时间做了一套大佬自己出的题&#xff08;大佬竟然是个宅男2333&#xff09;&#xff0c;蒟蒻的我自然是只得了30分的暴力分:-( fleet 舰队 【题目描…

读书笔记——《黑客大曝光》(1/8)

第一部分 收集情报 案例研究 Tor系统基于洋葱路由器&#xff0c;是第二代低延迟匿名系统&#xff0c;用户可通过它在互联网上进行匿名通信。Tor网络的使用者必须在他们的系统上运行一个洋葱代理&#xff0c;这个代理允许它们在Tor网络上进行通信&#xff0c;并协商一个虚拟链路…

H5 自动播放背景音频,兼容安卓和苹果手机, ios createInnerAudioContext 无法自动播放解决

原因应该是IOS不允许自动播放音频,有两种解决方法 在main.js Vue.prototype.innerAudioContext = uni.createInnerAudioContext(); //创建播放器对象 Vue.prototype.playAudio = function(audioUrl) {console.log(播放)var innerAudioContext = Vue.prototype.innerAudioCont…

粒子耗尽 粒子滤波_如何使用粒子的强大蓝牙API

粒子耗尽 粒子滤波This post is originally from www.jaredwolff.com 这篇文章最初来自www.jaredwolff.com I was defeated. 我被打败了。 I had spent the whole night trying to get a Bluetooth Low Energy project working. It was painful. It was frustrating. I was r…

Android笔记(adb命令--reboot loader)

Android 的机器通过adb进入升级模式的方法 # adb shell # reboot loader 通过上面两个命令就进入升级模式了&#xff0c;通过工具升级就好了 为什么会写这简单的一篇呢&#xff1f;因为今天干了一件很傻很傻的事&#xff0c;特别记录下来。 业务那边今天急着要把机器寄给客户&a…

样式集(八)弹窗,规则弹窗,半透明弹窗

效果图&#xff1a; 代码&#xff1a; <view class"popupBlock" v-if"showPopupBlock"><view class"xxx"><image class"xxxImg" click"showPopupBlockfalse" mode"widthFix" src"../../stat…

typeof操作符的返回值

使用typeof操作符 对一个值使用typeof操作符可能返回下列某个字符串: 1):undefined——如果这个值未定义 2):boolean——如果这个值是布尔值 3):string——如果这个值是字符串 4):number——如果这个值是数值 5):object——如果这个值是对象或null&#xff0c;数组&#xff0c;…

定制开发软件所有权_职业所有权软件开发人员指南

定制开发软件所有权介绍 (Introduction) 您的职业正在流向大海吗&#xff1f; (Is Your Career Drifting Out To Sea?) Like a frog whos slowly being boiled in a pot but doesnt realize it, 2 years into my career I slowly came to discover that I wasnt progressing a…

转:在线框架引用 bootstrap/jq/jqmobile/css框架

bootstrap百度调用 <script src"http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script><link href"http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel"stylesheet" /> 使用 Bootstrap 中文…

曲线图实现,可滚动曲线图,自定义数据

实现可以拖动的曲线图,自定义X轴数据的缩进,自定义X轴显示多少格。 效果图 数据格式,数据说明代码可见 曲线图实现 u-charts.js 可以在官网下载 <template><view class="qiun-columns"><view class=""><view class="qiu…

MongoDB 删除数据库

MongoDB 删除数据库 语法 MongoDB 删除数据库的语法格式如下&#xff1a; db.dropDatabase() 删除当前数据库&#xff0c;默认为 test&#xff0c;你可以使用 db 命令查看当前数据库名。 实例 以下实例我们删除了数据库 runoob。 首先&#xff0c;查看所有数据库&#xff1a; &…

安装meme_通过构建Meme生成器学习React

安装memeMemes are great - theyre such a fun way of describing ideas and opinions. So its no coincidence that I picked a meme generator app as the capstone project in my free React course on Scrimba. The app works by pulling a random meme image from an API …

Thunder团队第三周 - Scrum会议7

Scrum会议7 小组名称&#xff1a;Thunder 项目名称&#xff1a;i阅app Scrum Master&#xff1a;胡佑蓉 工作照片&#xff1a; 邹双黛在照相&#xff0c;所以图片中没有该同学。 参会成员&#xff1a; 王航&#xff1a;http://www.cnblogs.com/wangh013/ 李传康&#xff1a;htt…

修改u-charts的点的大小和线的粗细

效果图&#xff1a; 修改源码&#xff0c;找到u-charts.js &#xff0c; 修改他画布的点的大小&#xff0c;在这两行框的地方&#xff0c;改了就好了 完整代码&#xff1a; <template><view class"qiun-columns"><view class""><vie…

Swift中使用typealias定义一个闭包closure

在OC中我们定义一个Blocks是这样定义的&#xff1a; typedef void (^ZWProgressHUDCompletionBlock)();在Swift中定义一个闭包是这种&#xff1a; typealias ZWProgressHUDCompletionBlock()->Void转载请注明。。。欢迎大家增加交流群&#xff1a;爱疯、爱Coding&#xff1a…