windows下使用vscode编写运行以及调试C/C++

内容会不定期更新。

一、vscode介绍

vscode是一个轻量的代码编辑器,本身是不带有编译器的,所以使用的时候要提前安装好一个C/C++编译器。肯定会有人拿vscode和sublime text去做一个比较,因为vscode已集成控制台通过写命令行的方式实现编译运行程序,所以在Windows下vscode体验稍微好一些,其他不做比较。

二、C/C++编译器安装

安装完编译器需要配置环境变量。

直接去官网下载mingw或者mingw-w64可能会下载失败,这里给出mingw-w64的离线版下载地址:mingw-w64 ,下载完解压即可。

解压的目录尽量简单些,假设解压在了D:\下,那么需要将D:\mingw64\bin加入系统环境变量,因为比较简单,这里不再赘述,可自行百度。

在cmd或者powershell下输入gcc -v出现以下内容,说明编译器安装成功:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=D:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

之后就可以通过g++ -o $test $test.cpp(后缀名可省略)

三、vscode下编译C++程序

这里先给出一个简单的示例程序:

#include <iostream>
using namespace std;
int main(){
    cout<<"Enter two numbers:"<<endl;
    int v1=0,v2=0;
    cin>>v1>>v2;
    cout<<"The sum of "<<v1<<"and "<<v2<<"is "<<v1+v2<<endl;
    return 0;
}
1、通过控制台写命令

使用ctrl + ~ 打开vscode控制台,点击终端(其实就是在windows下面的powershell或者cmd下操作),使用的命令如下:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

PS D:\Document\C++> cd 01/1.4
PS D:\Document\C++\01\1.4> ls


    目录: D:\Document\C++\01\1.4


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2019/1/3     11:32            211 test.cpp


PS D:\Document\C++\01\1.4>
PS D:\Document\C++\01\1.4> g++ -o test test.cpp

// 编译后增加test.exe文件 
PS D:\Document\C++\01\1.4> ls


    目录: D:\Document\C++\01\1.4


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2019/1/3     11:32            211 test.cpp
-a----         2019/1/5     20:37          58019 test.exe


PS D:\Document\C++\01\1.4> ./test.exe
Enter two numbers:
3 4
The sum of 3and 4is 7
2、通过code runner插件

安装下面两个插件
C/C++
Code runner
已安装插件

安装好以后就能在代码编辑框里看到
在这里插入图片描述

但是现在还有一个问题,程序无法实现交互,不能从键盘输入数据,解决办法如下:

依次打开:文件>首选项>设置>用户设置>拓展>Run Code Configuration,
找到 Run In Terminal 打上勾 这样运行的程序就会运行在vscode的集成控制台上。

程序运行的效果如下:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

PS D:\Document\C++> cd "d:\Document\C++\01\1.4\" ; if ($?) { g++ test.cpp -o test } ; if ($?) { .\test }
Enter two numbers:
3 4
The sum of 3and 4is 7

可以看出,插件就是把g++ -o test test.cpp命令给集成了下,如果程序编译的时候需要特殊的参数,需要自己写编译的命令。

四、调试

待补充

参考内容:https://www.cnblogs.com/TAMING/p/8560253.html

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在macOS M1芯片上配置VS Code的C/C++开发环境,你可以按照以下步骤进行操作: 1. 确保你已经安装了Xcode和命令行工具。你可以在终端中运行以下命令来检查是否安装了Clang/LLVM环境: ``` clang --version ``` 如果已安装,你应该能看到类似于"Apple clang version..."的输出信息。 2. 安装VS Code的官方版本。你可以从官方网站下载并安装VS Code。 3. 打开VS Code,并安装C/C++扩展。你可以在扩展商店中搜索"C/C++",然后点击安装。 4. 配置VS Code Server和Remote SSH远程开发扩展。这样你就可以通过VS Code进行远程开发。你可以按照官方文档中的说明进行配置。 5. 配置C/C++开发环境。你可以按照以下步骤进行配置: - 打开VS Code的设置(Preferences)。 - 在搜索栏中输入"C/C++",找到"C/C++: Edit Configurations (JSON)"选项并点击。 - 在配置文件中,你可以配置编译器路径、包含库的路径等。具体配置会因你的项目和需求而有所不同。你可以参考官方文档或其他资源来获取更多信息。 6. 创建一个C/C++项目并开始编写代码。你可以按照以下步骤创建一个简单的Hello World程序: - 在VS Code中创建一个新的文件夹,作为你的项目文件夹。 - 在项目文件夹中创建一个新的C/C++源文件,例如"main.c"。 - 编写你的C/C++代码。 - 使用VS Code的终端选项编译和运行你的代码。 7. 如果需要调试你的C/C++代码,你可以按照VS Code的调试文档进行配置。这将允许你在VS Code中设置断点、单步调试等。 希望这些步骤对你在macOS M1芯片上配置VS Code的C/C++开发环境有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MAC Pro 安装 VS Code 配置 C/C++ 开发环境](https://blog.csdn.net/Jmilk/article/details/128976289)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值