Post

09. Why VSCode + CMake Sometimes Builds the Wrong Folder

09. Why VSCode + CMake Sometimes Builds the Wrong Folder

Why VSCode + CMake Sometimes Builds the Wrong Folder


Prerequisites


1. Define Problem

Recently while working on my C++/OpenCV projects, I ran into an issue where VSCode kept building the project into the wrong directory even though I had already changed the CMake build path.

At first it looked like:

1
2
CMake says: build_new
But VSCode still builds: build

and the debugger could not find the correct executable.

After digging into it, I realized the issue was caused by different VSCode configuration files pointing to different build directories.

2. Json

The Important Lesson

In VSCode + CMake projects, there are usually three different places controlling your build/debug behavior:

  • settings.json: CMake build directory
  • tasks.json: manual build task directory
  • launch.json: debugger executable path

If even one of them points to a different folder or configuration (Debug vs Release), things quickly become confusing.

2-1. settings.json

This controls the build directory used by the CMake Tools extension.

1
2
3
{
    "cmake.buildDirectory": "${workspaceFolder}/build_new"
}

This means CMake itself is configured to generate everything inside: build_new/

2-2. tasks.json

The problem was that my manual build task was still pointing to the old folder:

1
2
3
4
5
6
"args": [
    "--build",
    "build",
    "--config",
    "Release"
]

So even though CMake Tools was configured correctly, the custom build task was still compiling into build/ instead of build_new/

The fix was simply changing it to:

1
2
3
4
5
6
"args": [
    "--build",
    "build_new",
    "--config",
    "Release"
]

2-3. launch.json

Another issue appeared during debugging.

My debugger configuration was

1
"program": "${workspaceFolder}/build_new/Debug/SemiProject.exe"

But I was building in Release mode.

So the actual executable existed here:

1
build_new/Release/SemiProject.exe

not:

1
build_new/Debug/SemiProject.exe

which caused launch failures.

This post is licensed under CC BY 4.0 by the author.