Microsoft's Visual Studio Code editor is quite nice, but it has no default support for building C++ projects.
How do I configure it to do this?
Microsoft's Visual Studio Code editor is quite nice, but it has no default support for building C++ projects.
How do I configure it to do this?
With an updated VS Code you can do it in the following manner:
Hit (Ctrl+P) and type:
Open a folder (Ctrl+K & Ctrl+O) and create a new file inside the folder with the extension .cpp (ex: hello.cpp):
Type in your code and hit save.
Hit (Ctrl+Shift+P and type,
Configure task runner
and then selectother
at the bottom of the list.Create a batch file in the same folder with the name build.bat and include the following code to the body of the file:
Edit the task.json file as follows and save it:
Hit (Ctrl+Shift+B to run Build task. This will create the .obj and .exe files for the project.
For debugging the project, Hit F5 and select C++(Windows).
In launch.json file, edit the following line and save the file:
Hit F5.
There is a much easier way to compile and run C++ code, no configuration needed:
Ctrl+Alt+N
, or pressF1
and then select/typeRun Code
, or right click the Text Editor and then clickRun Code
in context menu, the code will be compiled and run, and the output will be shown in the Output Window.Moreover you could update the config in settings.json using different C++ compilers as you want, the default config for C++ is as below:
Out of frustration at the lack of clear documentation, I've created a Mac project on github that should just work (both building and debugging):
vscode-mac-c-example
Note that it requires XCode and the VSCode Microsoft cpptools extension.
I plan to do the same for Windows and linux (unless Microsoft write decent documentation first...).
If your project has a CMake configuration it's pretty straight forward to setup VSCode, e.g. setup
tasks.json
like below:This assumes that there is a folder
build
in the root of the workspace with a CMake configuration.There's also a CMake integration extension that adds a "CMake build" command to VScode.
PS! The
problemMatcher
is setup forclang
-builds. To use GCC I believe you need to changefileLocation
torelative
, but I haven't tested this.Here is how I configured my VS for C++ using g++ compiler and it works great including debugging options:
tasks.json file
launch.json file
I also have 'C/C++ for Visual Studio Code' extension installed in VS Code
You can reference to this latest gist having a version
2.0.0
task for Visual Studio Code, https://gist.github.com/akanshgulati/56b4d469523ec0acd9f6f59918a9e454You can easily compile and run each file without updating the task. It's generic and also opens the terminal for input entries.