Code Runner using MSVC create a too long string - c++

I use CodeRunner for VSCode on Windows, so I need to change g++ to MSVC (Visual C++ Compiler).
So I configure settings.json for coderunner:
{
"window.zoomLevel": 0,
"code-runner.runInTerminal": true,
"terminal.integrated.shell.windows": "cmd.exe",
"code-runner.executorMap": {
"cpp": "vcvars64.bat && cl.exe $fileName && del $fileNameWithoutExt.obj && cls && $fileNameWithoutExt.exe",
},
"files.autoSave": "afterDelay"
}
As you can see, I add path of vcvars64.bat to system PATH.
It works, but after several runs I get next error:
Input line is too long.
I search for it and found that it is because CodeRunner run vcvars64.bat every time! So after several runs total path become too long:
"Input line is too long" error in BAT File
Restarting console clear it, but after several runs falls again.
Looks like I need to find some way to use vcvars64.bat only once but I don't know how!

This is my setting.json
"code-runner.executorMap": {
"cpp": "cd $dir && cl /EHsc $fileName && cls && $dir$fileNameWithoutExt.exe && del
$dir$fileNameWithoutExt.obj $dir$fileNameWithoutExt.exe",
},
And it's working as expected.

change default shell to PowerShell in vs-code
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
then add this in setting.json file :
For PowerShell
"cpp": "cd $dir ; vcvars64.bat ; cl /EHsc $fileName ; ./$fileNameWithoutExt.exe",

Related

VSCode ignores cpp standard when folder is opened in WSL: Ubuntu?

When I open a folder with WSL: Ubuntu and then try to build the project it seems to ignore the cpp standard set in VSCode's User settings. I can choose the standard I want by configuring a task.json file however I want to know how to change the default cpp standard for any folder opened in VSCode with WSL: Ubuntu. I have tried adding the following in my User settings in VSCode.
...
"C_Cpp.default.cppStandard": "c++17",
...
"code-runner.executorMap": {
...
"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
...
},
And yet when I build the following and execute the output I get 201402 indicating it is using c++14.
#include <iostream>
int main()
{
std::cout << __cplusplus << std::endl;
return 0;
}
Any suggestions on how to change the default c++ standard?
It seems that if I open the file in windows then it works fine as well so I don't know what the difference is here?

Sublime Text 3 intel oneAPI Fortran build system

Has anyone figured out how to write the build system for the oneAPI Fortran compiler?
Previously, i was using Parallel Studio XE ifort, and i managed to get it working using the solution here:
{
"cmd": ["cmd", "/e:on", "/v:on", "/k", "ipsxe-comp-vars intel64 vs2013 && ifort ${file}"],
"file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",
"working_dir":"${file_path}",
"selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
"encoding":"cp936",
"path":"C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.4.210\\windows\\bin;${path}",
"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/e:on", "/v:on", "/c", "ipsxe-comp-vars intel64 vs2013 && ifort ${file} && ${file_base_name}"]
}
]
}
I tried changing the paths to the new ones but it doesn't work. I get the following error:
"ipsxe-comp-vars" is not recognized as an internal or external command,
program o executable.
I found the answer. Explanation below. Posting the working build system here for visibility.
This should be the build system:
{
"cmd": ["cmd", "/e:on", "/v:on", "/S", "/k", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file}"],
"file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",
"working_dir":"${file_path}",
"selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
"encoding":"cp936",
"path":"C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\bin\\intel64;${path}",
"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/e:on", "/v:on", "/s", "/c", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file} && ${file_base_name}"]
}
]
}
Why the problem happens
For starters, ipsxe-comp-vars is a batch file which when run, sets up environment variables required to execute the intel compilers. This file is specific to Intel Parallel Studio XE (IPSXE). Now, when installing IPSXE, it would add this batch file to your PATH, meaning you could simply call ipsxe-comp-vars from any directory to set up the required environment variables.
Intel oneAPI has a differently named file, that essentially does the same thing, called setvars.bat. This file is stored in:
C:\Program Files (x86)\Intel\oneAPI\setvars.bat
So, at first it seems that calling ipsxe-comp-vars fails because the file is named differently. However, unlike IPSXE did with ipsxe-comp-vars, oneAPI does not add setvars to PATH, so you cannot simply call setvars, you have to usethe full path.
How to solve it
With IPSXE, you could call ipsxe-comp-vars and it would run the batch file that sets up environment variables, but with oneAPI either you add the file to PATH (not reccomended because it has a generic name), or you use the full path when calling it (same as above):
C:\Program Files (x86)\Intel\oneAPI\setvars.bat
Now, because you have to plug this in into the build system config, you need to format it correctly. ST runs the commands in a cmd.exe, so you have to use the correct options and format the path in a way that cmd can understand it:
options (you can get a full list by opening a cmd prompt, typing cmd /? and hitting return):
- /e:on Enables command extensions
- /v:on Enables extension of environment variables
- /s Modifies how the string following a /c or /k is read
- /k Executes the string command and continues
The path to the setvars.bat file must be formatted as follows:
C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat
Each \ separating dirs needs to be escaped (using \ as well)
needs to be enclosed in double quotes, since it contains a whitespace. Each double quote needs to escaped as well (once again with )
The following options are specific to the setvars.bat file:
- intel64 specifies 64-bit configuration
- vs2022 specifies Visual Studio 2022 as the developer cmd or
powershell version to use
Finally, ifort is called on the current file with ifort ${file}
Additionally, the build system is completed with a variant "Run". This variant runs the output file once it has been compiled(&& ${file_base_name}), and will show the output in the Sublime Text 3 console (does not accept inputs, if anyone knows how to setup up sublimeREPL for Fortran please tell me)

