c++ vscode 第三方库_vscode初探

写在前面的话——vscode有个轻量级特点,初学编程的同学用起来可能会感觉挺好。在windows下用过vs2019系列或者qt的童鞋,在实际做项目时感觉体现不出vscode的优势(多个第三方库的混战实在是配置的头疼)。如果要用,就从一开始摒弃windows系统吧,直接linux;开源的各种项目在linux下的支持更友好。


个人在windows下开发c++较多,因此vscode的开始是c++版本的。vscode的c++细分了不同场合下如何应用:

  • windows+mingw
  • linux(windows子系统)
  • microsoft c++
  • clang macos

如果有看官网的习惯,不防直接看英文官网。选择左侧目录栏下的c++即可。

Documentation for Visual Studio Code​code.visualstudio.com
20d484363acc604302d297b1d521eee9.png

特别说明:json文件不允许有注释,下面的注释在项目中需要手动删除

下面的笔记以mingw为例。

安装内容

  • vscode
  • mingw
  • vscode扩展

安装过程忽略,自行百度。

vscode扩展在vscode安装好之后,左侧菜单栏最后一项打开,搜索以下内容进行安装

  • C/C++:又名 cpptools,提供Debug和Format功能
  • Code Runner:右键即可编译运行单文件,很方便;但无法Dubug

其他可选扩展:

  • Bracket Pair Colorizer 2:彩虹花括号
  • One Dark Pro:大概是VS Code安装量最高的主题

mingw版本的vscode启动方式:打开cmd,输入"code ."打开vscode

tasks.json

// 作用是 build cpp 和 running the build cpp

main menu --> Terminal --> configure default build task 自动创建tasks.json

编译:

如果只编译某个cpp,则切换到需要编译的文件下,执行编译命令,并产生exe;如果编译全部cpp,则切换到任意cpp文件下,执行编译命令,并产生exe

编译命令:Ctrl+Shift+B 或者main menu --> Terminal --> Run Build task

编译完成之后会在当前文件夹下产生一个***.exe;这个exe的名称可以在tasks.json中的“args”中指定

运行编译好的exe文件:

main menu --> Terminal --> New Terminal

dir

.***.exe

效果如下:

PS D:VS-Code-CplusplusHELLOWORLD> dir

目录: D:VS-Code-CplusplusHELLOWORLD

Mode LastWriteTime Length Name

---- ------------- ------ ----

d----- 2020/01/17 11:39 .vscode

-a---- 2020/01/17 10:50 291 helloworld.cpp

-a---- 2020/01/17 11:09 149497 helloworld.exe

-a---- 2020/01/17 11:47 149497 myProgram.exe

PS D:VS-Code-CplusplusHELLOWORLD> .myProgram.exe

Hello C++ World from VS Code and the C++ extension!

PS D:VS-Code-CplusplusHELLOWORLD>

launch.json

// 作用是 press F5 to debug the program

main menu --> Debug --> Add configuration --> C++(GDB/ILDB) 自动创建launch.json

不知道为什么我的vscode没有下拉选项“Choose g++.exe build and debug active file.” 只能手动修改

需要修改"program" 和 "miDebuggerPath"

且configurations中多了一项"preLaunchTask": "g++.exe build active file"

这一条的作用:调试前重新编译一遍

开始调试

A. 不设断点,停在程序开始位置:"stopAtEntry": true

B. 设置断点,停在断点位置:"stopAtEntry": false

1. 切换到cpp文件 (即active file)

2. Press F5 or from the main menu choose Debug > Start Debugging.

c_cpp_properties.json

which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.

运行C/C++:Edit configurations(UI):

Ctrl+Shift+P 打开控制窗口,输入"C/C++" --> Choose C/C++:Edit configurations(UI)

结果:在.vscode文件夹下自动生成c_cpp_properties.json文件

添加头文件(当需要的头文件不在工作区或者不在标准库路径时):

修改"includePath"选项

比如添加opencv的头文件

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${vcpkgRoot}/x86-windows/include",
                "E:opencv_4.1.1opencvbuildinclude" ,
                "E:opencv_4.1.1opencvbuildincludeopencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "E:MinGWbingcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x86"
        }
    ],
    "version": 4
}

那么添加opencv的库文件在哪里呢?网上有说在tasks.json下的args参数下,分别按照-I -L -l去设置

