Setting CMake compilation options in VS Code - visual-studio-code

I recently started using VS Code and I have a question. How do I set the values of the compilation options defined in CMakeList.txt?
For example if the following option is set in the CMakeList.txt:
option(BUILD_WITH_TESTS "Build with tests." OFF)
Then I can set this value when building using for example the CMake GUI:
selecting option value
Does a similar toolkit exist in VS Code? What are the ways to set value BUILD_WITH_TESTS when building a project in VS Code?
At the moment I'm using VS Code with the plugin CMake Tools 1.5.3. But I do not find such functionality as a CMake GUI there.

You have several choices, depending on your exact requirements.
The CMake Tools extensions is a good choice, but it doesn't come with a GUI. If you want to use cmake-gui, you could still use it in your vscode CMake build directory, by executing cmake-gui <path_to_build_folder> in the vscode terminal.
In case you need to run this command often, you could customize your vscode settings, for a better cmake-gui integration, by using the Command Runner extension, which allows to run custom shell commands. After installation add the following to your settings.json:
"command-runner.commands": {
"cmake-gui": "cmake-gui ${workspaceFolder}/build"
}
and, if you like, a key binding to keybindings.json
{
"key": "ctrl+alt+1",
"command": "command-runner.run",
"args": { "command": "cmake-gui" }
}
Personally I would do none of the above methods, but just use ccmake in the terminal.
All of the above describes how to change settings in the current existing build directory (which is usually created by CMake Tools on first launch). However, the settings will get lost if you switch to a different compiler or delete the CMake cache in any other way.
To persist your settings and configure CMake by default with your preferred configuration, you can add your CMake arguments to your vscode workspace settings (in ${workspaceFolder}/.vscode/settings.json), e.g.
{
"cmake.configureArgs": [
"-DBUILD_WITH_TESTS=ON"
]
}

Related

Can't run `terminal.integrated.env` command in integrated terminal: "command not found"

Following this manual page:
https://code.visualstudio.com/docs/python/environments
I would have to set PYTHONPATH "through the terminal settings":
The PYTHONPATH environment variable specifies additional locations
where the Python interpreter should look for modules. In VS Code,
PYTHONPATH can be set through the terminal settings
(terminal.integrated.env.*) and/or within an .env file.
How do I do that? If I write (I ssh to a Linux server):
terminal.integrated.env.linux
I get "command not found".
It seems you tried to enter this
terminal.integrated.env.linux
into the terminal prompt itself, which treated it as a literal command.
What that guide on PYTHONPATH meant when it said "through the terminal settings" was Visual Studio Code's Integrated Terminal settings: https://code.visualstudio.com/docs/getstarted/settings. Specifically the settings under Terminal (filtered on terminal.integrated.env):
Depending on your OS/platform, you put a terminal.integrated.env.<os> block in your VS Code's settings.json file to specify the environment variables to inject when using VS Code's integrated terminal:
From https://code.visualstudio.com/docs/getstarted/settings#_default-settings:
// Object with environment variables that will be added to the
// VS Code process to be used by the terminal on Linux. Set to
// `null` to delete the environment variable.
"terminal.integrated.env.linux": {},
So if you are on Linux:
"terminal.integrated.env.linux": {
"PYTHONPATH": "<absolute path>"
},
Note: see the Python docs on PYTHONPATH on what exactly needs to be specified here.
Or, as the guide says, you can also specify a .env file.

Visual Studio Code is not recogonizing an Autopep8 installation

