vscode-code-runner icon indicating copy to clipboard operation
vscode-code-runner copied to clipboard

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

Open stdcall0 opened this issue 7 years ago • 29 comments

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

stdcall0 avatar Feb 01 '18 08:02 stdcall0

Same problem. After modifying the configuration as follows:

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

I can run the first two command successfully, but it won't work for the last command ($dir$fileNameWithoutExt).

Here is the output. image

Michany avatar Feb 19 '18 09:02 Michany

@Michany Try changing $dir$fileNameWithoutExt to $fileNameWithoutExt?

formulahendry avatar Feb 24 '18 14:02 formulahendry

@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).

hwez avatar Feb 27 '18 17:02 hwez

Same issue here.

When I run C code in bash terminal. I get this: bash: cd: d:\VscodeProject\C\demo" && gcc file1.c -o file1 && d:VscodeProjectCdemo"file1: No such file or directory Obviously, the last command is not recognised correctly. How to fix it?

waithope avatar Apr 04 '18 12:04 waithope

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!

jesusdeveloper avatar Apr 20 '18 13:04 jesusdeveloper

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

Yuki-Nagato avatar Jul 14 '18 05:07 Yuki-Nagato

image

alan23273850 avatar Aug 28 '18 07:08 alan23273850

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

guillaume-dorczynski avatar May 05 '19 14:05 guillaume-dorczynski

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 ... :(

sanket-valani avatar Oct 03 '19 12:10 sanket-valani

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",

guillaume-dorczynski avatar Oct 03 '19 13:10 guillaume-dorczynski

I believe I am also having this issue. Are there any plans on resolving it? Here are my results: $ cd "c:\Users\Jacob Armiger\Documents\cpp_files\" && g++ hello.cpp -o hello && "c:\Users\Jacob Armiger\Documents\cpp_files\"hello bash: cd: too many arguments

jacob-armiger avatar Nov 07 '19 22:11 jacob-armiger

Hello, after trying a lot of time I found the solution, hope this will help others

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

mirror-source avatar Feb 26 '20 13:02 mirror-source

Hello, after trying a lot of time I found the solution, hope this will help others

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

Where to add this?? IN settings.json file in .vscode?? or somewhere else?

lakshits11 avatar Apr 27 '20 04:04 lakshits11

image

-->Yeah you need to copy that on your settings.json (exactly in your executor map) on vscode, i’m going to attach a photo  

mirror-source avatar Apr 27 '20 04:04 mirror-source

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

kaushik-rishi avatar Jun 05 '20 09:06 kaushik-rishi

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)

arpandutta0000 avatar Oct 15 '20 04:10 arpandutta0000

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 solution works!! Finally my c program could be compiled and run. Thanks!

jamestansx avatar Oct 23 '20 02:10 jamestansx

It really helped, Thank You! I am on Windows

HeyJayant avatar Oct 24 '20 13:10 HeyJayant

PLEASE HELP!!!😭 I'm getting the same error.

{
    "atomKeymap.promptV3Features": true,
    "editor.multiCursorModifier": "ctrlCmd",
    "editor.formatOnPaste": true,
    "python.pythonPath": "C:\\Users\\ABHISHEK\\anaconda3\\python.exe",
    "java.home": "C:\\Program Files\\Java\\jdk-12.0.2",
    "java.help.firstView": "gettingStarted",
    "workbench.iconTheme": "vscode-icons",
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "java.semanticHighlighting.enabled": true,
    "files.exclude": {
        "**/.classpath": true,
        "**/.project": true,
        "**/.settings": true,
        "**/.factorypath": true
    },
    "python.jediEnabled": false,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
    "python.languageServer": "Microsoft",
    "files.associations": {
        "*.cpp": "cpp"
    },
    "C_Cpp.updateChannel": "Insiders",
    "code-runner.executorMap": {
    

        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt.exe\" && & \"$dir$fileNameWithoutExt.exe\"",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir $fileNameWithoutExt",
        "php": "php",
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css"
    },
    "window.zoomLevel": 0,
    "files.autoSave": "onFocusChange",
    "diffEditor.ignoreTrimWhitespace": false,
    "[cpp]": {
    
        "editor.wordBasedSuggestions": false,
        "editor.suggest.insertMode": "replace",
        "editor.semanticHighlighting.enabled": true
    },
    "code-runner.runInTerminal": true,
    "code-runner.saveAllFilesBeforeRun": true,
    "settingsSync.ignoredSettings": [
        "-java.configuration.runtimes"
    ],
    "liveServer.settings.donotShowInfoMsg": true,
    "java.refactor.renameFromFileExplorer": "autoApply",
    "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[json]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "eslint.codeAction.showDocumentation": {
    
        "enable": true
    },
    "git.autofetch": true,
    "vsicons.dontShowNewVersionMessage": true,
    
}

