How do I check if a compiler error shows in cmd? - c++

I decided to make it easier for me to compile and run my C++ programs through command prompt (I use cmd in conjunction with Sublime Text).
I added a cmd command (dragged .bat file in system32) to jump to my C++ files directory, and then a few more commands to compile my three work files.
I wanted cmd to output "Compiling work.cpp..." (which I've implemented) and if it doesn't return an error, output "Successfully Compiled" (What I don't know how to do). Is there anyway to implement this in cmd?
I'm using g++ (tdm64-1) 5.1.0

Use the errorlevel "environment variable":
g++ work.cpp
if %errorlevel% == 0 echo "Successfully Compiled"
This uses the syntax of environment variables (the % part), but errorlevel is a special "variable", which holds the status of recently finished command. If the status is 0, it was successful, otherwise an error occurred.

Related

Script for Notepad++ NppExec for C++ in ubuntu

I just switched to ubuntu and I wanted to setup notepad++ for CPP.
So I used the NppExec plugin to compile within notepad++,
My script was :
npp_save
g++ "$(FULL_CURRENT_PATH)" -o "$(CURRENT_DIRECTORY)\$(NAME_PART)obj"
./"$(NAME_PART)obj"
Here the "obj" I used is to just save the file with an "obj" keyword nothing else.
The last line ./"$(NAME_PART)obj" is to run the program.
But it looks not working in ubuntu, it produces this error:
NPP_SAVE: Z:\home\username\cpp\test.cpp
g++ "Z:\home\username\cpp\test.cpp" -o "Z:\home\username\cpp\testobj"
; about to start a child process: "g++ "Z:\home\username\cpp\test.cpp" -o "Z:\home\username\cpp\testobj"
CreatProcess() failed with error code 2:
File not found.
./"testobj"
; about to start a child process: "./"testobj""
CreatProcess() failed with error code 2:
File not found.
I have investigated some of what I think is the problem, so I think is the usage of / and \ in changing the directory.
I don't know how to fix that, so I can not be sure.
Any ideas? :) I am using vim btw in the same machine and it is working perfectly.
In theory it might be possible (see below), in practice it is rather convoluted and works only for simple compiles (like single file hello world type).
I would suggest you try a linux program, e.g.
an editor like
scite (same editing engine as notepad++) or
kate
or a real IDE like
kdeveloper or
qtcreator.
The problems with Notepad++ inside wine and g++ outside wine (from the linux install ) are this:
notepad++ inside wine under linux is still a windows program
NppExec can only do, what a cmd inside wine can do.
starting g++ directly inside cmd is an error due to g++ being a linux binary and not a windows binary
that is your CreatProcess() failed with error code 2, it means: you are trying to execute a linux program inside wine.
That does not work! (At least not so easy.)
Though you can start linux program inside cmd inside wine using start /unix ...
started this way, g++ wants linux paths and NppExec through its variables will provide only windows paths (whatever wine has set up as drives like Z:\home\username\src\hello.cpp)
though you can convert wine paths to linux paths via the winepath -u command.
g++ started through 'start /unix ... ' inside a cmd inside wine has no proper terminal to report errors to you
though you can start an xterm for g++ and have g++ reports its messages to the xterm
the downside is that g++ will report errors using the linux paths in the xterm, so you cannot double click on an error message an get to the corresponding filename and line.
You get the idea: its complicated not comfortable.
What worked for me for a helloword.cpp was this NppExec script:
NPP_SAVE
npp_run cmd /c start /unix /usr/bin/xterm -e "/usr/bin/winepath -u '$(FULL_CURRENT_PATH)' | xargs g++ -o /tmp/a.out && /tmp/a.out ; echo 'Press return'; read"
The second line
uses an xterm,
let winepath convert the Z:\home\... path to /home/... and
have that send to g++ for compilation using /tmp/a.out as binary
if compile is successfull, /tmp/a.out is executed
the echo and read are for keeping the xterm open so that you can read the output.
If you really want to use Notepad++ inside wine, one option might be using Gnu Make outside of wine and have NppExec run make all or make run similar to the g++ in my script example. That would work for more complicated compiles.

Unable to build and run c++ code in visual studio code

I have setup the correct path for mingw and am successfully able to compile my code using terminal in vscode. But when I press ctrl+shift+b to build i get the following error in my terminal
Executing task: C:\MinGW\bin\g++.exe -g 'c:\Users\Ansh Kapoor\Desktop\cpp\main.cpp' -o 'c:\Users\Ansh
Kapoor\Desktop\cpp\main.exe'<
/usr/bin/bash: C:MinGWbing++.exe: command not found
The terminal process terminated with exit code: 127
whereas the same code is successfully executed when I write
g++ main.cpp -o 'main.exe'
Now I have checked the path and environment variables and all those things are correct.
The error in finding the proper path was because of using git bash as the terminal rather than windows powershell or cmd.

C++ Programs can't run from Notepad++

I've tried to compile the c++ programs from the Notepad++ editor.
I am using mingw64 g++ compiler here.
Added the execution script using NppExec plugin in notepad++, the script I have used is given below,
npp_save
cd $(CURRENT_DIRECTORY)
D:\mingw64\bin\g++.exe -g "$(FILE_NAME)"
Saved this script as a macro and executed to run a cpp program, but getting the System error The program can't start because libwinpthread-1.dll is missing from your computer.
The same mingw64 compiler am using in Code::Blocks and its working fine.
How we can solve this issue ?
cmd /k gcc -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe" "$(FULL_CURRENT_PATH)" && CLS && "$(CURRENT_DIRECTORY)\$(NAME_PART).exe" & PAUSE & EXIT
Press ctrl+F5 and input this command, then click 'run'. You can also save it as a shortcut.

What needs to be done to get a distributable program from Eclipse?

I’ve produced a C++ program in Eclipse running on Redhat, which compiles and runs fine through Eclipse.
I thought that to run it separately to Eclipse you use the build artifact which is in the directory set via the project’s properties.
However this executable doesn’t run (I know it’s an executable as I’ve set it to be an executable via the project’s properties and it shows up as such via the ls command and the file explorer).
When attempting to run it using the executable’s name, I get the error:
bash: <filename>: command not found
When attempting to run it as a bash file:
<filename>: <filename>: cannot execute binary file
And when running it with "./" before the file name, nothing happens. Nothing new appears in the running processes and the terminal just goes to the next line as though I’d just pressed enter with no command.
Any help?
You've more or less figure out the first error yourself. when you just run <filename> , it is not in your PATH environment variable, so you get "command not found". You have to give a full or relative path when to the program in order to run it, even if you're in the same directory as the program - you run it with ./<filename>
When you do run your program, it appears to just exit as soon as you start it - we can't help much with that without knowing what the program does or see some code.
You can do some debugging, e.g. after the program just exits run echo $? to see if it exited with a particular exit value, or run your program using the strace tool to see what it does (or do it the usual way, insert printf debugging, or debug it with gdb)

MinGW not recognised through path variable

Eclipse CDT in Windows 7 is showing warnings like this:
Error launching external scanner info generator (g++ -E -P -v -dD C:/Users/User/workspace/.metadata/.plugins/org.eclipse.cdt.make.core/specs.cpp)
So I already set the path environment variable to C:\MinGW\bin, but if i type gcc --version in cmd the command is not recognised. However, if I change the directory to C:\MinGW\bin, it works fine.
Thanks in advance.
You can type "path" in the cmd window in order to see the path that's in effect (make sure you appended a value to the path variable and didn't replace it entirely, no spaces, etc.)
If opening a new cmd or reboot doesn't help, you can use process monitor with a filter of "process name is cmd.exe" to see where exactly cmd.exe looks for gcc and why it is failing.