launch.json, tasks.json configuration error using CMakeLists

A simple helloworld.cpp, compiled using CMakeLists, everything is set up.

launch.json and tasks.json are also configured;

If you compile it manually, it will be successful.

mkdir build

cd build

cmake -G "MinGW Makefiles" ..

mingw32-make

everything is normal

But if the json file is configured, it will never work:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/cmake_hello.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/CppCompiler/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set disassembly flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build"
        }
    ]
}

 tasks.json

{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "type": "cppbuild",
            "label": "cmake",
            "command": "cmake",
            "args": ["-G", "\"MinGW Makefiles\"", ".."],
            "group": "build",
            "detail": "cmake -G \"MinGW Makefiles\" .."
        },
        {
            "options": {
                "cwd": "${workspaceFolder}/build",},
            "type": "cppbuild",
            "label": "make",
            "command": "mingw32-make",
            "args": [],
            "group": "build",
            "detail": "make"
        },
        {
            
            "label": "Build",
            "dependsOn": [
                "cmake",
                "make"
            ]
        }
    ]
}

Always reports error, unknown reason;

The reason is that there is a problem with the relative path of vscode. This problem has always existed. Although it has been modified according to some methods on the Internet, but

.. is not the upper-level directory, but the upper-level directory of the working directory. A single dot . does not represent the directory where the file is located, but the working directory;

You think it is a setting problem, but in fact, in other files, a single dot . represents the current directory;

Even if you run the same file multiple times, an error will appear in the relative path. vscode will recognize it as the current directory for a while, and then recognize it as the working directory for a while. It is a mess and will disgust you to death;

Therefore, if you change it to an absolute path, the problem will be solved;

{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "type": "cppbuild",
            "label": "cmake",
            "command": "cmake",
            "args": ["-G", "\"MinGW Makefiles\"", "${workspaceFolder}"],
            "group": "build",
            "detail": "cmake -G \"MinGW Makefiles\" ${workspaceFolder}"
        },
        {
            "options": {
                "cwd": "${workspaceFolder}/build",},
            "type": "cppbuild",
            "label": "make",
            "command": "mingw32-make",
            "args": [],
            "group": "build",
            "detail": "make"
        },
        {
            
            "label": "Build",
            "dependsOn": [
                "cmake",
                "make"
            ]
        }
    ]
}

In addition, this

"options": {

        "cwd": "${workspaceFolder}/build"

    },

It should be placed in a single task, otherwise it will be confusing and errors will be reported.

If one run only successfully generates the Makefile, then run it again and the executable file will be successfully generated again.

Guess you like

Origin blog.csdn.net/cicy5219/article/details/131494942