vscode-code-runner: C/C++: Can't compile or run when source file path has space(s)

I created a file D:\Program\USACO\[C2, S3.2] Magic Square.cpp

But when I compile, some errors occurred:

[Running] cd "d:\Program\USACO\" && g++ [C2, S3.2] Magic Square.cpp -o [C2, S3.2] Magic Square && "d:\Program\USACO\"[C2, S3.2] Magic Square
g++: error: [C2,: No such file or directory
g++: error: S3.2]: No such file or directory
g++: error: Magic: No such file or directory
g++: error: Square.cpp: No such file or directory
g++: error: S3.2]: No such file or directory
g++: error: Magic: No such file or directory
g++: error: Square: No such file or directory
g++: fatal error: no input files
compilation terminated.

OS: Win7 SP1 VSCode: 1.19.3 Runner: 0.8.7 5beeedfa674b330537448162dfe9c2330c7eda50

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 2
  • Comments: 29 (1 by maintainers)

Most upvoted comments

If you are using Windows, try configuring settings like this:

"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\""
}

@tripathi-abhishek use this

In case you run into issues with a bash error like “bash: cd: too many arguments” 2020-11-07 (5)

Fixed by:- Go to visual studio code settings for code runner and replace cpp key value pair in code-runner.executorMap with the following :- “code-runner.executorMap”: { “cpp”: “g++ $fileName -o $fileNameWithoutExt.exe && ‘./$fileNameWithoutExt.exe’” }

Screenshot:- replace your cpp key value pair with the below mentioned code in “code-runner.executorMap” like:- 2020-11-07 (3)

hello there… i am not able to find code-runner,executorMap anywhere where exactly do i paste this piece of code … in settings.json ? … it did not work be pasting the code there … 😦

Yes in settings.json.

I finally used this command instead:

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

Here is extract of my settings.json :

    "c-cpp-compile-run.cpp-compiler": "C:\\MinGW\\bin",
    "window.zoomLevel": -1,
    "code-runner.executorMap": {
        //"javascript": "node",
        //"php": "C:\\php\\php.exe",
        //"python": "python",
        //"perl": "perl",
        //"ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
        //"go": "go run",
        //"html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
        //"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        //"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    	"cpp": "cd $dir ; g++ $fileName -o $fileNameWithoutExt.exe ; .\\$fileNameWithoutExt.exe"
    
    },
    "C_Cpp.default.cppStandard": "c++20",
    "C_Cpp.default.cStandard": "c11",

Hello, I have the same problem. I’m using Windows, I’m trying to run a simple C++ program, I don’t have spaces anywhere in the path or file name.

[Running] cd "c:\Users\Guillaume\Desktop\" && g++ test.cpp -o test && "c:\Users\Guillaume\Desktop\"test
g++: error: test.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.

If I copy the command as is, and paste it in a cmd.exe then it works without problem 😕

I made changes as suggested by others but nothing worked.

EDIT: Solved! Here is how, maybe it will help others :

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

Windows Guys! Just Copy Past This to your settings.json file

I did a lot of research and this will surely work

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

Hello!

I have a same problem in Mac OS X. Some solution to this bug? If I rename Example 1.cpp to Example1.cpp or Example_1.cpp then, it works ok…

Thank you!

@hanghang0702 sorry I’m late, @Michany you forgot the escape characters, @formulahendry it isn’t the problem on *nix based system the executable file runs with ./"dir"filename ,

"code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir && javac \"$fileName\" && java \"./$fileNameWithoutExt\"",
        "c": "cd $dir && gcc \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",
        "cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",
        "objective-c": "cd $dir && gcc -framework Cocoa \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",}

I’m not a windows user & I’m not sure if it’s going to work on windows (you’ll probably need to adjust/change some escape characters or something similar). I really hope they edit & fix it in the upcoming version(s).

For windows in settings .json change cpp to the following:

“code-runner.executorMap”: { “cpp”: “g++ $fileName -o $fileNameWithoutExt.exe && ‘./$fileNameWithoutExt.exe’”, }

this works for me

If you are using Windows, try configuring settings like this:

"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\""
}

this worked for me. I Am now able to compile and run c cpp and java files(‘&’ is not needed in java though if anyone is wondering)