Can't build even a simple program in Sublime Text 3 - c++

I have just switched from Python to C++ for implementing Data Structures and Algorithms. I found that Sublime Text 3 was quiet powerful. I installed it, added my Mingw-64 compiler to the path and also added a "build system". I hoped it would be suffice to build and run any basic C++ program. But when I run
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
cout<<n<<endl;
return 0;
}
I know, there is some problem either in the build part or the compiler settings. I tried different compilers, from code blocks to independent Mingw-64 compiler without any success. In the past, I have used Code Blocks, it never required me to create an exe file or reference it. If I restart the program, it will show permission denied error which I know why it occurs.
Here is the error:
The system cannot find the file G:\Programming\C++\second.exe.
[Finished in 15.0s with exit code 1]
[shell_cmd: g++ "G:\Programming\C++\second.cpp" -o
"G:\Programming\C++/second" && "G:\Programming\C++/second"] [dir:
G:\Programming\C++] [path: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program
Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program
Files\Intel\WiFi\bin\;C:\Program Files\Common
Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows
Live\Shared;C:\Program Files\MATLAB\MATLAB Production
Server\R2015a\runtime\win64;C:\Program Files\MATLAB\MATLAB Production
Server\R2015a\bin;C:\Program Files\MATLAB\MATLAB Production
Server\R2015a\polyspace\bin;C:\Python27\;C:\Python27\Lib\site-packages\PyQt4;C:\Program
Files\Git\cmd;C:\Program Files
(x86)\mingw-w64\i686-7.1.0-posix-dwarf-rt_v5-rev0\mingw32\bin;C:\Users\80LM0141IH\Anaconda3;C:\Users\80LM0141IH\Anaconda3\Library\mingw-w64\bin;C:\Users\80LM0141IH\Anaconda3\Library\usr\bin;C:\Users\80LM0141IH\Anaconda3\Library\bin;C:\Users\80LM0141IH\Anaconda3\Scripts;C:\Users\80LM0141IH\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\80LM0141IH\AppData\Local\Programs\Python\Python36\;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;G:\Microsoft
VS
Code\bin;C:\Users\80LM0141IH\AppData\Local\GitHubDesktop\bin;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;";C:\Program
Files (x86)\Graphviz2.38\bin";C:\Program Files
(x86)\Graphviz2.34\bin;]

Before we start, you need to make sure that you have installed a C++ compiler and configured its path correctly. Make sure that you can invoke g++ command in the command line.
I see from your code that you need input from the standard input. Sublime Text's console can not accept input. So that maybe that is the problem. You need to run this program in a terminal
Try to replace your build system with following settings:
{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++, source.cpp, source.cc, source.cxx",
"variants":
[
{
"name": "Run in Terminal",
"linux": {
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name}; bash'", // for xterm
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -hold -e ${file_path}/${file_base_name}", // for xterm
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && konsole --hold -e ${file_path}/./${file_base_name}", // for konsole
},
"windows":{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k $file_base_name "
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start \"$file_base_name\" call $file_base_name"
},
"osx":{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
},
"shell": true,
},
]
}
Press Ctrl + Shift + B and choose C++ - Run in Terminal. It will compile and run the program in your cmd.
I can run your code snippet correctly in my environment. Let me know if you encounter any problem.

Related

How to Modify Code Runner's Default Command Line to Include g++ -std=c++11?

I recently tried running some code on VS Code but, as I'm using C++11 Standard code, I have to manually change the execution code in terminal to g++ -std=c++11 to actually run the code without errors.
I'm using Code Runner v0.11.8 by Jun Han
How can I set this option as the default?
On this page there is a Configuration section in which you have example how to specify compilation for c language. Write your own for C++ with flags you need and I think that it will be enough.
Follow below steps:
Open VsCode settings using CTRL+, or from left bottom side
click edit settings.json
add the following code snippet in the end:
{
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
}
& it's done.
NOTE: Make sure VsCode terminal profile is Command prompt.
My setup:
I use this execution command, it also remove .exe file after execution of the program:
"code-runner.executorMap": {
// -lm flag for linking c file with libm (will enable use of math.h in VsCode)
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -lm && $dir$fileNameWithoutExt.exe && del $dir$fileNameWithoutExt.exe ",
"cpp":"cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt.exe && $fileNameWithoutExt.exe && del $dir$fileNameWithoutExt.exe",
},
You can use any version like -std=c++14 or -std=c++17.
Hope it helps

Sublime Text 3 problem with compiling C++ programm

So I have following problem, I installed MinGW and it works I can manually compile my program using this g++ filename in cmd, and it crates exe file which I can run in cmd and see result. But when I try to use sublime text 3 and run it from there there is following error "[WinError 2] The specified file could not be found".
This is build system which I found and tried to use:
{
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
Which I found here Sublime Text C++ Build System
Im not sure if I describe my problem correctly so if you have more question feel free to ask.
Thanks :)

Sublime Text C++ Build System