I use Visual Studio Code for developing in Django. I did all of the following inside a virtual environment.
Whenever I save the Python (.py) files, an alert pops up at the bottom right of the screen telling me: "Formatter autopep8 is not installed. Install?". It gives me three options - "Yes", "Use black, and "Use yapf".
If I click "Yes", it gives me another alert saying that there is no pip installer available in the selected environment. I then tried to go inside the integrated terminal and run pip install autopep8 and it says in the terminal that it was installed successfully. But when I save the Python files, it still gives me the same alert. Check the photo below.
Open your command palette with Shift + ⌘ + P. Type in Preferences: Open Workspace Settings. I'll share the settings I use with you, some of which may be of interest:
{
"python.pythonPath": "${workspaceFolder}/backend/env/bin/python3",
"python.venvPath": "${workspaceFolder}/backend/env",
"python.linting.flake8Enabled": true,
"python.linting.flake8Path": "flake8",
"python.linting.flake8Args": ["--ignore", "E501"],
"python.linting.pylintEnabled": true,
"python.linting.pylintPath": "pylint",
"python.linting.pylintArgs": ["--load-plugins", "pylint_django"]
}
python.pythonPath is the location of the Python interpreter executable within your virtual environment. In this case it's an environment named env in a folder named backend. *${workspaceFolder} is a reference to where your project lives.
python.venvPath is the folder of your virtual environment(s).
The remaining six key/values are for enabling and execution for flake8 and pylint. Experiment with either.
Note that these are the workspace settings and not the user settings. User settings are applied across all of your Visual Studio Code projects.

How to make the debugger use "make" in VSCode for C/C++ files. What parameters should I add/change in tasks.json or launch.json?

I had to use an external library which is not inbuilt in gcc, which needs linking manually every time I compile. So I changed some environment variables in order for it to work properly with make filename. But VSCode debugger uses gcc so it doesn't debug. So I want to change it to use make.

Add a custom command in Visual Studio Code Command Palette

Is it possible out of the box or using extensions to add a custom command in the Command Palette in Visual Studio Code like "External Tools" as in the IDE from JetBrains or in Visual Studio?
I would like to be able to run custom bash/cmd command directly from the Command Palette.
You can either use VS Code built-in functionality using shortcuts. Just add to keybindings.json:
{
"key": "cmd+shift+R",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "clear; rails server\u000D"
}
},
Or you can take a look at this extension: Command Runner
https://marketplace.visualstudio.com/items?itemName=usernamehw.commands
This extension can run it from custom Quick Pick (like command palette, but shows only your items). Command id is commands.openAsQuickPick
There's no api to seamlessly add commands to Command Palette #1422, but it's possible to modify package.json what that extension does when this setting is enabled:
"commands.populateCommandPalette": true,
With this setting it will not update Command Palette until the editor is reloaded. It might be an ok experience if you don't do that very often.
You can use multiCommand Extention to build your custom commands, which you can access through the Command Palette. Ctrl+Shift+P > Multi command > custom command.
I know it's not ideal, but I guess you can open multi command with a key binding and then it's almost what you want. Plus the feature that you can execute multiple commands with this extension.
This guy wrote something where you can customize the toolbar. https://github.com/AdamAnandUS/AdamsTool
Maybe add to it with a new StatusBarItem that registers a command you want to run.
https://code.visualstudio.com/docs/extensionAPI/vscode-api#commands.registerCommand
There are also many VS Code Extensions that might do what you want already. https://stackify.com/top-visual-studio-code-extensions/
Go to tools, External tools in visual Studio. Click Add, name the new command then you can point to a batch file command using the browse ellipses. When you save it, you will then see the new menu item under tools.

How do I set the "elm-format" path within VSCode?

How do I set the "elm-format" path within VSCode?
I receive the following error:
I then installed the executable:
How do I now point VSCode to the elm-format.exe path as the error suggests?
Do I add it to the Language Specific Settings for Elm below?
// Place your settings in this file to overwrite the default settings
{
"window.zoomLevel": 1,
"files.autoSave": "off",
"elm.formatOnSave": true,
"[elm]": {
}
}
The elm-format mechanism is pretty rudimentary, and works as follows:
1) install elm-format for your platform (from github, as you've indicated)
2) add it to your PATH environment variable (i.e. PATH environment variable of the operating system, nothing to do w/ VS Code, etc). The idea is that if you open a command prompt/shell and type elm-format the system should be able to find it
3) set elm.formatOnSave in VS Code (as you've already done)
Now when VS Code saves, if you've installed, say, the elm extension, it'll invoke the elm-format command line utility (installed in 1 above)