formulahendry / vscode-code-runner

Code Runner for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner
MIT License
2.21k stars 294 forks source link

code runner can't compile multiple source files #416

Open lemon07r opened 5 years ago

lemon07r commented 5 years ago

For example, here is a simplified version of the executor map for cpp files: "cpp": "g++ $fileName -o $fileNameWithoutExt"

$filename will only compile the current file, which doesnt work for projects with multiple source (.cpp) files. Is there a parameter that can be used in place of $filename to compile all .cpp files in the working/project directory?

formulahendry commented 5 years ago

Hi @lemon07r , I have plan to improve this. I am not an expert of C++ and need you insight here. If you change executor to g++ *.cpp -o $fileNameWithoutExt, will multiple source (*.cpp) files be compiled?

lemon07r commented 5 years ago

Hi @lemon07r , I have plan to improve this. I am not an expert of C++ and need you insight here. If you change executor to g++ *.cpp -o $fileNameWithoutExt, will multiple source (*.cpp) files be compiled?

This works for me. Took a bit of configuring to get this extension to work on windows with msys2's mingw64 toolchain.

A few notes, with default extension settings, attempting to compile would give me an unrecognized command error, even though a quick g++ -v command in the integrated terminal would show that I have gcc/g++ 8.2.1. I'm assuming this is cause the extension was trying to use the default msys subsystem to compile instead of the mingw64 subsystem. My integrated shell (msys2) is setup as such:

"terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",

"terminal.integrated.shellArgs.windows": [
     "--login",
 ], 
"terminal.integrated.env.windows": {
     "CHERE_INVOKING": "1",
     "MSYSTEM": "MINGW64",
 }

I was able to resolve this issue by setting the extension to run in terminal code-runner.runInTerminal": true,.

After this I started getting an error for too many arguments so I just simplified the executor setting to cpp": "g++ $fileName -o $fileNameWithoutExt, which worked but only compiled from a single source file. After trying your suggestion I have a working configuration using cpp": "g++ *.cpp -o $fileNameWithoutExt" which does compile with all the source files in the working directory.

This will only compile without running, so my crude method for getting it to run after compiling is: "cpp": "g++ *.cpp -o $fileNameWithoutExt && ./$fileNameWithoutExt.exe"

Just wondering, is there any way to compile in the integrated terminal but then run the compiled code in a new window/external terminal? EDIT: Nevermind, figured it out. "cpp": "g++ *.cpp -o $fileNameWithoutExt && start bash -c './$fileNameWithoutExt.exe'"