This and still I'm getting the same error.

2020-11-04_020713

And my environment variables :

2020-11-04_020930

And just in case if anyone has doubts about my files and such...

2020-11-04_021239 @formulahendry @HeyJayant @kaushik-rishi

tripathi-abhishek avatar Nov 03 '20 20:11 tripathi-abhishek

@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)

VipulKhandelwal1999 avatar Nov 07 '20 06:11 VipulKhandelwal1999

@VipulKhandelwal1999 thanks this worked!

tripathi-abhishek avatar Nov 07 '20 08:11 tripathi-abhishek

image

ushakhanal avatar Jan 01 '21 17:01 ushakhanal

@ushakhanal read my comments. I have already explained what to do in case of "too many arguments" error.

tripathi-abhishek avatar Jan 01 '21 17:01 tripathi-abhishek

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

"code-runner.executorMap": { "cpp": "g++ $fileName -o $fileNameWithoutExt.exe && './$fileNameWithoutExt.exe'", }

gagan24 avatar Jun 05 '21 16:06 gagan24

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

nalin88 avatar Jun 27 '21 13:06 nalin88

Thank you very much, I was using the same .json as you suggested.

HeyJayant avatar Jun 29 '21 10:06 HeyJayant

Simple open the code runner configuration by search in settings code runner and under extensions selecting it then scroll and click on edit setting.json change the cpp configuration as "code-runner.executorMap": { "cpp": "cd $dir && g++ "$fileName" -o "$fileNameWithoutExt.exe" && "$fileNameWithoutExt.exe"" }

Rbansal89 avatar Aug 09 '21 17:08 Rbansal89

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",

Thank you. Your executorMap worked for my cpp files in code runner but I copied the same executorMap for c programs and it isnt working.

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

I am getting this error

PS C:\Users\jithin_d_mathew\Desktop\stufff\CC++> cd "c:\Users\jithin_d_mathew\Desktop\stufff\CC++\" && gcc jhbuhb.c -o jhbuhb && "c:\Users\jithin_d_mathew\Desktop\stufff\CC++\"jhbuhb
At line:1 char:52
+ cd "c:\Users\jithin_d_mathew\Desktop\stufff\CC++\" && gcc jhbuhb.c -o ...
+                                                    ~~
The token '&&' is not a valid statement separator in this version.
At line:1 char:78
+ ... n_d_mathew\Desktop\stufff\CC++\" && gcc jhbuhb.c -o jhbuhb && "c:\Use ...
+                                                                ~~
The token '&&' is not a valid statement separator in this version.
At line:1 char:81
+ ... .c -o jhbuhb && "c:\Users\jithin_d_mathew\Desktop\stufff\CC++\"jhbuhb
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
At line:1 char:128
+ ... .c -o jhbuhb && "c:\Users\jithin_d_mathew\Desktop\stufff\CC++\"jhbuhb
+                                                                    ~~~~~~
Unexpected token 'jhbuhb' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidEndOfLine

EDIT

I managed to do it for C with this

"c": "cd $dir ; gcc $fileName -o $fileNameWithoutExt.exe ; .\\$fileNameWithoutExt.exe",

jithindmathew avatar Sep 10 '21 14:09 jithindmathew

It works for me!

		"c": "gcc $fullFileName -o ./out/$fileNameWithoutExt && ./out/$fileNameWithoutExt",
		"cpp": "g++ $fullFileName -o ./out/$fileNameWithoutExt && ./out/$fileNameWithoutExt",

miku3920 avatar Apr 18 '22 18:04 miku3920