VSCode Use Code Runner plugin can not compile and run the file filename with spaces [How to Solve]

The most annoying thing about using Visual Studio Code to write C++ programs is that the Code Runner plug-in can’t compile and run files with spaces in the file name. This problem has troubled me for a long time. Although it does not affect my learning, too many separators always feel unpleasant. So I studied it carefully.
First create a test program called “hello world”, and then analyze the reason based on the G++ error report in English:

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.

No such file or directoryIt means that there is no such file or directory, fatal error: no input fileswhich means a fatal error: there is no input file, and then the compilation has been terminated. According to error, we found that the C ++ compiler is hello world.cppas the helloand world.cppthe two documents, my first reaction is the filename with spaces to be enclosed in double quotes. Go to the Code Runner plug-in page, click Settings -> Extended Settings.

After that, find Executor Map and click to edit in setting.json.

Find “cpp” and change it to:

"cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt.exe\" && \"$fileNameWithoutExt.exe\"",

Run hello world.cpp, the compilation is successful, but how to output the file name? I tested it in CMD again. It was able to compile and run the program. The problem was immediately locked in Powershell. I thought that the code of the CMD and Powershell running program must be different, so the failure occurred.

After searching for Baidu, I found out that Powershell should add the symbol (&) in front of it. This is called a call operation.

After adding &, an error message appears again:

It turned out to be added with “.”. The final compile and run code becomes:

"cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt.exe\" && & \".\\$fileNameWithoutExt.exe\"",

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *