4

I'm using VSCode to program in C++.
When I run the code (using the "Code Runner" extension), the .exe file stored in the same folder.
I've seen in many projects that they stored it in a /bin folder.
Is it posibble to automatically store it in there? (Also create the /bin folder if it doesn't exists).
Thank you!

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • You can change the exact command that the code runner executes: https://github.com/formulahendry/vscode-code-runner#configuration – UnholySheep Jan 13 '21 at 14:06

4 Answers4

8

I figured out 2 ways how to do your task:

  1. In this scenario you have to create bin folder manually

    So let's configure your VScode extension

    From extension page follow this step to open settings

    From extension page follow this step to open settings enter image description here

    In settings tab open "Edit in settings.json"

    enter image description here

    In your Json find "code-runner.executorMap"::cpp and insert the following command instead of the existing one

    cd $dir/bin && g++ ../$fileName -o $fileNameWithoutExt && $fileNameWithoutExt
    

    So it would look like this enter image description here

    P. S. This command works on Windows, and may not work on linux. I guess If the previous command does not work in linux, then try the next one

    cd $dir/bin && g++ ../$fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt
    

    And I think, if you're linux user you can automate everything with a command(but unfortunately I can't test it)

    cd $dir && mkdir -p bin && cd bin && g++ ../$fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt
    
  2. In this scenario you have to switch execution to integrated terminal(so output will change a little bit) by adding to your json following line

    "code-runner.runInTerminal": true
    

    and changing your cpp command to:

    for windows

    cd $dir && mkdir -Force bin > $null && cd bin && g++ ../$fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt
    

    and I guess for linux

    cd $dir && mkdir -p bin && cd bin && g++ ../$fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt
    

    So it would looks like this enter image description here

Deumaudit
  • 978
  • 7
  • 17
  • Tried this for Mac and it is as follows: "cpp": "cd $dir && mkdir -p bin && cd bin && g++ ../$fileName -o $fileNameWithoutExt && $dirbin/$fileNameWithoutExt", I hope it's the same in linux – zizutg Oct 22 '21 at 06:42
2

For those who use a /src and /bin separately, here's my solution:

change "cpp": under "code-runner.executorMap": to

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

This creates and runs the .exe from the bin folder within the parent.

1

If anbody is interested in separating executables to a \bin directory and running the code with external cmd.exe use this (code-runner.executorMap):

"c": "cd $dir && gcc $fileName -o .\\bin\\$fileNameWithoutExt && start cmd \"/k ; .\\bin\\$fileNameWithoutExt\""

This is for C but is easily modified for C++.

pmatkov
  • 144
  • 1
  • 5
0

To add to Deumaudit's answer for Linux users,

Create a bin folder in your working directory manually. Now in your settings.json file under "code-runner.executorMap"::cpp insert the following command instead of the existing one:

cd $dir && g++ $fileName -o $dir/bin/$fileNameWithoutExt && $dir/bin/$fileNameWithoutExt
Zardian
  • 1
  • 3