21

I cloned a github repository. It has been set to built with CMake. I want to build it in VS Code, but I cannot find a place to set CMake command line arguments.

When I execute CMake configure in VS Code, it complains cannot find some headers or libs. I test in terminal, when I set -D arguments, this error can be solved.

So, I wonder is there a place to pass CMake command line arguments in VS Code?

1
  • 1
    Does anyone know how to pass args to the "Run" button in the status bar at the bottom? I don't see any settings to be able to modify this. For example if I want to run my binary like this ./binary -c config Commented Aug 17, 2023 at 15:02

3 Answers 3

32

Passing information onto CMake in VSCode happens in the settings.json file.

You need to install the CMake Tools extention first.

Then, open the command pallet in VSCode with Ctrl+Shift+P and type Open Settings.

Once selected, VSCode will show a settings.json file where you should add cmake.configureArgs e.g.:

{
 "other":"settings",
 "cmake.configureArgs": [
        "-DOPTIONA=ON",
        "-DOPTIONB=ON"
    ],
}

The cmake.configureArgs array of arguments is the same as you would normally use during a command-line cmake configure e.g.

cmake -DOPTIONA=ON -DOPTIONB=ON ..
Sign up to request clarification or add additional context in comments.

4 Comments

I needed "-DCMAKE_TOOLCHAIN_FILE=path/to/vcpkg/scripts/buildsystems/vcpkg.cmake" to integrate vcpkg. Thaks
Side note: this saves CMake command line arguments globally. If you need per-project configuration, save these settings to ${project_dir}/.vscode/settings.json
I'm not sure this information is current anymore. I am fairly sure there are other files that users should add/edit to set cmake argument, rather than adding them to the .vscode settings file.
Ok I figured it out. I think it would be better to use a CMakePresets.json, into which you can put all of the cmake command line arguments. I will post an answer.
5

In general, command line arguments can be given by specifying cmake.configureArgs in the settings.json file, as shown by Jan Gabriel's answer.

But there is another setting (also in settings.json) specifically for -D arguments:

{
    "cmake.configureSettings": {
        "OPTIONA": "ON",
        "OPTIONB": "ON"
    }
}

See https://vector-of-bool.github.io/docs/vscode-cmake-tools/settings.html

Comments

2

Ideally, use a CMakePresets.json instead of mixing cmake configuration with general purpose configuration in your .vscode/settings.json

If you configure your VSCode + C++ project using cmake, you can configure all cmake configuration options in a file called CMakePresets.json.

This is preferable as it separates general purpose configuration which may be placed in .vscode/settings.json from cmake specific configuration which should be placed in its own configuration file.

This has the additional advantage that CMakePresets.json can be used with cmake via the command line directly to build the whole project in a single step.

Further information can be found by following the links below.

Disclaimer: I am not an expert in this area. I just happened to figure some relevant stuff out today using these docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.