How can I write a shortcut to compile and run a program in a separate terminal window in Vim?

I have this as a Sublime Text build system right now: It compiles a C++ program, then opens a new window in Terminal.app and runs it there upon pressing ctrl+b.
{
"shell_cmd": "g++-11 -std=c++20 '${file}' -o '${file_base_name}' && echo 'cd \"${file_path}/\"' > '/tmp/${file_base_name}' && echo './\"${file_base_name}\"' >> '/tmp/${file_base_name}' && echo read >> '/tmp/${file_base_name}' && chmod +x '/tmp/${file_base_name}' && open -a Terminal.app '/tmp/${file_base_name}'"
}
However, I'm not sure how I could get something in (Mac)Vim. I've read some similar questions, but none of the ones I've seen mention opening in a separate terminal.
I've tried writing some shortcuts/commands to compile and run within Vim which mostly worked, but I would still rather my programs run in a separate window (edited).
noremap <C-b> :!g++-11 %:p && ./a.out<CR>
You actually have a few options, so use whatever suits your needs the most.
You can use vim's built in terminal :terminal (or :term; see :help :terminal), which is probably the easier way:
:term g++-11 %:p && ./a.out<CR>
Or you can use :compiler with :make (see :help :compile and :help 'makeprg'):
:compiler gcc
:let $CXXFLAGS='-std=c++20 -Wall -Werror'
:make
Wish this plugin in neovim can help you: https://github.com/michaelb/sniprun

Configure VS Code code runner extension to display CPP warnings

I am wondering how I could configure the code runner extension on VS Code (MacOS 10.15) to display warnings (i.e. -Wall?)
Would there be a simple, almost permanent method so code runner will always display warnings no matter which CPP file I build?
Yes! This is possible.
Go to the Workspace Settings, search for code runner and click on Executor Map:
You may then use the following JSON code:
{
"code-runner.executorMap": {
// ...
"c": "cd $dir && gcc $fileName -Wall -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
} // used -Wall here
}
Save it and quit from the file and jump to the code you want to run. Press Ctrl+Alt+J to select the run type. Select C simply and you're good to go.

Sublime Text 2 Run built file after building

So I'm new to this but I am using Sublime Text 2 on Mac OS X and the problem is that when i simply run my c++ code it runs in a local output tab and it dont allow me to do any input, so to input variables i need to build my code and then locate it in finder and open in terminal, but is there a way to allow input in that little output windows or simply tell ST2 to open the built file just after building it?
P.S.
I found the answear to my own question :D
I will add it so if someone needs it - it will be here.
so you need to eddit c++.sublime-build file - just add this
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
before
"variants":
and the when you build your programm it will just open it in terminal