Search code examples
visual-c++visual-studio-codedirectory-structure

Change the build path for obj files


I recently downloaded the C/C++ extension for vs code, following this tutorial on setting it up for windows. I managed to figure out how to change the build path for the executable but I can't figure out how to change the build path for the .obj and vc140.pdb files.

This is the directory structure I would like to end up with once I build and run the program: directory

Right now the .obj and the vc140.pdb files end up in the root of my directory but I would like to place them under the obj folder as shown above.

This is my current tasks.json file:

{
 "version": "2.0.0",
 "tasks": [
    {
        "type": "shell",
        "label": "C/C++: cl.exe build active file",
        "command": "cl.exe",
        "args": [
            "/Zi",
            "/EHsc",
            "/Fe:",
            "${workspaceFolder}\\bin\\debug\\${fileBasenameNoExtension}.exe",
            "${file}"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$msCompile"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
     }
  ]
}

Solution

  • I found the solution to my problem after going through the command line arguments for the msvc compiler

    I had to add a /Fd and a /Fo argument to correctly configure the build path for the pdb and obj files.

    If anyone is interested in the tasks.json, here it is:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "type": "shell",
                "label": "C/C++: cl.exe build active file",
                "command": "cl.exe",
                "args": [
                    "/Zi",
                    "/EHsc",
                    "/Fe:",
                    "${workspaceFolder}\\bin\\debug\\${fileBasenameNoExtension}.exe",
                    "/Fd:",
                    "${workspaceFolder}\\obj\\debug\\vc140.pdb",
                    "/Fo:",
                    "${workspaceFolder}\\obj\\debug\\${fileBasenameNoExtension}.obj",
                    "${file}"
                    
                ],
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "problemMatcher": [
                    "$msCompile"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }