Search code examples
c++jsonvisual-studio-codecompilationvscode-code-runner

Code Runner in vsCode ignoring my compiler arguments in my tasks.json file


I am using vsCode with the C/C++ extension and code runner. I want to compile with a -DLOCAL flag so that I can do

#ifdef LOCAL
...
#endif

The problem is that when I compile my code with the code runner extension, it seems to ignore that flag and the #ifdef doesn't work. However, when I use run and debug, the ifdef works. This is my tasks.json, I only added the -DLOCAL flag:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-DLOCAL",
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

When I hit the run button from code runner it runs this command on the command line:

cd "d:\vsCodeCompProg\coding5\" ; if ($?) { g++ myfile.cpp -o myfile } ; if ($?) { .\myfile }

Here is what is run when I hit run and debug

"C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe" -DLOCAL -fdiagnostics-color=always -g D:\vsCodeCompProg\coding5\myfile.cpp -o D:\vsCodeCompProg\coding5\myfile.exe

How can I configure code runner so that it follows my tasks.json args? I have looked in the settings for the extension and have not found something that fixes this.


Solution

  • It looks like the code runner is not honoring the tasks.json at all. For me I need to modify the the Code Runner's executorMap by going to:

    Extensions -> Code Runner -> extension settings -> edit in JSON -> add the -DLOCAL flag in the "cpp" command entry.

    extension settings

    edit in JSON

    executorMap path under Library folder

    CPP command entry