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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012949658/article/details/85872382

内容会不定期更新。

一、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

猜你喜欢

转载自blog.csdn.net/u012949658/article/details/85872382