Skip to content

Vscode

基本设置

conda集成

如果系统没有原有的python解释器,只有conda里的python, 会出现报错

Python error in VSCode :Sorry, something went wrong activating IntelliCode support for Python

这是因为vscode找不到pylance

visual studio code - Python error in VSCode :Sorry, something went wrong activating IntelliCode support for Python - Stack Overflow

解决:安装pylance扩展

调试配置

利用在.vscode中创建launch.json和task.json。可以实现一键运行或调试程序

说明

常用的一些变量

  • ${workspaceFolder}: 在 VS Code 中打开的文件夹的根路径。
  • ${file}: 当前正在编辑器中打开(具有焦点)的文件的完整路径。
  • ${fileDirname}: 当前打开文件所在的文件夹路径。
  • ${fileBasenameNoExtension}: 当前打开文件的文件名,但不包含扩展名(例如 test.cpp 变为 test)。
  • ${cwd}: 任务运行器或调试器启动时的当前工作目录。 常用的参数

  • args: 传给程序的命令行参数。每一个空格分隔的字符串都必须作为数组的一个独立元素。

    • 重要提示:不要把整个命令写在一起。
    • 正确示例:"args": ["--batch_size", "16", "--lr", "1e-4"]
  • 环境变量设置:

    • Python (env): 使用对象格式。常用于指定 GPU 或 Python 路径。
      • 示例:"env": { "CUDA_VISIBLE_DEVICES": "0", "PYTHONPATH": "${workspaceFolder}" }
    • C++ (environment): 使用包含 name/value 对象的数组格式。
      • 示例:"environment": [{"name": "LD_LIBRARY_PATH", "value": "/usr/local/cuda/lib64"}]

例子

python

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Train ",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/train.py",
            "console": "integratedTerminal",
            "justMyCode": false, // 设置为false可以跳转进第三方库源码
            "env": {
                "CUDA_VISIBLE_DEVICES": "0", // 指定显卡
            },
            "args": [
                "--data_path", "${workspaceFolder}/data",
                "--batch_size", "32",
                "--epochs", "10",
                "--resume" // 这是一个flag开关
            ]
        }
    ]
}

Cmake

以minisql为例子 launch.json:

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "Debug tests",

            "type": "cppdbg",

            "request": "launch",

            "program": "${workspaceRoot}/build/test/${fileBasenameNoExtension}",

            //只能对test里面的cpp文件启动调试

            "args": ["para1", "para2"],

            "stopAtEntry": false,

            "cwd": "${workspaceRoot}/build/test",//工作目录,

            // "cwd":"${fileDirname}",

            //决定了程序生成文件的路径,防止数据库文件,test文件等被生成到其他文件夹

            "environment": [],

            "externalConsole": false,

            "MIMode": "gdb",

            "setupCommands": [

                {

                    "description": "Enable pretty-printing for gdb",

                    "text": "-enable-pretty-printing",

                    "ignoreFailures": true

                }

            ],

            "preLaunchTask": "Make Test",//调用task.json里面的make

            "miDebuggerPath": "/usr/bin/gdb"

        },

        {

            "name": "Debug main",

            "type": "cppdbg",

            "request": "launch",

            "program": "${workspaceRoot}/build/bin/main",

            "args": ["para1", "para2"],

            "stopAtEntry": false,

            "cwd": "${workspaceRoot}/build/bin",

            "environment": [],

            "externalConsole": false,

            "MIMode": "gdb",

            "setupCommands": [

                {

                    "description": "Enable pretty-printing for gdb",

                    "text": "-enable-pretty-printing",

                    "ignoreFailures": true

                }

            ],

            "preLaunchTask": "",

            "miDebuggerPath": "/usr/bin/gdb"

        }

    ]

}

tasks.json

{

    "version": "2.0.0",

    "tasks": [

        {

            "label": "Make Test", // 显示在VS Code任务列表中的名称

            "type": "shell",

            "command": "make", // 要执行的命令

            "args": [

                "-C","${workspaceRoot}/build",//指定输出目录

                "${fileBasenameNoExtension}"

            ], // 命令的参数列表,如果有的话

            "group": {

                "kind": "build",

                "isDefault": true // 这将使这个任务成为默认的构建任务

            },

            "presentation": {

                "reveal": "always", // 控制命令行输出的显示方式

                "panel": "shared"

            },

            "problemMatcher": [] // 用于匹配编译错误和警告的模式

        }

        {

            "label": "Make Main", // 显示在VS Code任务列表中的名称

            "type": "shell",

            "command": "make", // 要执行的命令

            "args": [

                "-C","${workspaceRoot}/build/bin",//指定输出目录

            ], // 命令的参数列表,如果有的话

            "group": {

                "kind": "build",

                "isDefault": true // 这将使这个任务成为默认的构建任务

            },

            "presentation": {

                "reveal": "always", // 控制命令行输出的显示方式

                "panel": "shared"

            },

            "problemMatcher": [] // 用于匹配编译错误和警告的模式

        }

    ]

}

cursor

无法打开wsl: 关掉cursor, 命令行重启wsl,再打开cursor WSL extension is supported only in Microsoft versions of VS Code · Issue #2027 · getcursor/cursor

无法在wsl安装C/C++扩展:Workaround: Set “Download Extensions Locally” in Settings. Can't install C/C++ extension in remote - Bug Reports - Cursor - Community Forum

Comments