C++-配置vscode

vscode配置

win10

  1. 下载vscode,默认安装即可
  2. 下载mingw,压缩包,因为online的.exe下载太慢,这个压缩包浏览器下载也太慢,复制链接到迅雷下载飞快。
    下载后将压缩包解压到软件安装的位置,将*\mingw64\bin*添加到环境变量即可
  3. 安装vscode插件,暂时只安装了C/C++ code runner, 修改配置File>Preferences>setting>User>Run Code configuration
    勾选以下项即可再vscode终端运行代码
    vscode Terminal runing
  4. 测试代码
  • test.cpp
#include <iostream>
using namespace std;
int main(){
   cout<< "Hello World!"<<endl;
   return 0;
}

运行
ctrl+alt+n or F5

  • F5运行创建了.vscode/launch.json
    修改文件
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++", //配置名称
            "type": "cppdbg", //配置类型,默认
            "request": "launch", //请求配置类型,launch启动,attach附加?
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //调试程序路径
            "args": [], 
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", //工作目录
            "environment": [],
            "externalConsole": true, //是否显示控制台
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe", //gdb路径
            "preLaunchTask": "g++", //编译g++/c++,gcc/c
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        },
    ]
}
  • 再按F5弹框,点击configure task, 编辑tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "cpp.exe build active file",
            //"command": "D:\\mingw64\\bin\\cpp.exe",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}
  1. 程序可以运行,具体配置含义用到再了解

基本参照这个配置,建议看原文,只有tasks.json"label": "cpp.exe build active file",这样必须有,其他均一致
-2019.11.19更新,在新的电脑上这样配置task.json, 不行,参考这个,修改

"label": "g++", //和launch.json中preLaunchTask一致
"command": "D:/mingw64/bin/g++.exe",

ubuntu16.04

  • ctrl+alt+n可以运行
  • F5并不会出现.json配置
    手动配置
    • 你可以在菜单栏里面选择Terminal-->Configure Tasks,会自动帮你生成.vscode目录
    • .vscode文件夹下新建launch.json tasks.json
    • launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask":"build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-g", "${file}", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
        }
    ]
}

F5运行OK


添加头文件路径

#include<caffe/caffe.hpp>

出错,参考这个

  • ctrl+shift+p 选择c/c++ config,会创建c_cpp_properties.json文件
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/home/wl/software/caffe/include"
            ],
            "browse": {  
                "path": [  
                    "${workspaceFolder}",  
                    "/home/wl/software/caffe/include"  
                ],  
                "limitSymbolsToIncludedHeaders": true,  
                "databaseFilename": ""  
            }, 
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

实验证明,"browse"这个参数可以不配置

  • 修改task.json
"args": ["-g", "${file}", "-I", "/home/wl/software/caffe/include", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]

-I表示包含头文件路径,实验证明不要这个配置也行

  • 但是还是会抛出其他错误
	"message": "cannot open source file \"cublas_v2.h\" (dependency of \"caffe/caffe.hpp\")",

经查阅,是caffe有关cuda配置的,但是我的caffe明明编译的是cpu版本…

  • 剩下来解决#include<caffe/caffe.hpp>出错问题,重开一个记录
发布了20 篇原创文章 · 获赞 0 · 访问量 380

猜你喜欢

转载自blog.csdn.net/yywxl/article/details/102945066