Visual Studio代码中的任务没有找到文件

时间:2023-01-28 20:20:52

I want to compile a cpp-file in Visual Studio Code, and thus I set up the following tasks.json:

我想用Visual Studio代码编译一个cppa文件,因此我设置了以下的task .json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++-5",
    //"args": ["-O2",  "-Wall", "${file}", "-o ${fileBasename}"],
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "Compile",
            // Make this the default build command.
            "isBuildCommand": true,
            // Show the output window only if unrecognized errors occur.
            "showOutput": "always",
            // No args
            //"args": ["all"],
            "args": ["-O2",  "-Wall", "${file}", "-o ${fileBasename}"],
            // Use the standard less compilation problem matcher.
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

But now if I switch to the file in question (which is located next to the .vscode-folder, where the tasks.json-file is located), and execute the compile-task, I get "file not found" as error from gcc. Where is my problem in my json-code?

但是现在如果我切换到有问题的文件(它位于任务所在的.vscode文件夹旁边)。在执行编译任务时,我从gcc获得“文件未找到”作为错误。我的json代码有什么问题吗?

1 个解决方案

#1


1  

There are a couple problems.

有几个问题。

  1. You need to suppress the task name, it is being passed to the command as well. If you swap out "css" in the command for "echo", you'll see that the command being executed with Compile -02 -Wall filename.cpp -o "-o filename.cpp". Which brings me to the next two issues...
    1. You need to seperate -o and basefilename. Extra "s are making their way into your command
    2. 您需要分离-o和basefilename。你的命令中有额外的命令
    3. filename and basefilename are the same (in my tests). But if you just want the default output (filename.cpp -> filename.o), do you even need the -o argument? Can't you just call gcc -02 -Wall filename.cpp? If so, then you don't need to worry about #2 or about how to get a different output name.
    4. 文件名和basefilename是相同的(在我的测试中)。但是如果您只想要默认的输出(文件名)。cpp -> filename.o),你是否需要-o参数?你不能直接调用gcc -02 -Wall filename.cpp吗?如果是这样,那么您不需要担心#2或如何获得不同的输出名。
  2. 您需要抑制任务名称,并将其传递给命令。如果您在“echo”命令中切换“css”,您将看到正在执行的命令的编译-02 -Wall文件名。cpp - o - o filename.cpp”。这就引出了接下来的两个问题……您需要分离-o和basefilename。额外的“s”正在进入您的命令文件名和basefilename是相同的(在我的测试中)。但是如果您只想要默认的输出(文件名)。cpp -> filename.o),你是否需要-o参数?你不能直接调用gcc -02 -Wall filename.cpp吗?如果是这样,那么您不需要担心#2或如何获得不同的输出名。

tasks.json

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "gcc",
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "Compile",
            // This prevents passing the taskName to the command
            "suppressTaskName": true,
            // Make this the default build command.
            "isBuildCommand": true,
            "showOutput": "always",
            "args": ["-O2",  "-Wall", "${file}"],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

#1


1  

There are a couple problems.

有几个问题。

  1. You need to suppress the task name, it is being passed to the command as well. If you swap out "css" in the command for "echo", you'll see that the command being executed with Compile -02 -Wall filename.cpp -o "-o filename.cpp". Which brings me to the next two issues...
    1. You need to seperate -o and basefilename. Extra "s are making their way into your command
    2. 您需要分离-o和basefilename。你的命令中有额外的命令
    3. filename and basefilename are the same (in my tests). But if you just want the default output (filename.cpp -> filename.o), do you even need the -o argument? Can't you just call gcc -02 -Wall filename.cpp? If so, then you don't need to worry about #2 or about how to get a different output name.
    4. 文件名和basefilename是相同的(在我的测试中)。但是如果您只想要默认的输出(文件名)。cpp -> filename.o),你是否需要-o参数?你不能直接调用gcc -02 -Wall filename.cpp吗?如果是这样,那么您不需要担心#2或如何获得不同的输出名。
  2. 您需要抑制任务名称,并将其传递给命令。如果您在“echo”命令中切换“css”,您将看到正在执行的命令的编译-02 -Wall文件名。cpp - o - o filename.cpp”。这就引出了接下来的两个问题……您需要分离-o和basefilename。额外的“s”正在进入您的命令文件名和basefilename是相同的(在我的测试中)。但是如果您只想要默认的输出(文件名)。cpp -> filename.o),你是否需要-o参数?你不能直接调用gcc -02 -Wall filename.cpp吗?如果是这样,那么您不需要担心#2或如何获得不同的输出名。

tasks.json

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "gcc",
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "Compile",
            // This prevents passing the taskName to the command
            "suppressTaskName": true,
            // Make this the default build command.
            "isBuildCommand": true,
            "showOutput": "always",
            "args": ["-O2",  "-Wall", "${file}"],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}