How can i compile C++ files through Visual Studio Code
I had this problem too when I switched to Ubuntu (I'm kind of new too, so I will try to be very clear I understand it's more complicated than Windows). I don't know your experience but, first of all, it is not like any Windows compiler (like devc, borland, etc) where it will output your values on a external console (unless you are debugging). So this are the steps to get a program to work:
-
You need to install g++
sudo apt-get install g++
and show the compiler where is the include route in a file namedc_cpp_properties.json
in the linux section, like this:"name": "Linux", "includePath": [ "/usr/include/c++/7", "/usr/include/x86_64-linux-gnu/c++/7", "/usr/include/c++/7/backward", "/usr/lib/gcc/x86_64-linux-gnu/7/include", "/usr/local/include", "/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed", "/usr/include/x86_64-linux-gnu", "/usr/include", "/usr/bin/g++", "${workspaceRoot}"
First select the folder you are going to work (it's going to be your workspace).
-
You need to build your program before you put it to work, so you need to create a "task" to do that, so you go to the command pallet (Ctrl+P) and type:
Tasks: Configure Default Build Tasks
That is going to create a
task.json
and you will have to pick an option (MsBuild, dotNet, Other, etc). So you are going to select "Other" and you will change some values like this:{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "echo", "type": "shell", "command": "g++", "args": [ "-g","Erlang_B_Correcto.cpp" ], "group": { "kind": "build", "isDefault": true } } ] }
Now press Ctrl+Shift+B
-
Now, if your program has no errors, is going to output something like this in the integrated terminal
Executing task in folder YourFolder: g++ -g YourProgram.cpp
and is going to create a file named
a.out
in your workspace folder, I recommend you download an extension namedCodeRunner
: it adds a play button on the right top corner that you only click and runs the program without debugging. The thing is, it can only output. I mean: if you are expecting the user to input values, you can't (As I said before, I'm also new so I'm not entirely sure you can't). -
Finally, if you want to debug, you need to configure that one
lauch.json
you uploaded in the images to something like this:{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "/home/carlosrpz26/Documents/C++ Programas/a.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
Pro tip: I would recommend a darker theme!
Related videos on Youtube
Roosevelt Mendieta
Updated on September 18, 2022Comments
-
Roosevelt Mendieta over 1 year
Quick run-down: I'm completely new to Ubuntu, but not new to C++: I've coded on a Windows machine before, but I switched to Ubuntu
Now I've installed VS code on Ubuntu as well as the C++ extension but when I even attempt to run a
helloworld.cpp
and debug my editor starts throwing up gang signs:Can anybody help me with this problem?
-
Roosevelt Mendieta almost 6 years@dsstorefile1 i added an imgur screenshot of what my editor gives me
-
-
Roosevelt Mendieta almost 6 yearsWow, thank you so much for the effort in this response. I'm having trouble though with one of the steps, I'm getting a fatal error in my terminal when i press ctrl+shift+b, I'll post a screengrab of my terminal. And ah a darker theme would be look good but it's very taxing on my eyes! imgur.com/a/vsCHusT edit: i just had to change the name of the .cpp file lol, i've got it running now!
-
Carlos Pérez almost 6 yearsI'm glad it helped! it can be rough sometimes, I wanted to give VSCode a try, I personally didn't like it, I'm looking for a windows like compiler where I just type the code and put it to work, instead of changing the name of the file I want to build (like VSCode). Anyways I had this thing that the program wouldn't beat me so I put some effort to get it done haha.