"args": [

"-I", "E:opencv_4.1.1opencvbuildinclude",

"-L", "E:opencv_4.1.1opencvbuild***", //这里因为是windows下使用了mingw,所以必须是mingw版本的opencv

//如果是linux系统下,后面的输出文件后缀就要从exe改为o

"-l", "opencv_world411",

"-g",

"${workspaceFolder}*.cpp", //"${file}", 只编译当前文件改为编译文件夹内的所有cpp文件

"-o",

"${workspaceFolder}myProgram.exe"//"${fileDirname}${fileBasenameNoExtension}.exe"

],

mingw版本的opencv下载地址:https://github.com/huihut/OpenCV-MinGW-Build

当创建一个新的工作区时,只需要拷贝之前的.vscode文件夹到新的工作区,做适当修改即可开始coding!


microsoft c++例子

只列出与mingw版本不同的地方

安装内容

  • vscode
  • vs2019 c++模块
  • vscode扩展

To use MSVC in VS Code, you must start VS Code from a Developer Command Prompt for Visual Studio.

  1. 开始菜单搜索 Developer Command Prompt for Visual Studio
  2. 进入到你想要放置项目位置的文件夹
  3. 输入命令code . 打开vscode

命令如下:

mkdir projects 
cd projects 
mkdir helloworld 
cd helloworld 
code . 

第一步配置c_cpp_properties.json

// 作用是 build cpp 和 running the build cpp

Ctrl+Shift+P 打开控制窗口,输入"C/C++" --> Choose C/C++:Edit configurations(UI)

修改compiler path为

E:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105binHostx64x64cl.exe

修改IntelliSense mode为msvc-x64

添加opencv外部依赖库类似于mingw

tasks.json

main menu --> View --> Command Palette

--> "task" --> Tasks: Configure Default Build Task

--> Create tasks.json file from template

--> Others

替换全部文件内容:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "msvc build",
      "type": "shell",
      "command": "cl.exe",
      "args": ["/EHsc",  //specifying the exception handling mode (EHsc)
               "/Zi",    //produce a debug build with symbols (Zi)
              "/Fe:", "helloworld.exe", //tells the compiler to name the executable "helloworld.exe"
              "helloworld.cpp"],
      "group": {
        "kind": "build",
        "isDefault": true // run when you press Ctrl+Shift+B
      },
      "presentation": {
        "reveal": "always"
      },
      "problemMatcher": "$msCompile"
    }
  ]
}

launch.json

// 作用是 press F5 to debug the program

main menu --> Debug --> Add configuration --> C/C++ Windows(Launch) 自动创建launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(msvc) Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/helloworld.exe",
      "args": [],
      "stopAtEntry": true, //F5在main的第一行创建一个断点并主动停下,否则停在手动断点处
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false
    }
  ]
}

build

注意:再次强调编译之前需要把文件切换到某个cpp

Ctrl+Shift+B

or

View > Command Palette and start typing "Tasks: Run Build Task"

编译出错:

Note: If you see an error message that looks like this: cl.exe: command not found, it means you have not started VS Code from a Developer Command Prompt for VS.

debug

Press F5 or from the main menu choose Debug > Start Debugging

watch

变量监视

手动添加代码中的变量到watch中,实时监视变量状态


opencv配置-msvc

c_cpp_propotites.json

{
    "configurations": [
        {
            "name": "x64",
            "includePath": [
                "${workspaceFolder}/**",
                "${vcpkgRoot}/x86-windows/include",
                "E:opencv_4.1.1opencvbuildinclude",
                "E:opencv_4.1.1opencvbuildincludeopencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "E:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105binHostx64x64cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "msvc build",
        "type": "shell",
        "command": "cl.exe",
        "args": [   
            "-I","E:opencv_4.1.1opencvbuildinclude",
            "E:opencv_4.1.1opencvbuildx64vc15libopencv_world411.lib",  
            "/EHsc", 
            "/Zi", 
            "/Fe:", "helloworld.exe", 
            "${workspaceFolder}*.cpp" //"helloworld.cpp"         "${workspaceFolder}*.cpp"   
        ],
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "presentation": {
          "reveal": "always"
        },
        "problemMatcher": "$msCompile"
      }
    ]
  }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值