【问题标题】:Getting random command-line args when compiling and debugging in Visual Studio Code在 Visual Studio Code 中编译和调试时获取随机命令行参数
【发布时间】:2020-07-23 23:38:02
【问题描述】:

所以我正在制作一个示例程序来学习如何在 Visual Studio Code 中构建和调试 C 项目。作为参考,这是我的项目的launch.jsontasks.json

launch.json:

"version": "0.2.0",
"configurations": [
    {
        "name": "test-project",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}\\test.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "build test-project"
    }
]

tasks.json:

"version": "2.0.0",
"tasks": [
    {
        "label": "build test-project",
        "type": "shell",
        "command": "gcc",
        "args": [
            "-std=c99",
            "-g",
            "${workspaceFolder}\\test.c",
            "-o",
            "${workspaceRoot}\\test.exe"              
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

这里是“test.c”:

test.c

#include <stdio.h>

int main(int argc, char* argv[]){


    int t = 9;
    int p = 10;
    return 0;
}

这是我在命令行参数中看到的,即使我没有提供命令行参数:

正如您在监视面板中看到的那样,有 3 个额外的命令行参数 "2&gt;CON""1&gt;CON""&lt;CON"。为什么即使我提供了 0 个命令行参数也会发生这种情况?

【问题讨论】:

  • 这些看起来像是文件描述符的重定向。 2&gt;CON 将 stderr 重定向到 CON,1&gt;CON 将 stdout 重定向到 CON,&lt;CON 将 stdin 从 CON 重定向。看起来有些东西正在将这些添加到 argv

标签: c visual-studio gcc


【解决方案1】:

在 Visual Studio Code 中进行一些搜索和测试后,我在 launch.json 中发现了一个名为 "externalConsole" 的选项,该选项将输出重定向到实际的命令提示符窗口,从命令行中删除了额外的重定向。还有另一个名为"avoidWindowsConsoleRedirection" 的选项可避免在不显示外部命令提示符的情况下添加额外的命令行参数/重定向。如果您的launch.json 文件中同时存在这两个选项,则"externalConsole" 优先。使用以下两者的示例:

"version": "0.2.0",
"configurations": [
    {
        "name": "test-project",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}\\test.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "avoidWindowsConsoleRedirection": true,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "build test-project"

    }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2010-09-22
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多