I use the following compiler settings to compile my C++ code:
g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 A.cpp
But I am not sure how to setup the build system for the purpose, nor I want to try myself! I doubt that I may mess up the configuration (because I did once).
So I need help to configure my build system for the above compiler settings. How can I do that?
This build system works in my machine, basically its the default C++ build system with the extra options,
{
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -Wall -Wextra -O2 -fwrapv -Wfloat-equal -Wconversion -std=c++17 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
You can place it in /Users/{user}/Library/Application Support/Sublime Text 3/Packages/User directory for MacOs, C:\Users\{user}\AppData\Roaming\Sublime Text 3\Packages\User in Windows or ~/.config/sublime-text-3/Packages/User in Linux.
You can call the file something like C++17.sublime-build to differentiate it from the default build system.

How to run an executable file (.exe) in Windows Powershell with spaces in it?

Recently I wanted to try out Visual Studio Code for C++ programming. Since I am a fairly new to C++ and programming in general I didn't want to do the tedious setup process for running programs. So I installed the extension "Code Runner" which runs the cpp file in the terminal. It was all going great until I wanted to run cpp files with spaces in them. After some searching I found out that the extension uses some kind of batch file to change the directory of the terminal,compile it and run the output file. When there were spaces in the name the 2 parts of the name were being interpreted as separate commands.This is the code-runner.executorMap setting in the VS code settings:
"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 && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
The output for this was:
PS D:\> cd "d:\" ; if ($?) { g++ Test 1.cpp -o Test 1 } ; if ($?) { .\Test 1 }
g++.exe: error: Test: No such file or directory
g++.exe: error: 1.cpp: No such file or directory
g++.exe: error: 1: No such file or directory
I managed to fix some of the problem by surrounding the name and directory with double quotes.
"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\"",}
The output was:
PS D:\> cd "d:\" ; if ($?) { g++ "Test 1.cpp" -o "Test 1" } ; if ($?) { "./Test 1" }
./Test
The "cd" command and the "g++" command seemed to work but the part which actually executes the program to be malfunctioning. It interpreted the double quoted file name as a string and just displayed it as it is instead of running it. I searched on the internet on a solution to running .exe files with spaces in the name but couldn't find any. I'm sorry if the topic is unrelated or its due to my incompetence as I'm very new to programming.

GCC to compile compile 64 bit

I'm trying to compile C++ with G++ in 64 bit.
I'm using sublime text 3. This is my build system file :
{
"cmd": ["g++ ", "-m64", "-std=c++0x", "$file", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cxx, source.cpp",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -m64 -std=c++0x '${file}' -o '${file_path}/${file_base_name}.exe' && xterm -e bash -c '\"${file_path}/${file_base_name}.exe\" ; read'"]
}
]
}
Compiled files are still created as 32 bit.
When I run g++ --print-multi-lib I get:
32;#m32
x32;#mx32
I'm sure this has something to do with it, but I'm not sure how to fix it, (Pretty new to this), I've tried installing multilib 64 but nothing has changed.
Any help appreciated. Thank you :)
Your sublime text 3 IDE is confusing and spoiling you and you misconfigured it.
I hope that you are using some POSIX operating system, e.g. Linux.
Learn first to compile on the command line (in some terminal, outside of your editor or IDE). You absolutely need to understand how to invoke the GCC compiler (with commands in a terminal). Here are a few hints.
Be sure to have a recent GCC compiler, at least GCC 6 if you want C++11 or better. Perhaps you need to upgrade your compiler.
compiling a simple single-source program
To compile a single-source C++ program in single.cc into tinyprog executable, use
g++ -std=c++11 -Wall -g single.cc -o tinyprog
FYI, -std=c++11 sets your C++ standard, -Wall ask for almost all warnings (and you might even add -Wextra to get more of them), -g is for debug information (in DWARF) and -o tinyprog sets the output executable. Do ls -l tinyprog, ldd tinyprog, file tinyprog to understand more about your executable.
compiling a simple multi-source program
To compile a small multi-source C++ program, e.g. made of first.cc and second.cc into a smallprog executable
compile each of these into object files
g++ -std=c++11 -Wall -g -c first.cc -o first.o
g++ -std=c++11 -Wall -g -c second.cc -o second.o
link these object files into your executable
g++ -std=c++11 -Wall -g first.o second.o -o smallprog
building more complex programs
If you are using extra libraries, you may need to add some extra -I includedir option at compile time, and some extra -L libdir and -l libname options at link time. You might want to use pkg-config to help you. You could, if curious, add -v after g++ to be shown the actual steps done by g++
I run g++ --print-multi-lib
This just queries how your g++ compiler has been configured. You could also try g++ -v alone.
Once you are able to compile by (one or several) commands, consider using some build automation tool. To build a single program using GNU make, you could take inspiration from this.
Once you are fluent with compiling thru commands (either directly or thru some build automation tool like make or ninja or thru your script), read the documentation of your editor or IDE to configure it suitably.