Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stdcall0 opened this issue Feb 1, 2018 · 29 comments
Open

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

stdcall0 opened this issue Feb 1, 2018 · 29 comments
Labels

Comments

@stdcall0
Copy link

stdcall0 commented Feb 1, 2018

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 5beeedf

@Michany
Copy link

Michany commented Feb 19, 2018

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

@formulahendry
Copy link
Owner

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

@hwez
Copy link

hwez commented Feb 27, 2018

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

@waithope
Copy link

waithope commented Apr 4, 2018

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?

@jesusdeveloper
Copy link

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!

@Yuki-Nagato
Copy link

Yuki-Nagato commented Jul 14, 2018

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

@alan23273850
Copy link

image

@guillaume-dorczynski
Copy link

guillaume-dorczynski commented May 5, 2019

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

@sanket-valani
Copy link

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

@guillaume-dorczynski
Copy link

guillaume-dorczynski commented Oct 3, 2019

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

@jacob-armiger
Copy link

jacob-armiger commented Nov 7, 2019

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

@mirror-source
Copy link

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

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

@lakshits11
Copy link

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?

@mirror-source
Copy link

mirror-source commented Apr 27, 2020 via email

@kaushik-rishi
Copy link

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

@arpandutta0000
Copy link

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)

@jamestansx
Copy link

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!

@HeyJayant
Copy link

HeyJayant commented Oct 24, 2020

It really helped, Thank You!
I am on Windows

@tripathi-abhishek
Copy link

tripathi-abhishek commented Nov 3, 2020

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

@VipulKhandelwal1999
Copy link

VipulKhandelwal1999 commented Nov 7, 2020

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

@tripathi-abhishek
Copy link

@VipulKhandelwal1999 thanks this worked!

@ushakhanal
Copy link

image

@tripathi-abhishek
Copy link

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

@gagan24
Copy link

gagan24 commented Jun 5, 2021

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

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

@nalin88
Copy link

nalin88 commented Jun 27, 2021

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

@HeyJayant
Copy link

HeyJayant commented Jun 29, 2021

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

@Rbansal89
Copy link

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

@jithindmathew
Copy link

jithindmathew commented Sep 10, 2021

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

@miku3920
Copy link

It works for me!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests