'C++ file name with space causes error in Visual Studio Code

When I write a C++ program with the file name Hello World.cpp in Visual Studio Code I got some errors:

#include<iostream>
using namespace std;
int main()
{
  cout<<"Hello World!!!"<<endl;

return 0;
}

Output:

g++.exe: error: Hello: No such file or directory
g++.exe: error: World.cpp: No such file or directory
g++.exe: error: World: No such file or directory
g++.exe: fatal error: no input files
compilation terminated.

But when I rename the file without space like HelloWorld.cpp, then I did not get any type of error. And I use the latest updated Visual Studio Code and MingW compiler.

But I wanted to use filename with space. How do I resolve this problem?



Solution 1:[1]

If you are using Windows, try configuring settings like this in your Setting.JSON

"code-runner.executorMap": {
    "cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt.exe\" && & \"$dir$fileNameWithoutExt.exe\""
}

Pay attention to the extra '&' character.

If you are using Unix based system, try this (I haven't tested, but I think this would work):

"code-runner.executorMap": {
    "cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt\" && \"$dir$fileNameWithoutExt\""
}

Solution 2:[2]

You can simply remove space from the filename to run the program like "Hello_world.cpp" or HelloWorld.cpp". Spaces in the file name are not allowed in vs code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Sinaga
Solution 2 Dev Verma