I'm having problem trying to stop Sublime Text 2 from build on save. I had previously installed SaveAllOnBuild plugin but I removed it. I have also made sure that Tools -> Save All on Build is turned off. In addition, I also checked my keyboard mapping, and Cmd+S is not listed as the keyboard binding
This is the snippet from the default binding file. User binding file is empty
{ "keys": ["f7"], "command": "build" },
{ "keys": ["super+b"], "command": "build" },
{ "keys": ["super+shift+b"], "command": "build", "args": {"variant": "Run"} },
I don't understand why Sublime keeps build on save. Any information would be nice. Thanks.
Related
I am using Visual Studio Code with MinGW-w64. This involves creating two files (launch.json and tasks.json) that allow me to build and debug my C++ code. I select my tasks.json file by going to Terminal --> Configure Default Build Task... and then selecting "C/C++: g++.exe build active file."
Normally, the tasks.json file appears as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "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": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
However, in the last few days, it has appeared as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"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": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
When the file is in this format, I'm not able to debug my code. Instead, I get the message "Could not find the task 'g++.exe build active file'.
The only apparent difference is that C/C++is now preceding g++.exe build active file in the "label" line. Once I delete this label, I'm able to get the code to compile and/or debug.
This leads me to ask two questions: first, why is C/C++ being inserted in the label? It doesn't appear in the Microsoft documentation for setting up MinGW with Visual Studio Code: https://code.visualstudio.com/docs/cpp/config-mingw
Secondly, how am I able to permanently remove C/C++ from my label so that I don't have to manually delete it each time? I have tried choosing Configure Task next to C/C++:g++.exe build active file in the Configure Default Build Tasks in the dropdown menu; removing C/C++, and then saving the tasks.json file, but this doesn't appear to cause the build task to change permanently.
Thank you as always for your help.
It took me a while to read the source code of the cpptools extension, and then I found some problems. Under different conditions, the extension has different response
If there is ".vscode" folder in the folder root directory, then when adding the debug configuration, it will only guide the creation of launch.json.The user needs to be guided to configure "tasks.json" again.But the name of the task created at this time is not consistent with value of "preLaunchTask". This is a problem with this extension. I think the reason is that the ".vscode" folder was not found when initializing the debug configuration, some conditions are missing when the extension's code is executed
If there is a empty ".vscode" folder in the root directory of the folder, "launch.json" and "tasks.json" will be created at the same time when initializing the debug configuration.The two files are exactly matched and do not need any modification
If the user configures the task as first.When initializing the debug configuration, the extension will recreate a task called "g++.exe build active file" which is not start with "C/C++".This of course works, but you may need to delete the task created at the beginning whichhas not been used
This design is reasonable because "C/C++: g++.exe build active file" is actually a task template built into cpptools (there are two others). The built-in templates can be referenced by "preLaunchTask", but the user cannot see them in "tasks.json"
If you delete the file "tasks.json" and change the value of "preLaunchTask" to "C:C++: g++.exe build active file".It also works,but this is not a standard usage
I think this is a mistake of them, it started to appear after a recent version
I have this code in VSCode:
{
"version": "2.0.0",
"tasks": [
{
"taskName": "build first program",
"type": "shell",
"command": "g++",
"args": [
"-g", "firstprogram.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
which is pretty much the exact same code in the official guide to debugging, but it says that g++ is not recognized as a cmdlet, function, script file, or operable program. Pretty much all of the rest of my code is copied from the official thing, aside from code specific to my program, and I have the MinGW c++ compiler.
You need to add the location of the g++ executable to your computer's $PATH Environment Variable.
To do this:
Open up the start menu and search for "Environment Variables".
Click on the "Environment Variables" button at the bottom
Under "System Variables", click on "Path" and then click on "Edit"
Click on "New", and then type the path to your MinGW installation's bin directory. From what you have told me in the comments I believe yours is C:/Users/21nalex/Documents/MinGW/bin
I have a tasks.json script that currently compiles the code
{
"version": "0.1.0",
"command": "gcc",
"isShellCommand": true,
"args": ["-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
"echoCommand": true,
"showOutput": "always",
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
This works fine, but when I want to run the file I have to run the exe from command line. Is it possible to do this in the task as well? So if it finishes building succesfully it then runs a different task?
If anyone else comes across this when searching like I did, you can now set the property preLaunchTask in your launch.json to your build task's name property and it will run before your launch.
For Example
"name": "Debug (gdb) Launch",
"preLaunchTask": "Build All",
Will run the "name": "Builld All" in your tasks.json before launching your program.
You can read the information on this on the Debugging in Visual Code docs page.
You can configure multiple tasks in Visual Studio Code, one of which will allow you to build your executable, and the other will run your executable.
Optionally, you could also look into Visual Studio Code's "Run Mode" (see here). If you use "Run Mode", you should be able to configure Visual Studio Code to build your executable, and then launch it.
I'm not extremely familiar with "Run Mode", thus I will detail how to define multiple tasks to achieve a similar result.
Disclaimer: Visual Studio Code does not support tasks that use different shell commands (see here).
That's right. At its current state, Visual Studio Code doesn't have "native" support for defining tasks that use different shell commands.
Disclaimer: Visual Studio Code's task-output pane will not allow you to pass input to your program interactively.
If your program relies on user-input (for example, from stdin), you're probably better off not using Visual Studio Code to run your executable.
Basically, what we'll need to do, is define two tasks, one of which will be a build task, the other will be our launch task.
Seeing as Visual Studio Code doesn't have great support for defining multiple tasks that each use different shell commands, we'll need to change our tasks.json's "command" property to cmd (or sh, if on Linux/macOS). We'll also need to set the "args" property to [/C] ([-c] if on Linux/macOS).
The reason behind us doing this, is because we want each of the tasks we're about to define, to be passed as arguments to a new shell instance.
The next step, is to define our build and launch tasks. When we do so, we'll need to make sure we place the command we want to run, as a task argument. For example:
{
"taskName": "build",
"args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"]
}
Finally, what we'll do, is add the "isBuildCommand" property to our build task (and make sure it's true), as well as add the "isTestCommand" property to our launch task (and, again, make sure it's true).
After all of that, our tasks.json file could look something like this:
{
"version": "0.1.0",
"command": "cmd",
"args": ["/C"],
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
{
"taskName": "build",
"args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
"isBuildCommand": true
},
{
"taskName": "run",
"args": ["${relativeFile}.exe"],
"isTestCommand": true
}
]
}
Note: If placing each task argument in their own string within the args array doesn't work, you can also try placing all of the arguments in a single string within the args array. Example:
["gcc -Wall ${relativeFile} -o ${relativeFile}.exe -pedantic"]
Note: If you would like to be able to invoke your task(s) via keyboard shortcuts, you have the "workbench.action.tasks.build" and "workbench.action.tasks.test" editor commands at your disposal.
If you need an example of binding keys to those commands, here's an example of how I have them mapped in my keybindings.json file:
[
{
"key": "f6",
"command": "workbench.action.tasks.build"
},
{
"key": "f7",
"command": "workbench.action.tasks.test"
}
}
Edit: You probably only need to define a keyboard shortcut for the test task, as the build task probably already has one defined. Check here before you take the time to define a different keyboard shortcut.
You can create a task for build and as the arguments of it you can pass the commands for running. As an example the task I use to compile and run c++ is shown below.
{
"version": "2.0.0",
"tasks": [
{
"label": "g++ build and run",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"out.exe",
"\"${file}\"",
"&&",
"./out.exe",
"<",
"input.in",
">",
"output.out"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In the above task I compile my source file (the name of the file could be anyone here as ${file} is used) into out.exe and the run out.exe while getting input from input.in and outputting the output to output.out.
I've made a custom build in sublime which runs my program and it works fine. But I have the Makefile build as default. Is there a way, to let Ctrl+B use the default build (in my case Makefile) and have another shortcut (Ctrl+Shift+B) use another build. If yes, how?
I tried using this :
[
{ "keys": ["ctrl+shift+b"], "command": "build buildName" }
]
but it isn't working and watching at the sublime documentation this command is only for the default build selected.
Thanks in advance.
The actual syntax of the keybinding should be like so:
{ "keys": ["f1"], "command": "build", "args": {"build_system": "Packages/Python/Python3.sublime-build"} },
I would actually recommend against using CtrlShiftB as your custom keybinding, as it is already applied to the "Build With" command.
I'm working with JavaScript projects using nodejs and visual code editor. I wonder is it possible to configure such a great code editor for C++ projects.
I want to link the debugger and make some hotkeys for building the debug/release versions of project.
Is it possible for C++ projects and what should I do/read for it?
I want to link the debugger
This is currently not possible until there is a public extension API available. I expect it to come in November or December this year.
I want to [...] make some hotkeys for building the debug/release versions of project.
You can do it right now if there is only one project you want to compile in your workspace.
This is how to do it:
Open the root folder of your project in VSCode (this is your workspace)
Place a batch/shell script in the workspace that accepts a parameter with a value of release/debug and compiles the project in release or debug mode depending on the passed parameter value
In case there is no .vscode directory in the workspace then create it on your own
Add a file tasks.json to that folder having this content:
{
"version": "0.1.0",
"command": "${workspaceRoot}/CompileProject.bat",
"tasks": [
{
"taskName": "Compile debug build",
"args": [
"debug"
],
"isTestCommand": true
},
{
"taskName": "Compile release build",
"args": [
"release"
],
"isBuildCommand": true
}
]
}
You can trigger Compile debug build with CTRL + Shift + T and Compile release build with CTRL + Shift + B.
You can change the keybindings by going to File -> Preferences -> Keyboard Shortcuts and define your preferred shortcuts for the commands workbench.action.tasks.test and workbench.action.tasks.build.
Example:
[
{ "key": "f5", "command": "workbench.action.tasks.test" },
{ "key": "f6", "command": "workbench.action.tasks.build" }
]
Use the following in the tasks.json file, changing "helloworld" strings as appropriate.
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
{
"version": "0.1.0",
"command": "gcc",
"args": ["-Wall", "helloWorld.c", "-o", "helloWorld"],
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
EDIT: This requires gcc to be available on the path. The build can be triggered with Ctrl + shift + b.
Debugger is not available yet AFAIK