zoukankan      html  css  js  c++  java
  • visual studio code使用MSVC编译C++

    环境

    OS:Microsoft Windows [Version 10.0.17134.285] x64

    VSCVersion:1.27.2 (system setup)

    VS:2017

       

    心血来潮想使用VSC来写C++,官方文档和网上大多资料都是使用g++进行编译的。这里给出使用MSVC的方案。

    参考了《用Visual Studio Code 来生成您的C++应用程序》的方法,当给出的版本太旧,在最新的win10上会有错误。

       

    1. 在插件界面安装C++开发组件。

    2. 新建个文件夹,命名为helloworld,并使用VSC打开。之后新建helloworld.cpp,输入标准的HelloWorld代码。

    3. Ctrl+Shift+P唤出控制台,执行C/Cpp: Edit Configurations...命令,初始化工程。

      之后会生成.vscode文件夹和c_cpp_properties.json文件。

    4. 查找vcvarsall.bat(需要安装Visual Studio)路径,之后新建一个build.bat(与helloworld.cpp同一路径),输入以下指令:

    @echo off

    call "C:Program Files (x86)Microsoft Visual Studio2017CommunityVCAuxiliaryBuildvcvarsall.bat" x64

    set compilerflags=/Od /Zi /EHsc

    set linkerflags=/OUT:helloworld.exe

    cl.exe %compilerflags% helloworld.cpp /link %linkerflags%

    注意将vcvarsall.bat路径替换成你自己的,如果是32位系统,需要将x64替换位x86

    1. Ctrl+,打开Settings界面,搜索terminal.integrated.shell.windows修改VSC默认的命令行为cmd(默认为powershell,使用powershell会出错)。

    2. Ctrl+Shift+P唤出控制台,Tasks:Configure Default Build Task,选择Other模板,新建tasks.json

      修改为如下参数:

    3. Ctrl+Shift+B运行buil task,开始编译。

      顺利的话就能在EXPLORER窗口看到编译后的程序。

       

    使用MSVC编译的话,调试配置会比较方便。

    1. Ctrl+Shift+D进入Debug界面,点击齿轮,选择C++(Windows)模板,生成launch.json。

    2. 修改launch.json中的:program属性。

      启动参数可以修改args参数。

    3. 下断点,开始调试。

    更多调试的操作,可以参考《C/C++ for Visual Studio Code (Preview)

       

    多文件编译可以参考《Walkthrough: Compiling a Native C++ Program on the Command Line》,编写相应的build.bat即可。

    参考资料:
    Visual Studio Code 来生成您的C++应用程序
    From <https://blogs.msdn.microsoft.com/c/2016/12/20/%E7%94%A8visual-studio-code-%E6%9D%A5%E7%94%9F%E6%88%90%E6%82%A8%E7%9A%84c%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F/>

    Walkthrough: Compiling a Native C++ Program on the Command Line
    From <https://msdn.microsoft.com/en-us/library/ms235639.aspx?f=255&MSPPError=-2147217396>

    C/C++ for Visual Studio Code (Preview)
    From <https://code.visualstudio.com/docs/languages/cpp>

     

  • 相关阅读:
    HDU1397:Goldbach's Conjecture
    火狐浏览器丢失书签,恢复无法处理备份文件的解决方案
    火狐浏览器单击链接总是在一个新的标签页打开的设置方法
    Mysql错误:#1054 Unknown column 'id' in 'field list' 解决办法
    关于PHP的内置服务器的使用
    PHP内置服务器
    SecurityManager 规格严格
    Centos或者Redhet开通telnet 规格严格
    搜索分布式 规格严格
    BenchMark 规格严格
  • 原文地址:https://www.cnblogs.com/foundkey/p/9675300.html
Copyright © 2011-2022 走看看