Issue
I'm trying to follow this link on how to get started with c++ and vscode in ubuntu.
I have gcc already installed with the latest version.
Running sudo apt-get install build-essential gdb
gives:
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.8ubuntu1.1).
build-essential set to manually installed.
gdb is already the newest version (9.2-0ubuntu1~20.04.1).
gdb set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
However, when I get to the stage of creating the config file, I have no option for C/C++: g++ build active file
. I only have
So, I choose /usr/bin/cpp.Then I build the file, and get the success message. However, when run the newly created executable file, I get several error messages:
./helloworld: line 17: namespace: command not found
./helloworld: line 23: syntax error near unexpected token `('
./helloworld: line 23: ` typedef decltype(nullptr) nullptr_t;'
the strange thing is that the lines with code in the helloworld file end on line 16, so I think there's something wrong with the compiler...
Solution
Its best to get GCC working in your commandline, then get it working using VS Code tasks.
I suggest that you create the most simplistic project structure you can. Use only a project directory, and a single file named main.cpp
.
Something that looks like this:
PROJECT (dir) // path = ./
│
└──> main.cpp (file) // path = ./main.cpp
Once you have a directory with main.cpp
do 1 of 2 things:
- Use the following command to add a Hello World example to your
main.cpp
file.
$> echo -e "\n#include <iostream>\n\nusing namespace std;\n\nint main()\n{\n cout << \"Hello World\!\" << endl;\n}" > main.cpp
- Or copy and paste the code below into
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
}
FYI: You should be doing this from the command-line not vscode (not until you create the vscode task which I will show bellow)
Another thing to note, is your commandline should be pointed to your project directory, the directory you created with main.cpp
in it.
From inside your project directory execute the following command.
$> g++ ./main.cpp -o build
if your file compiled & built your executable correctly you should be able to use the ls
command to see a new file named build in your project directory.
If all went well, the new build
file is an executable. Execute it by entering...
$> ./build
and you should see "Hello World!"
At this point use the following command...
$> code .
VS Code should open to your projects directory.
Now using vscode create another directory, and name it ./.vscode
Then add a file to the ./.vscode
directory named tasks.json
The files full pathname will be: ./.vscode/tasks.json
then you will want to add the following to your tasks file
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "GCC: My Compilation Task",
"command": "/usr/bin/g++",
"args": ["-g", "main.cpp", "-o", "./build"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
then you should be able to press F1 and type RUN TASK
, when you see the option in the quick menu that says RUN TASK
click it, and then select the tasks with the same name as the label key in your tasks.json
file, which is "GCC: My Compilation Task"
Answered By - j D3V Answer Checked By - Terry (WPSolving Volunteer)